Skip to content

Commit

Permalink
fix: anchor positioning fails when the page is initialized If there i…
Browse files Browse the repository at this point in the history
…s a symbol or blank space in the title
  • Loading branch information
子奂 committed Jan 4, 2020
1 parent 68a4c5c commit 077a723
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export class MarkdownNavbar extends Component {
}, 1e3);
}

shouldComponentUpdate() {
this._initheadingsId(false);
shouldComponentUpdate(nextProps) {
if (nextProps.source !== this.props.source) {
this._initheadingsId();
}
return true;
}

Expand Down Expand Up @@ -126,7 +128,13 @@ export class MarkdownNavbar extends Component {
}, 1e2);
}

_initheadingsId(goTarget = true) {
_initheadingsId() {
const headingId = decodeURIComponent(
this.props.declarative
? window.location.hash.replace(/^#/, '').trim()
: (window.location.hash.match(/heading-\d+/g) || [])[0]
);

this._getNavStructure().forEach(t => {
const headings = document.querySelectorAll(`h${t.level}`);
const curheading = Array.prototype.slice
Expand All @@ -144,17 +152,11 @@ export class MarkdownNavbar extends Component {
: `heading-${t.index}`;
}

const headingId = this.props.declarative
? window.location.hash.replace(/^#/, '').trim()
: (window.location.hash.match(/heading-\d+/g) || [])[0];

if (headingId && headingId === curheading.dataset.id) {
if (goTarget) {
this._scrollToTarget(headingId);
this.setState({
currentListNo: t.listNo,
});
}
this._scrollToTarget(headingId);
this.setState({
currentListNo: t.listNo,
});
}
});
}
Expand Down

0 comments on commit 077a723

Please sign in to comment.