Skip to content

Commit ead48e4

Browse files
committed
Fix header id and text might affects by mathjax tags
1 parent de2067f commit ead48e4

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

public/js/extra.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ function generateToc(id) {
689689
'top': -1,
690690
'class': 'toc',
691691
'ulClass': 'nav',
692-
'targetId': id
692+
'targetId': id,
693+
'process': getHeaderContent
693694
});
694695
if (target.text() == 'undefined')
695696
target.html('');
@@ -777,7 +778,7 @@ var linkifyAnchors = function (level, containingElement) {
777778
if (header.getElementsByClassName("anchor").length == 0) {
778779
if (typeof header.id == "undefined" || header.id == "") {
779780
//to escape characters not allow in css and humanize
780-
var id = slugifyWithUTF8(header.innerHTML);
781+
var id = slugifyWithUTF8(getHeaderContent(header));
781782
header.id = id;
782783
}
783784
header.insertBefore(anchorForId(header.id), header.firstChild);
@@ -795,6 +796,13 @@ function autoLinkify(view) {
795796
}
796797
}
797798

799+
function getHeaderContent(header) {
800+
var headerHTML = $(header).clone();
801+
headerHTML.find('.MathJax_Preview').remove();
802+
headerHTML.find('.MathJax').remove();
803+
return headerHTML[0].innerHTML;
804+
}
805+
798806
function deduplicatedHeaderId(view) {
799807
var headers = view.find(':header.raw').removeClass('raw').toArray();
800808
for (var i = 0; i < headers.length; i++) {
@@ -826,7 +834,8 @@ function renderTOC(view) {
826834
'level': 3,
827835
'top': -1,
828836
'class': 'toc',
829-
'targetId': id
837+
'targetId': id,
838+
'process': getHeaderContent
830839
});
831840
if (target.text() == 'undefined')
832841
target.html('');

public/vendor/md-toc.js

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
this.ulClass = options['ulClass'];
1414
this.tocTop = parseInt(options.top) || 0;
1515
this.elChilds = this.el.children;
16+
this.process = options['process'];
1617
if (!this.elChilds.length) return;
1718
this._init();
1819
}
@@ -53,7 +54,7 @@
5354
var j = i + 1;
5455
this._elTitleElement = this.elTitleElements[i];
5556
this._elTitleElementName = this._elTitleElement.tagName;
56-
this._elTitleElementText = this._elTitleElement.innerHTML.replace(/<(?:.|\n)*?>/gm, '');
57+
this._elTitleElementText = (typeof this.process === 'function' ? this.process(this._elTitleElement) : this._elTitleElement.innerHTML).replace(/<(?:.|\n)*?>/gm, '');
5758
var id = this._elTitleElement.getAttribute('id');
5859
if (!id) {
5960
this._elTitleElement.setAttribute('id', 'tip' + i);

0 commit comments

Comments
 (0)