@@ -2060,7 +2060,7 @@ function replaceDefinitionLists(s, protect) {
2060
2060
2061
2061
/** Inserts a table of contents in the document and then returns
2062
2062
[string, table], where the table maps strings to levels. */
2063
- function insertTableOfContents ( s , protect ) {
2063
+ function insertTableOfContents ( s , protect , exposer ) {
2064
2064
2065
2065
// Gather headers for table of contents (TOC). We
2066
2066
// accumulate a long and short TOC and then choose which
@@ -2080,6 +2080,7 @@ function insertTableOfContents(s, protect) {
2080
2080
s = s . rp ( / < h ( [ 1 - 6 ] ) > ( .* ?) < \/ h \1> / gi, function ( header , level , text ) {
2081
2081
level = parseInt ( level )
2082
2082
text = text . trim ( ) ;
2083
+
2083
2084
// If becoming more nested:
2084
2085
for ( var i = currentLevel ; i < level ; ++ i ) {
2085
2086
nameStack [ i ] = '' ;
@@ -2100,12 +2101,14 @@ function insertTableOfContents(s, protect) {
2100
2101
// numbers instead of mangled names
2101
2102
var oldname = 'toc' + number ;
2102
2103
2103
- table [ removeHTMLTags ( text ) . trim ( ) . toLowerCase ( ) ] = number ;
2104
+ var cleanText = removeHTMLTags ( exposer ( text ) ) . trim ( ) . toLowerCase ( ) ;
2105
+
2106
+ table [ cleanText ] = number ;
2104
2107
2105
2108
// Remove links from the title itself
2106
2109
text = text . rp ( / < a \s .* > ( .* ?) < \/ a > / g, '$1' ) ;
2107
2110
2108
- nameStack [ currentLevel - 1 ] = mangle ( removeHTMLTags ( text ) ) ;
2111
+ nameStack [ currentLevel - 1 ] = mangle ( cleanText ) ;
2109
2112
2110
2113
var name = nameStack . join ( '/' ) ;
2111
2114
@@ -2121,7 +2124,9 @@ function insertTableOfContents(s, protect) {
2121
2124
}
2122
2125
}
2123
2126
2124
- return entag ( 'a' , ' ' , protect ( 'class="target" name="' + name + '"' ) ) + entag ( 'a' , ' ' , protect ( 'class="target" name="' + oldname + '"' ) ) + header ;
2127
+ return entag ( 'a' , ' ' , protect ( 'class="target" name="' + name + '"' ) ) +
2128
+ entag ( 'a' , ' ' , protect ( 'class="target" name="' + oldname + '"' ) ) +
2129
+ header ;
2125
2130
} ) ;
2126
2131
2127
2132
if ( shortTOC . length > 0 ) {
@@ -2266,12 +2271,14 @@ function markdeepToHTML(str, elementMode) {
2266
2271
return PROTECT_CHARACTER + i + PROTECT_CHARACTER ;
2267
2272
}
2268
2273
2274
+ var exposeRan = false ;
2269
2275
/** Given the escaped identifier string from protect(), returns
2270
2276
the orginal string. */
2271
2277
function expose ( i ) {
2272
2278
// Strip the escape character and parse, then look up in the
2273
2279
// dictionary.
2274
2280
var j = parseInt ( i . ss ( 1 , i . length - 1 ) . rp ( / z / g, 'x' ) , PROTECT_RADIX ) ;
2281
+ exposeRan = true ;
2275
2282
return protectedStringArray [ j ] ;
2276
2283
}
2277
2284
@@ -2290,7 +2297,7 @@ function markdeepToHTML(str, elementMode) {
2290
2297
// separately
2291
2298
function makeHeaderFunc ( level ) {
2292
2299
return function ( match , header ) {
2293
- return '\n\n</p>\n<a ' + protect ( 'class="target" name="' + mangle ( removeHTMLTags ( header ) ) + '"' ) +
2300
+ return '\n\n</p>\n<a ' + protect ( 'class="target" name="' + mangle ( removeHTMLTags ( header . rp ( PROTECT_REGEXP , expose ) ) ) + '"' ) +
2294
2301
'> </a>' + entag ( 'h' + level , header ) + '\n<p>\n\n' ;
2295
2302
}
2296
2303
}
@@ -2605,7 +2612,6 @@ function markdeepToHTML(str, elementMode) {
2605
2612
// This is video. Any attributes provided will override the defaults given here
2606
2613
img = '<video ' + protect ( 'class="markdeep" src="' + url + '"' + attribs + ' width="480px" controls="true"' ) + '/>' ;
2607
2614
} else if ( / \. ( m p 3 | m p 2 | o g g | w a v | m 4 a | a a c | f l a c ) $ / i. test ( url ) ) {
2608
- // TODO
2609
2615
img = '<audio ' + protect ( 'class="markdeep" controls ' + attribs + '><source src="' + url + '">' ) + '</audio>' ;
2610
2616
} else if ( hash = url . match ( / ^ h t t p s : \/ \/ (?: w w w \. ) ? (?: y o u t u b e \. c o m \/ \S * ?v = | y o u t u \. b e \/ ) ( [ \w \d - ] + ) ( & .* ) ? $ / i) ) {
2611
2617
// Youtube video
@@ -2667,8 +2673,8 @@ function markdeepToHTML(str, elementMode) {
2667
2673
} ) ;
2668
2674
2669
2675
// REFERENCE IMAGE: ![...][ref attribs]
2670
- // Rewrite as a regular image for further processing
2671
- str = str . rp ( / ( ! \[ [ ^ \[ \] ] * ? \] ) \[ ( " ? ) ( [ ^ " < > \s ] + ?) \2( \s [ ^ \] ] * ?) ? \] / , function ( match , caption , maybeQuote , symbolicName , attribs ) {
2676
+ // Rewrite as a regular image for further processing below
2677
+ str = str . rp ( / ( ! \[ (?: [ \s \S ] + ? ) ? \] ) \[ ( " ? ) ( [ ^ " < > \s ] + ?) \2( \s [ ^ \] ] * ?) ? \] / g , function ( match , caption , maybeQuote , symbolicName , attribs ) {
2672
2678
symbolicName = symbolicName . toLowerCase ( ) . trim ( ) ;
2673
2679
var t = referenceLinkTable [ symbolicName ] ;
2674
2680
if ( ! t ) {
@@ -2681,7 +2687,6 @@ function markdeepToHTML(str, elementMode) {
2681
2687
}
2682
2688
} ) ;
2683
2689
2684
-
2685
2690
// IMAGE GRID: Rewrite rows and grids of images into a grid
2686
2691
var imageGridAttribs = protect ( 'width="100%"' ) ;
2687
2692
var imageGridRowAttribs = protect ( 'valign="top"' ) ;
@@ -2994,7 +2999,7 @@ function markdeepToHTML(str, elementMode) {
2994
2999
2995
3000
// If not in element mode and not an INSERT child, maybe add a TOC
2996
3001
if ( ! elementMode ) { // && ! myURLParse[2]) {
2997
- var temp = insertTableOfContents ( str , protect ) ;
3002
+ var temp = insertTableOfContents ( str , protect , function ( text ) { return text . rp ( PROTECT_REGEXP , expose ) } ) ;
2998
3003
str = temp [ 0 ] ;
2999
3004
var toc = temp [ 1 ] ;
3000
3005
// SECTION LINKS: Replace sec. [X], section [X], subsection [X]
@@ -3011,9 +3016,16 @@ function markdeepToHTML(str, elementMode) {
3011
3016
3012
3017
// Expose all protected values. We may need to do this
3013
3018
// recursively, because pre and code blocks can be nested.
3014
- while ( str . indexOf ( PROTECT_CHARACTER ) + 1 ) {
3019
+ var maxIterations = 50 ;
3020
+
3021
+ exposeRan = true ;
3022
+ while ( ( str . indexOf ( PROTECT_CHARACTER ) + 1 ) && exposeRan && ( maxIterations > 0 ) ) {
3023
+ exposeRan = false ;
3015
3024
str = str . rp ( PROTECT_REGEXP , expose ) ;
3025
+ -- maxIterations ;
3016
3026
}
3027
+
3028
+ if ( maxIterations <= 0 ) { console . log ( 'WARNING: Ran out of iterations while expanding protected substrings' ) ; }
3017
3029
3018
3030
// Warn about unused references
3019
3031
Object . keys ( referenceLinkTable ) . forEach ( function ( key ) {
0 commit comments