import * as path from 'path' ; import * as fs from 'fs' ; import { fixRelativeLinks , fiddleUrls , plaintextFix } from '../transfomers' ; import { IDocFile } from '../interfaces' ; import hubdown = require ( 'hubdown' ) ; import * as cheerio from 'cheerio' ; import { categoryNames , IGNORE_PATTERN } from '../constants' ; import { Entry } from 'walk-sync' ; export async function parseFile ( file , ids ) { const locale = file . relativePath . split ( '/' ) [ 0 ] ; const slug = path . basename ( file . relativePath , '.md' ) ; const category = file . relativePath . split ( '/' ) . slice ( 2 , - 1 ) . join ( '/' ) ; const categoryFancy = categoryNames [ category ] ; const href = `/docs/${ category }/${ slug }` . replace ( '//' , '/' ) ; const githubUrl = `https://github.com/electron/electron/tree/main${ href }.md` ; const crowdinFileId = ids [ `[electron.i18n] master/content/en-US${ href }.md` ] ; const isTutorial = category === 'tutorial' ; const isApiDoc = category === 'api' ; const isDevTutorial = category === 'development' ; const isApiStructureDoc = category === 'api/structures' ; const markdown = await fs . promises . readFile ( file . fullPath , 'utf8' ) ; const docFile = { locale , slug , href , crowdinFileId , githubUrl , category , categoryFancy , isTutorial , isApiDoc , isApiStructureDoc , isDevTutorial , } ; if ( markdown . includes ( IGNORE_PATTERN ) ) { docFile . ignore = true ; return docFile ; } const parsed = await hubdown ( markdown , { runBefore : [ fixRelativeLinks ( docFile ) , plaintextFix , fiddleUrls ] , highlight : { ignoreMissing : true , } , } ) ; docFile . html = parsed . content ; const $ = cheerio . load ( parsed . content || '' ) ; docFile . title = docFile . title || $ ( 'h1' ) . first ( ) . text ( ) . trim ( ) || $ ( 'h2' ) . first ( ) . text ( ) . replace ( 'Class: ' , '' ) ; docFile . description = docFile . description || $ ( 'blockquote' ) . first ( ) . text ( ) . trim ( ) ; return docFile ; }