1- import { marked } from 'marked' ;
2- import hljs from 'highlight.js' ;
3- import cheerio from 'cheerio' ;
4-
5- marked . setOptions ( {
6- highlight : code => hljs . highlightAuto ( code ) . value ,
7- } ) ;
8-
91export default function ( doc ) {
10- console . log ( 'transforming markup for document' ) ;
112 for ( const data of doc . data ) {
123 const { id, attributes } = data ;
13- console . log ( `\tGenerating markup for ${ id } ` ) ;
144
155 try {
166 const description = attributes . description ;
177
188 if ( description ) {
19- attributes . description = highlight ( description ) ;
9+ attributes . description = fixFilename ( description ) ;
2010 }
2111
22- // console.log('\tcompleted highlighting')
23-
2412 replaceDescriptionFor ( attributes . methods ) ;
2513 replaceDescriptionFor ( attributes . properties ) ;
2614 replaceDescriptionFor ( attributes . events ) ;
27- // console.log(`\tMarkup Generated for ${id}\n`)
2815 } catch ( e ) {
2916 console . log ( `Error generating markup for ${ id } ` ) ;
3017 console . log ( e ) ;
3118 process . exit ( 1 ) ;
3219 }
3320 }
3421
35- console . log ( 'completed markup transformation for document' ) ;
3622 return doc ;
3723}
3824
@@ -41,90 +27,19 @@ function replaceDescriptionFor(items) {
4127 items . forEach ( item => {
4228 let itemDescription = item . description ;
4329 if ( itemDescription ) {
44- item . description = highlight ( itemDescription ) ;
30+ item . description = fixFilename ( itemDescription ) ;
4531 }
4632 } ) ;
4733 }
4834}
4935
50- function highlight ( description ) {
36+ export function fixFilename ( description ) {
5137 if ( description ) {
52- description = description . replace ( / & # ( \d + ) ; / g, function ( match , dec ) {
53- return String . fromCharCode ( dec ) ;
54- } ) ;
55- }
56- let markedup = removeHLJSPrefix ( marked . parse ( description ) ) ;
57- let $ = cheerio . load ( markedup ) ;
58-
59- let codeBlocks = $ ( 'pre code' ) ;
60-
61- codeBlocks . each ( ( i , el ) => {
62- let element = $ ( el ) ;
63- let klass = element . attr ( 'class' ) ;
64- let lang = '' ;
65- let tableHeader = '' ;
66- if ( klass ) {
67- let type = klass . split ( '-' ) . pop ( ) ;
68- if ( isFile ( type ) ) {
69- tableHeader = `
70- <thead>
71- <tr>
72- <td colspan="2">${ type } </td>
73- </tr>
74- </thead>` ;
75- }
76- lang = determineLanguage ( type ) ;
77- }
78- let lines = element . html ( ) . split ( '\n' ) ;
79-
80- // get rid of empty blank line
81- if ( lines [ lines . length - 1 ] . trim ( ) === '' ) {
82- lines . pop ( ) ;
83- }
84-
85- let wrappedLines = `<pre>${ lines . join ( '\n' ) } </pre>` ;
86- let lineNumbers = lines . map ( ( _ , i ) => `${ i + 1 } \n` ) . join ( '' ) ;
87-
88- element . parent ( ) . after ( `<div class="highlight ${ lang } ">
89- <div class="ribbon"></div>
90- <div class="scroller">
91- <table class="CodeRay">${ tableHeader }
92- <tbody>
93- <tr>
94- <td class="line-numbers"><pre>${ lineNumbers } </pre></td>
95- <td class="code">${ wrappedLines } </td>
96- </tr>
97- </tbody>
98- </table>
99- </div>
100- </div>
101- ` ) ;
102-
103- element . parent ( ) . remove ( ) ;
104- } ) ;
105-
106- return $ . html ( ) ;
107- }
108-
109- function determineLanguage ( maybeFileName ) {
110- const lang = maybeFileName . split ( '.' ) . pop ( ) ;
111- switch ( lang ) {
112- case 'js' :
113- case 'javascript' :
114- return 'javascript' ;
115- case 'ts' :
116- return 'typescript' ;
117- case 'hbs' :
118- return 'handlebars' ;
119- default :
120- return lang ;
38+ description = description
39+ . replaceAll ( / ` ` ` ( [ ^ \n ] + ) \. ( j s | h b s | t s ) \n / g, '```$2 {data-filename=$1.$2}\n' )
40+ . replaceAll ( '```hbs' , '```handlebars' )
41+ . replaceAll ( '```no-highlight' , '```' ) ;
12142 }
122- }
123-
124- function removeHLJSPrefix ( string ) {
125- return string . replace ( / h l j s - / g, '' ) ;
126- }
12743
128- function isFile ( string ) {
129- return / \. / . test ( string ) ;
44+ return description ;
13045}
0 commit comments