@@ -7,14 +7,9 @@ import * as Code from '../code'
77import { Logger } from 'nagato'
88
99import URL from 'url-parse'
10+ import * as CM from 'commonmark'
1011
1112
12- let Marked = require ( 'marked' )
13- const MarkedOpts = {
14- gfm : true ,
15- tables : true ,
16- }
17-
1813class Meta {
1914 static PageKey = PageKey
2015
@@ -119,15 +114,11 @@ class Meta {
119114
120115 parse_impl ( content_type , data ) {
121116 if ( content_type === Net . ContentType . MARKDOWN ) {
122- let lexer = new Marked . Lexer ( MarkedOpts )
123- this . log . debug ( 'lexer' , lexer )
124-
125- this . tokens = lexer . lex ( data ) . map ( e => {
126- return new Map ( Object . entries ( e ) )
127- } )
128- this . log . debug ( `markdown (${ this . tokens . length } tokens)` , this . tokens )
117+ let reader = new CM . Parser ( { } )
118+ const parsed = reader . parse ( data )
119+ this . log . debug ( 'parsed' , parsed )
129120
130- this . process ( this . tokens )
121+ this . process ( parsed . walker ( ) )
131122 }
132123 }
133124
@@ -138,34 +129,35 @@ class Meta {
138129 return this . codes . get ( id )
139130 }
140131
141- process ( tokens ) {
132+ process ( walker ) {
142133 this . is_first_list = true
143134 this . single_bufs = [ ]
144135
145136 const old_level = this . log . opts . data . ctx . level
146137 this . log . opts . data . ctx . level = Logger . Level . info
147138
148139 try {
149- for ( const token of tokens ) {
150- this . process_single ( token )
140+ let ev = null
141+ while ( ev = walker . next ( ) ) {
142+ this . process_single ( ev )
151143 }
152144
153145 } finally {
154146 this . log . opts . data . ctx . level = old_level
155147 }
156148 }
157149
158- static isSampleCode ( lang , buf ) {
159- return lang === 'cpp' && buf . split ( / \n + / ) . some ( ( line ) => line . trim ( ) . match ( / ^ # i n c l u d e / ) )
150+ static isSampleCode ( lang , info ) {
151+ return info . includes ( 'example' )
160152 }
161153
162- process_single ( token ) {
163- this . log . debug ( `processing token <${ token . get ( 'type' ) } >` , token )
154+ process_single ( ev ) {
155+ const node = ev . node
156+ this . log . debug ( `[${ ev . entering ? 'enter' : 'leave' } ] ${ node . type } ` , ev )
164157
165- switch ( token . get ( ' type' ) ) {
158+ switch ( node . type ) {
166159 case 'heading' : {
167- this . heading_depth = token . depth
168- this . heading = token . get ( 'text' ) . trim ( )
160+ this . heading_depth = node . level
169161 break
170162 }
171163
@@ -201,34 +193,32 @@ class Meta {
201193
202194 case 'text' : {
203195 if ( this . is_first_list ) {
204- this . single_bufs [ this . single_bufs . length - 1 ] += token . get ( 'text' )
196+ this . single_bufs [ this . single_bufs . length - 1 ] += node . literal
205197 }
206198 break
207199 }
208200
209- case 'code' : {
210- const lang = token . get ( 'lang' )
211- const buf = token . get ( 'text' )
212-
213- this . log . info ( `found a code section (#${ this . last_key } )` )
201+ case 'code_block' : {
202+ const [ lang , ...info ] = node . info . split ( / \s + / )
203+ this . log . info ( `found a code block (#${ this . last_key } , lang: '${ lang } ', info: ${ info . length ? `[${ info . join ( ', ' ) } ]` : '(empty)' } )` , node )
214204
215- if ( ! Meta . isSampleCode ( lang , buf ) ) {
216- this . log . warn ( `unsupported code snippet (lang: ' ${ lang || '(empty)' } ') ` , buf )
217- if ( lang ) ++ this . last_key
205+ if ( ! Meta . isSampleCode ( lang , info ) ) {
206+ this . log . warn ( `unsupported code snippet` , node )
207+ ++ this . last_key
218208 break
219209 }
220210
221211 try {
222212 if ( lang === 'cpp' ) {
223- this . log . info ( `got C++ code (#${ this . last_key } )` , buf )
213+ this . log . info ( `got C++ code (#${ this . last_key } )` , node . literal )
224214
225215 const headers = [ this . andareMetaInfo . get ( 'header' ) ] . filter ( Boolean )
226216 const id = new Code . ID ( 'CPP' , this . last_key )
227217 this . codes . add (
228218 new Code . CPP (
229219 this . log ,
230220 id ,
231- buf ,
221+ node . literal ,
232222 {
233223 headers : headers ,
234224 } ,
@@ -237,7 +227,7 @@ class Meta {
237227 this . onCodeFound ( id )
238228
239229 } else {
240- this . log . warn ( `got code for unknown language '${ lang } ', skipping...` , buf )
230+ this . log . warn ( `got code for unknown language '${ lang } ', skipping...` , node . literal )
241231 }
242232
243233 } finally {
0 commit comments