@@ -76,7 +76,16 @@ async function htmlToMarkdown(options = {}) {
7676const excludedFiles = { "index.html" : "" , "global.html" : "" , "custom.css" : "" } ;
7777const inputDirectory = path . join ( "dist" , "api" ) ;
7878const outputDirectory = path . join ( "docs" , "api" ) ;
79+ let packageTagName = "https://github.com/UI5/cli/blob/main/packages/" ;
7980
81+ // 1. Find tag name
82+ if ( process . argv [ 2 ] == "gh-pages" ) {
83+ // Read package.json of packages/builder
84+ const builderJson = JSON . parse ( fs . readFileSync ( "tmp/packages/@ui5/builder/package.json" ) ) ;
85+ packageTagName = `https://github.com/UI5/cli/blob/cli-v${ builderJson [ "version" ] } /packages/` ;
86+ }
87+
88+ // 2. Check and create api directory, also remove all existing files if it exists
8089if ( ! fs . existsSync ( outputDirectory ) ) {
8190 fs . mkdirSync ( outputDirectory ) ;
8291} else {
@@ -85,19 +94,37 @@ if (!fs.existsSync(outputDirectory)) {
8594 }
8695}
8796
97+ // 3. Iterate through every .html generated by jsdoc
8898for ( const file of fs . readdirSync ( path . join ( "dist" , "api" ) ) ) {
99+ // Skip js files
89100 if ( excludedFiles [ file ] !== undefined ) continue ;
101+
90102 const filePath = path . join ( inputDirectory , file ) ;
103+ // Skip directories
91104 if ( fs . statSync ( filePath ) . isDirectory ( ) ) continue ;
105+
106+ // Read the html file
92107 const htmlString = fs . readFileSync ( filePath ) ;
108+
109+ // Convert html to markdown
93110 let markdown = await htmlToMarkdown ( {
94111 html : htmlString
95112 } ) ;
113+
114+ // Escape characters in markdown
96115 markdown = escapeMarkdown ( markdown ) ;
116+
117+ // Fix some markdown syntax e.g. <optional> -> Optional
97118 markdown = fixMarkdown ( markdown ) ;
98- if ( file . endsWith ( ".js.html" ) ) {
99- markdown = markdown . replace ( "```" , "```javascript" ) ;
119+
120+ // Point all js links to GitHub
121+ const linkMatches = markdown . matchAll ( / \( ( .* ?) \. j s .h t m l \) / g) ;
122+ for ( const linkMatch of linkMatches ) {
123+ const githubUrl = packageTagName + linkMatch [ 1 ] . replace ( ".js.html" , "" ) . replaceAll ( "_" , "/" ) . replace ( "project" , "" ) + ".js" ;
124+ markdown = markdown . replaceAll ( linkMatch [ 1 ] + ".js.html#line" , githubUrl + "#L" ) . replaceAll ( linkMatch [ 1 ] + ".js.html" , githubUrl ) ;
100125 }
126+
127+ // Save markdown file
101128 fs . writeFileSync (
102129 path . join ( outputDirectory , file . replace ( ".html" , ".md" ) ) ,
103130 markdown
0 commit comments