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 ) {
102 console . log ( 'transforming markup for document' ) ;
113 for ( const data of doc . data ) {
@@ -16,7 +8,7 @@ export default function (doc) {
168 const description = attributes . description ;
179
1810 if ( description ) {
19- attributes . description = highlight ( description ) ;
11+ attributes . description = fixFilename ( description ) ;
2012 }
2113
2214 // console.log('\tcompleted highlighting')
@@ -41,90 +33,18 @@ function replaceDescriptionFor(items) {
4133 items . forEach ( item => {
4234 let itemDescription = item . description ;
4335 if ( itemDescription ) {
44- item . description = highlight ( itemDescription ) ;
36+ item . description = fixFilename ( itemDescription ) ;
4537 }
4638 } ) ;
4739 }
4840}
4941
50- function highlight ( description ) {
42+ function fixFilename ( description ) {
5143 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' ) ;
5547 }
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- }
12748
128- function isFile ( string ) {
129- return / \. / . test ( string ) ;
49+ return description ;
13050}
0 commit comments