@@ -8,31 +8,40 @@ function processIndexTerm(term) {
88 return terms . length == 1 ? terms [ 0 ] : "[" + terms . join ( ", " ) + "]"
99}
1010
11+ function maybeQuote ( value ) {
12+ return / \W / . test ( value ) ? JSON . stringify ( value ) : value
13+ }
14+
1115text = text
1216 . replace ( / ^ ( : \w + : \s * .+ \n ) + / , function ( meta ) {
1317 let re = / (?: ^ | \n ) : ( \w + ) : \s * ( .+ ) / g, m , props = [ ]
14- while ( m = re . exec ( meta ) ) props . push ( m [ 1 ] + ": " + m [ 2 ] )
15- return `{{meta {${ props . join ( ", " ) } }}}`
18+ while ( m = re . exec ( meta ) ) props . push ( m [ 1 ] + ": " + ( m [ 1 ] == "load_files" ? m [ 2 ] : maybeQuote ( m [ 2 ] ) ) )
19+ return `{{meta {${ props . join ( ", " ) } }}}\n `
1620 } )
1721 . replace ( / \n ( = + ) ( .* ?) = + \n / g, function ( _ , depth , title ) {
1822 return "\n" + "#" . repeat ( depth . length ) + " " + title + "\n"
1923 } )
20- . replace ( / \n i m a g e : : ( [ ^ \] ] + ) \[ ( .* ?) \] / g, function ( _ , url , meta ) {
21- return "\n{{figure {url: " + JSON . stringify ( url ) + ", " + meta . replace ( / = " / g, ": \"" ) + "}}}"
24+ . replace ( / \n i m a g e : : ( [ ^ \[ ] + ) \[ ( .* ?) \] \n / g, function ( _ , url , meta ) {
25+ return "\n{{figure {url: " + JSON . stringify ( url ) + ", " + meta . replace ( / = " / g, ": \"" ) + "}}}\n "
2226 } )
23- . replace ( / \n ( \[ c h a p t e r q u o t e = .* ?\] \n ) ? \[ q u o t e , ( [ ^ \] ] + ) \] \n _ _ _ _ \n ( [ ^ ] * ?) _ _ _ _ \n / g, function ( _ , chapter , author , content ) {
24- let match = / ( [ ^ , ] + ) , ( .+ ) / . exec ( author ) , title = null
25- if ( match ) { title = match [ 2 ] ; author = match [ 1 ] }
26- return "\n{{quote {" + ( chapter ? "chapter: true, " : "" ) + "author: " + JSON . stringify ( author ) +
27- ( title ? ", title: " + JSON . stringify ( title ) : "" ) + "}\n\n" + content + "\n}}\n"
27+ . replace ( / \n ( \[ c h a p t e r q u o t e = .* ?\] \n ) ? (?: \[ q u o t e , \s * ( [ ^ \] ] + ) \] \n ) ? _ _ _ + \n ( [ ^ ] * ?) _ _ _ + \n / g, function ( _ , chapter , author , content ) {
28+ let props = [ ]
29+ if ( author ) {
30+ let match = / ( [ ^ , ] + ) , ( .+ ) / . exec ( author ) , title = null
31+ if ( match ) props . push ( `author: ${ JSON . stringify ( match [ 1 ] ) } ` , `title: ${ JSON . stringify ( match [ 2 ] ) } ` )
32+ else props . push ( `author: ${ JSON . stringify ( author ) } ` )
33+ }
34+ if ( chapter ) props . push ( "chapter: true" )
35+ return `\n{{quote${ props . length ? " {" + props . join ( ", " ) + "}" : "" } \n\n${ content } \nquote}}\n`
2836 } )
29- . replace ( / \n \n + ( (?: (? ! \n \n ) [ ^ ] ) * ?\( \( \( (?: (? ! \n \n ) [ ^ ] ) * ) / g, function ( _ , para ) {
37+ . replace ( / \n \n + ( (?: (? ! \n \n ) [ ^ ] ) * ?\( \( \( (?: (? ! \n \n ) [ ^ ] ) * ) / g, function ( all , para ) {
3038 let terms = [ ]
3139 para = para . replace ( / \( \( \( ( (?: \( [ ^ \) ] * \) | [ ^ ] ) * ?) \) \) \) / g, function ( _ , content ) {
3240 terms . push ( content )
3341 return ""
3442 } ) . replace ( / ^ \s * / , "" )
35- return "\n\n{{index " + terms . map ( processIndexTerm ) . join ( ", " ) + "}}\n\n" + para
43+ if ( terms . length ) return "\n\n{{index " + terms . map ( processIndexTerm ) . join ( ", " ) + "}}\n\n" + para
44+ else return all
3645 } )
3746 . replace ( / \b i n d e x s e e : \[ ( .* ?) , \s * ( .* ?) \] \s * / g, function ( _ , term , ref ) {
3847 return "{{indexsee " + processIndexTerm ( term ) + ", " + processIndexTerm ( ref ) + "}}\n\n"
@@ -44,16 +53,16 @@ text = text
4453 if ( sandbox ) params . push ( "sandbox-" + sandbox )
4554 return "\n```" + params . join ( " " ) + "\n" + content + "\n```\n"
4655 } )
47- . replace ( / \n \/ \/ (?: ( s t a r t _ c o d e ) | t e s t : ( .* ) | i n c l u d e _ c o d e ( .* ) ) / g, function ( _ , startCode , test , includeCode ) {
48- if ( startCode ) return " \n{{startCode}}"
56+ . replace ( / \n \/ \/ (?: ( s t a r t _ c o d e (?: ( . * ) ) ? ) | t e s t : ( .* ) | i n c l u d e _ c o d e ( .* ) ) / g, function ( _ , startCode , startCodeParam , test , includeCode ) {
57+ if ( startCode ) return ` \n{{startCode${ startCodeParam ? " " + JSON . stringify ( startCodeParam ) : "" } }}`
4958 if ( test ) return "\n{{test " + test + "}}"
5059 return "\n{{includeCode " + JSON . stringify ( includeCode ) + "}}"
5160 } )
5261 . replace ( / \b l i n k : ( [ ^ \[ ] + ) \[ ( .* ?) \] / g, function ( _ , url , content ) {
5362 return "[" + content + "](" + url + ")"
5463 } )
5564 . replace ( / \n i f d e f : : ( \w + ?) _ t a r g e t \[ \] \n ( [ ^ ] * ?) \n e n d i f : : .* / g, function ( _ , type , content ) {
56- return "\n{{if " + type + "\n" + content + "\n }}"
65+ return "\n{{if " + type + "\n" + content + "\nif }}"
5766 } )
5867 . replace ( / \+ \+ (? ! | \) ) ( (?: (? ! \n \n ) [ ^ ] ) + ) \+ \+ / g, function ( _ , text ) {
5968 return "_" + text + "_"
@@ -62,7 +71,10 @@ text = text
6271 return "_" + text + "_"
6372 } )
6473 . replace ( / \n \[ \[ ( .* ?) \] \] \n / g, function ( _ , name ) {
65- return `\n{{id ${ / \W / . test ( name ) ? JSON . stringify ( name ) : name } }}\n`
74+ return `\n{{id ${ maybeQuote ( name ) } }}\n`
75+ } )
76+ . replace ( / \n ! ! h i n t ! ! \n ( [ ^ ] + ?) \n ! ! h i n t ! ! / g, function ( _ , content ) {
77+ return `\n{{hint\n${ content } \nhint}}`
6678 } )
6779 . replace ( / \[ s i c ] / , "\\[sic]" )
6880
0 commit comments