Skip to content

Commit fea526c

Browse files
committed
feat: add block-id preview
1 parent 005644c commit fea526c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

zola/static/js/preview.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@ function showPreview(mouseEvent, link) {
1212
.then((res) => res.text())
1313
.then((html) => {
1414
let doc = new DOMParser().parseFromString(html, "text/html");
15-
previewDiv.innerHTML =
16-
doc.querySelector(".docs-content").innerHTML;
15+
let docContent = doc.querySelector(".docs-content");
16+
previewDiv.innerHTML = docContent.innerHTML;
17+
18+
let blockId = link.href.match(/(?<=#).{6}/);
19+
20+
if (blockId != null) {
21+
blockId = [blockId];
22+
const blockContent = [
23+
...docContent.querySelectorAll(
24+
"p, li, h1, h2, h3, h4, h5, h6"
25+
),
26+
].findLast((e) => {
27+
return e.textContent.includes(`^${blockId}`);
28+
});
29+
30+
previewDiv.innerHTML = blockContent.outerHTML;
31+
}
1732
cache.set(link.href, previewDiv.innerHTML);
1833
initPreview(`.${getPreviewUniqueClass(previewDiv)} a`);
1934
});
@@ -46,7 +61,7 @@ function showPreview(mouseEvent, link) {
4661
function getPreviewPosition(clientX, clientY) {
4762
const offset = 10,
4863
previewDivWidth = 400,
49-
previewDivHeight = 300;
64+
previewDivHeight = 100;
5065
const boundaryX = window.innerWidth,
5166
boundaryY = window.innerHeight;
5267
const overflowRight = clientX + offset + previewDivWidth > boundaryX;

0 commit comments

Comments
 (0)