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
2
console . log ( 'transforming markup for document' ) ;
11
3
for ( const data of doc . data ) {
@@ -16,7 +8,7 @@ export default function (doc) {
16
8
const description = attributes . description ;
17
9
18
10
if ( description ) {
19
- attributes . description = highlight ( description ) ;
11
+ attributes . description = fixFilename ( description ) ;
20
12
}
21
13
22
14
// console.log('\tcompleted highlighting')
@@ -41,90 +33,18 @@ function replaceDescriptionFor(items) {
41
33
items . forEach ( item => {
42
34
let itemDescription = item . description ;
43
35
if ( itemDescription ) {
44
- item . description = highlight ( itemDescription ) ;
36
+ item . description = fixFilename ( itemDescription ) ;
45
37
}
46
38
} ) ;
47
39
}
48
40
}
49
41
50
- function highlight ( description ) {
42
+ function fixFilename ( description ) {
51
43
if ( description ) {
52
- description = description . replace ( / & # ( \d + ) ; / g , function ( match , dec ) {
53
- return String . fromCharCode ( dec ) ;
54
- } ) ;
44
+ description = description
45
+ . replaceAll ( / ` ` ` ( [ ^ \n ] + ) \. ( j s | h b s | t s ) \n / g , '```$2 {data-filename=$1.$2}\n' )
46
+ . replaceAll ( '```hbs' , '```handlebars' ) ;
55
47
}
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 ;
121
- }
122
- }
123
-
124
- function removeHLJSPrefix ( string ) {
125
- return string . replace ( / h l j s - / g, '' ) ;
126
- }
127
48
128
- function isFile ( string ) {
129
- return / \. / . test ( string ) ;
49
+ return description ;
130
50
}
0 commit comments