Skip to content

Commit

Permalink
fix: 非标题行内出现#号导致的导航解析不正确
Browse files Browse the repository at this point in the history
  • Loading branch information
子奂 committed May 24, 2020
1 parent 10cac17 commit 074cb10
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class MarkdownNavbar extends Component {
getNavStructure() {
const contentWithoutCode = this.props.source
.replace(/^[^#]+\n/g, '')
.replace(/(?:[^\n#]+)#+\s([^#\n]+)\n*/g, '') // 匹配行内出现 # 号的情况
.replace(/^#\s[^#\n]*\n+/, '')
.replace(/```[^`\n]*\n+[^```]+```\n+/g, '')
.replace(/`([^`\n]+)`/g, '$1')
Expand Down Expand Up @@ -162,7 +163,11 @@ export class MarkdownNavbar extends Component {
const headings = document.querySelectorAll(`h${t.level}`);
const curHeading = Array.prototype.slice
.apply(headings)
.find(h => h.innerText === t.text && (!h.dataset || !h.dataset.id));
.find(
h =>
h.innerText.trim() === t.text.trim() &&
(!h.dataset || !h.dataset.id)
);

if (curHeading) {
curHeading.dataset.id = this.props.declarative
Expand All @@ -188,7 +193,7 @@ export class MarkdownNavbar extends Component {
.apply(headings)
.find(
h =>
h.innerText === t.text &&
h.innerText.trim() === t.text.trim() &&
!headingList.find(x => x.offsetTop === h.offsetTop)
);
if (curHeading) {
Expand Down

0 comments on commit 074cb10

Please sign in to comment.