Skip to content

Commit efc74e3

Browse files
committed
archive
1 parent 9c06c8a commit efc74e3

File tree

4 files changed

+54
-30
lines changed

4 files changed

+54
-30
lines changed

1.03/markdeep.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ function replaceDefinitionLists(s, protect) {
20602060

20612061
/** Inserts a table of contents in the document and then returns
20622062
[string, table], where the table maps strings to levels. */
2063-
function insertTableOfContents(s, protect) {
2063+
function insertTableOfContents(s, protect, exposer) {
20642064

20652065
// Gather headers for table of contents (TOC). We
20662066
// accumulate a long and short TOC and then choose which
@@ -2080,6 +2080,7 @@ function insertTableOfContents(s, protect) {
20802080
s = s.rp(/<h([1-6])>(.*?)<\/h\1>/gi, function (header, level, text) {
20812081
level = parseInt(level)
20822082
text = text.trim();
2083+
20832084
// If becoming more nested:
20842085
for (var i = currentLevel; i < level; ++i) {
20852086
nameStack[i] = '';
@@ -2100,12 +2101,14 @@ function insertTableOfContents(s, protect) {
21002101
// numbers instead of mangled names
21012102
var oldname = 'toc' + number;
21022103

2103-
table[removeHTMLTags(text).trim().toLowerCase()] = number;
2104+
var cleanText = removeHTMLTags(exposer(text)).trim().toLowerCase();
2105+
2106+
table[cleanText] = number;
21042107

21052108
// Remove links from the title itself
21062109
text = text.rp(/<a\s.*>(.*?)<\/a>/g, '$1');
21072110

2108-
nameStack[currentLevel - 1] = mangle(removeHTMLTags(text));
2111+
nameStack[currentLevel - 1] = mangle(cleanText);
21092112

21102113
var name = nameStack.join('/');
21112114

@@ -2121,7 +2124,9 @@ function insertTableOfContents(s, protect) {
21212124
}
21222125
}
21232126

2124-
return entag('a', '&nbsp;', protect('class="target" name="' + name + '"')) + entag('a', '&nbsp;', protect('class="target" name="' + oldname + '"')) + header;
2127+
return entag('a', '&nbsp;', protect('class="target" name="' + name + '"')) +
2128+
entag('a', '&nbsp;', protect('class="target" name="' + oldname + '"')) +
2129+
header;
21252130
});
21262131

21272132
if (shortTOC.length > 0) {
@@ -2266,12 +2271,14 @@ function markdeepToHTML(str, elementMode) {
22662271
return PROTECT_CHARACTER + i + PROTECT_CHARACTER;
22672272
}
22682273

2274+
var exposeRan = false;
22692275
/** Given the escaped identifier string from protect(), returns
22702276
the orginal string. */
22712277
function expose(i) {
22722278
// Strip the escape character and parse, then look up in the
22732279
// dictionary.
22742280
var j = parseInt(i.ss(1, i.length - 1).rp(/z/g, 'x'), PROTECT_RADIX);
2281+
exposeRan = true;
22752282
return protectedStringArray[j];
22762283
}
22772284

@@ -2290,7 +2297,7 @@ function markdeepToHTML(str, elementMode) {
22902297
// separately
22912298
function makeHeaderFunc(level) {
22922299
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))) + '"') +
22942301
'>&nbsp;</a>' + entag('h' + level, header) + '\n<p>\n\n';
22952302
}
22962303
}
@@ -2605,7 +2612,6 @@ function markdeepToHTML(str, elementMode) {
26052612
// This is video. Any attributes provided will override the defaults given here
26062613
img = '<video ' + protect('class="markdeep" src="' + url + '"' + attribs + ' width="480px" controls="true"') + '/>';
26072614
} else if (/\.(mp3|mp2|ogg|wav|m4a|aac|flac)$/i.test(url)) {
2608-
// TODO
26092615
img = '<audio ' + protect('class="markdeep" controls ' + attribs + '><source src="' + url + '">') + '</audio>';
26102616
} else if (hash = url.match(/^https:\/\/(?:www\.)?(?:youtube\.com\/\S*?v=|youtu\.be\/)([\w\d-]+)(&.*)?$/i)) {
26112617
// Youtube video
@@ -2667,8 +2673,8 @@ function markdeepToHTML(str, elementMode) {
26672673
});
26682674

26692675
// 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) {
26722678
symbolicName = symbolicName.toLowerCase().trim();
26732679
var t = referenceLinkTable[symbolicName];
26742680
if (! t) {
@@ -2681,7 +2687,6 @@ function markdeepToHTML(str, elementMode) {
26812687
}
26822688
});
26832689

2684-
26852690
// IMAGE GRID: Rewrite rows and grids of images into a grid
26862691
var imageGridAttribs = protect('width="100%"');
26872692
var imageGridRowAttribs = protect('valign="top"');
@@ -2994,7 +2999,7 @@ function markdeepToHTML(str, elementMode) {
29942999

29953000
// If not in element mode and not an INSERT child, maybe add a TOC
29963001
if (! elementMode) {// && ! myURLParse[2]) {
2997-
var temp = insertTableOfContents(str, protect);
3002+
var temp = insertTableOfContents(str, protect, function (text) {return text.rp(PROTECT_REGEXP, expose)});
29983003
str = temp[0];
29993004
var toc = temp[1];
30003005
// SECTION LINKS: Replace sec. [X], section [X], subsection [X]
@@ -3011,9 +3016,16 @@ function markdeepToHTML(str, elementMode) {
30113016

30123017
// Expose all protected values. We may need to do this
30133018
// 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;
30153024
str = str.rp(PROTECT_REGEXP, expose);
3025+
--maxIterations;
30163026
}
3027+
3028+
if (maxIterations <= 0) { console.log('WARNING: Ran out of iterations while expanding protected substrings'); }
30173029

30183030
// Warn about unused references
30193031
Object.keys(referenceLinkTable).forEach(function (key) {

1.03/markdeep.min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

latest/markdeep.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ function replaceDefinitionLists(s, protect) {
20602060

20612061
/** Inserts a table of contents in the document and then returns
20622062
[string, table], where the table maps strings to levels. */
2063-
function insertTableOfContents(s, protect) {
2063+
function insertTableOfContents(s, protect, exposer) {
20642064

20652065
// Gather headers for table of contents (TOC). We
20662066
// accumulate a long and short TOC and then choose which
@@ -2080,6 +2080,7 @@ function insertTableOfContents(s, protect) {
20802080
s = s.rp(/<h([1-6])>(.*?)<\/h\1>/gi, function (header, level, text) {
20812081
level = parseInt(level)
20822082
text = text.trim();
2083+
20832084
// If becoming more nested:
20842085
for (var i = currentLevel; i < level; ++i) {
20852086
nameStack[i] = '';
@@ -2100,12 +2101,14 @@ function insertTableOfContents(s, protect) {
21002101
// numbers instead of mangled names
21012102
var oldname = 'toc' + number;
21022103

2103-
table[removeHTMLTags(text).trim().toLowerCase()] = number;
2104+
var cleanText = removeHTMLTags(exposer(text)).trim().toLowerCase();
2105+
2106+
table[cleanText] = number;
21042107

21052108
// Remove links from the title itself
21062109
text = text.rp(/<a\s.*>(.*?)<\/a>/g, '$1');
21072110

2108-
nameStack[currentLevel - 1] = mangle(removeHTMLTags(text));
2111+
nameStack[currentLevel - 1] = mangle(cleanText);
21092112

21102113
var name = nameStack.join('/');
21112114

@@ -2121,7 +2124,9 @@ function insertTableOfContents(s, protect) {
21212124
}
21222125
}
21232126

2124-
return entag('a', '&nbsp;', protect('class="target" name="' + name + '"')) + entag('a', '&nbsp;', protect('class="target" name="' + oldname + '"')) + header;
2127+
return entag('a', '&nbsp;', protect('class="target" name="' + name + '"')) +
2128+
entag('a', '&nbsp;', protect('class="target" name="' + oldname + '"')) +
2129+
header;
21252130
});
21262131

21272132
if (shortTOC.length > 0) {
@@ -2266,12 +2271,14 @@ function markdeepToHTML(str, elementMode) {
22662271
return PROTECT_CHARACTER + i + PROTECT_CHARACTER;
22672272
}
22682273

2274+
var exposeRan = false;
22692275
/** Given the escaped identifier string from protect(), returns
22702276
the orginal string. */
22712277
function expose(i) {
22722278
// Strip the escape character and parse, then look up in the
22732279
// dictionary.
22742280
var j = parseInt(i.ss(1, i.length - 1).rp(/z/g, 'x'), PROTECT_RADIX);
2281+
exposeRan = true;
22752282
return protectedStringArray[j];
22762283
}
22772284

@@ -2290,7 +2297,7 @@ function markdeepToHTML(str, elementMode) {
22902297
// separately
22912298
function makeHeaderFunc(level) {
22922299
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))) + '"') +
22942301
'>&nbsp;</a>' + entag('h' + level, header) + '\n<p>\n\n';
22952302
}
22962303
}
@@ -2605,7 +2612,6 @@ function markdeepToHTML(str, elementMode) {
26052612
// This is video. Any attributes provided will override the defaults given here
26062613
img = '<video ' + protect('class="markdeep" src="' + url + '"' + attribs + ' width="480px" controls="true"') + '/>';
26072614
} else if (/\.(mp3|mp2|ogg|wav|m4a|aac|flac)$/i.test(url)) {
2608-
// TODO
26092615
img = '<audio ' + protect('class="markdeep" controls ' + attribs + '><source src="' + url + '">') + '</audio>';
26102616
} else if (hash = url.match(/^https:\/\/(?:www\.)?(?:youtube\.com\/\S*?v=|youtu\.be\/)([\w\d-]+)(&.*)?$/i)) {
26112617
// Youtube video
@@ -2667,8 +2673,8 @@ function markdeepToHTML(str, elementMode) {
26672673
});
26682674

26692675
// 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) {
26722678
symbolicName = symbolicName.toLowerCase().trim();
26732679
var t = referenceLinkTable[symbolicName];
26742680
if (! t) {
@@ -2681,7 +2687,6 @@ function markdeepToHTML(str, elementMode) {
26812687
}
26822688
});
26832689

2684-
26852690
// IMAGE GRID: Rewrite rows and grids of images into a grid
26862691
var imageGridAttribs = protect('width="100%"');
26872692
var imageGridRowAttribs = protect('valign="top"');
@@ -2994,7 +2999,7 @@ function markdeepToHTML(str, elementMode) {
29942999

29953000
// If not in element mode and not an INSERT child, maybe add a TOC
29963001
if (! elementMode) {// && ! myURLParse[2]) {
2997-
var temp = insertTableOfContents(str, protect);
3002+
var temp = insertTableOfContents(str, protect, function (text) {return text.rp(PROTECT_REGEXP, expose)});
29983003
str = temp[0];
29993004
var toc = temp[1];
30003005
// SECTION LINKS: Replace sec. [X], section [X], subsection [X]
@@ -3011,9 +3016,16 @@ function markdeepToHTML(str, elementMode) {
30113016

30123017
// Expose all protected values. We may need to do this
30133018
// 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;
30153024
str = str.rp(PROTECT_REGEXP, expose);
3025+
--maxIterations;
30163026
}
3027+
3028+
if (maxIterations <= 0) { console.log('WARNING: Ran out of iterations while expanding protected substrings'); }
30173029

30183030
// Warn about unused references
30193031
Object.keys(referenceLinkTable).forEach(function (key) {

latest/markdeep.min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)