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
-
9
1
export default function ( doc ) {
10
- console . log ( 'transforming markup for document' ) ;
11
2
for ( const data of doc . data ) {
12
3
const { id, attributes } = data ;
13
- console . log ( `\tGenerating markup for ${ id } ` ) ;
14
4
15
5
try {
16
6
const description = attributes . description ;
17
7
18
8
if ( description ) {
19
- attributes . description = highlight ( description ) ;
9
+ attributes . description = fixFilename ( description ) ;
20
10
}
21
11
22
- // console.log('\tcompleted highlighting')
23
-
24
12
replaceDescriptionFor ( attributes . methods ) ;
25
13
replaceDescriptionFor ( attributes . properties ) ;
26
14
replaceDescriptionFor ( attributes . events ) ;
27
- // console.log(`\tMarkup Generated for ${id}\n`)
28
15
} catch ( e ) {
29
16
console . log ( `Error generating markup for ${ id } ` ) ;
30
17
console . log ( e ) ;
31
18
process . exit ( 1 ) ;
32
19
}
33
20
}
34
21
35
- console . log ( 'completed markup transformation for document' ) ;
36
22
return doc ;
37
23
}
38
24
@@ -41,90 +27,19 @@ function replaceDescriptionFor(items) {
41
27
items . forEach ( item => {
42
28
let itemDescription = item . description ;
43
29
if ( itemDescription ) {
44
- item . description = highlight ( itemDescription ) ;
30
+ item . description = fixFilename ( itemDescription ) ;
45
31
}
46
32
} ) ;
47
33
}
48
34
}
49
35
50
- function highlight ( description ) {
36
+ export function fixFilename ( description ) {
51
37
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' , '```' ) ;
121
42
}
122
- }
123
-
124
- function removeHLJSPrefix ( string ) {
125
- return string . replace ( / h l j s - / g, '' ) ;
126
- }
127
43
128
- function isFile ( string ) {
129
- return / \. / . test ( string ) ;
44
+ return description ;
130
45
}
0 commit comments