Skip to content

Commit 2dcc2a0

Browse files
ayazhafizcalebcartwright
authored andcommitted
Ensure that doc url hash IDs are scrolled to on page load
I broke this a few weeks ago so I'll fix it Demo: https://5f0fa445faca4aff5f580029--naughty-borg-09b903.netlify.app/?version=master&search=#brace_style
1 parent 247f976 commit 2dcc2a0

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

docs/index.html

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
shouldStable: false,
109109
version: versionNumber,
110110
oldVersion: undefined,
111-
versionOptions: ['master']
111+
versionOptions: ['master'],
112+
scrolledOnce: false,
112113
},
113114
asyncComputed: {
114115
async outputHtml() {
@@ -156,18 +157,20 @@
156157
renderer.heading = function(text, level) {
157158
const id = htmlToId(text);
158159
return `<h${level}>
159-
<a href="#${id}" name="${id}" class="header-link">${text}</a>
160+
<a id="${id}" href="#${id}" name="${id}" class="header-link">${text}</a>
160161
</h${level}>`;
161162
};
162163

163-
return marked.parser(ast, {
164+
const html = marked.parser(ast, {
164165
highlight(code, lang) {
165166
return hljs.highlight(lang ? lang : 'rust', code).value;
166167
},
167168
headerIds: true,
168169
headerPrefix: '',
169170
renderer,
170171
});
172+
document.dispatchEvent(new Event('htmlbuilt'));
173+
return html;
171174
}
172175
},
173176
created: async function() {
@@ -178,12 +181,15 @@
178181
.filter(tag => tag.startsWith('v'));
179182
this.versionOptions = this.versionOptions.concat(tagOptions);
180183
},
181-
mounted() {
184+
updated: function() {
182185
if (UrlHash === '') return;
183-
const target = document.querySelector(`#${UrlHash}`);
184-
if (target != null) {
185-
target.scrollIntoView(true);
186-
}
186+
this.$nextTick(() => {
187+
const target = document.querySelector(`#${UrlHash}`);
188+
if (target != null && !this.scrolledOnce) {
189+
target.scrollIntoView(true);
190+
this.scrolledOnce = true;
191+
}
192+
});
187193
}
188194
});
189195
const extractDepthOnes = (ast) => {

0 commit comments

Comments
 (0)