Skip to content

Commit

Permalink
fix: block anchor in list with single top level element (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaan Genç committed Aug 31, 2021
1 parent b7bade9 commit 1ce3a21
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/engine-server/src/markdown/remark/noteRefsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,11 @@ function findBlockAnchor({

if (_.isUndefined(foundIndex)) return null;
if (!_.isEmpty(foundAncestors)) {
if (foundAncestors[0].ancestor.children.length === 1) {
if (
foundAncestors[0].ancestor.children.length === 1 &&
foundAncestors[0].ancestor.children[0].type ===
DendronASTTypes.BLOCK_ANCHOR
) {
// If located by itself after a block, then the block anchor refers to the previous block
return { type: "block", index: foundIndex - 1 };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,64 @@ VFile {
}
`;

exports[`noteRefV2 with block anchors compile "HTML: a single top level list" 1`] = `
VFile {
"contents": "<h1 id=\\"foo\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#foo\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Foo</h1>
<h1 id=\\"foo-bar\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#foo-bar\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Foo Bar</h1>
<p></p><p></p><div class=\\"portal-container\\">
<div class=\\"portal-head\\">
<div class=\\"portal-backlink\\">
<div class=\\"portal-title\\">From <span class=\\"portal-text-title\\">Ch1</span></div>
<a href=\\"/undefined/foo.ch1.html\\" class=\\"portal-arrow\\">Go to text <span class=\\"right-arrow\\">→</span></a>
</div>
</div>
<div id=\\"portal-parent-anchor\\" class=\\"portal-parent\\" markdown=\\"1\\">
<div class=\\"portal-parent-fader-top\\"></div>
<div class=\\"portal-parent-fader-bottom\\"></div><ul>
<li><a aria-hidden=\\"true\\" class=\\"block-anchor anchor-heading\\" id=\\"^block-anchor\\" href=\\"#^block-anchor\\"><svg viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Reprehenderit doloribus. </li>
</ul>
</div></div><p></p><p></p>
<hr>
<h2 id=\\"children\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#children\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Children</h2>
<ol>
<li><a href=\\"/undefined/foo.ch1.html\\">Ch1</a></li>
</ol>",
"cwd": "<PROJECT_ROOT>",
"data": Object {},
"history": Array [],
"messages": Array [],
}
`;

exports[`noteRefV2 with block anchors compile "HTML: a single top level list" 2`] = `
VFile {
"contents": "<h1 id=\\"foo\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#foo\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Foo</h1>
<h1 id=\\"foo-bar\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#foo-bar\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Foo Bar</h1>
<p></p><p></p><div class=\\"portal-container\\">
<div class=\\"portal-head\\">
<div class=\\"portal-backlink\\">
<div class=\\"portal-title\\">From <span class=\\"portal-text-title\\">Ch1</span></div>
<a href=\\"/undefined/foo.ch1.html\\" class=\\"portal-arrow\\">Go to text <span class=\\"right-arrow\\">→</span></a>
</div>
</div>
<div id=\\"portal-parent-anchor\\" class=\\"portal-parent\\" markdown=\\"1\\">
<div class=\\"portal-parent-fader-top\\"></div>
<div class=\\"portal-parent-fader-bottom\\"></div><ul>
<li><a aria-hidden=\\"true\\" class=\\"block-anchor anchor-heading\\" id=\\"^block-anchor\\" href=\\"#^block-anchor\\"><svg viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Reprehenderit doloribus. </li>
</ul>
</div></div><p></p><p></p>
<hr>
<h2 id=\\"children\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#children\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Children</h2>
<ol>
<li><a href=\\"/undefined/foo.ch1.html\\">Ch1</a></li>
</ol>",
"cwd": "<PROJECT_ROOT>",
"data": Object {},
"history": Array [],
"messages": Array [],
}
`;

exports[`noteRefV2 with block anchors compile "HTML: after a paragraph block" 1`] = `
VFile {
"contents": "<h1 id=\\"foo\\"><a aria-hidden=\\"true\\" class=\\"anchor-heading\\" href=\\"#foo\\"><svg aria-hidden=\\"true\\" viewBox=\\"0 0 16 16\\"><use xlink:href=\\"#svg-link\\"></use></svg></a>Foo</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,49 @@ describe("noteRefV2", () => {
},
});

const SINGLE_TOP_LEVEL_LIST = createProcTests({
name: "a single top level list",
setupFunc: async (opts) => {
const { engine, vaults } = opts;
return processTextV2({
text: "# Foo Bar\n![[foo.ch1#^block-anchor]]",
dest: opts.extra.dest,
engine,
vault: vaults[0],
fname: "foo",
});
},
preSetupHook: async (opts) => {
await ENGINE_HOOKS.setupBasic(opts);
await modifyNote(opts, "foo.ch1", (note: NoteProps) => {
const txt = [
"Ut temporibus quidem nihil corporis",
"",
"* Ullam optio est quia.",
" * Laborum libero quia ducimus.",
" * Reprehenderit doloribus. ^block-anchor",
" * Iure neque alias dolorem.",
];
note.body = txt.join("\n");
return note;
});
},
verifyFuncDict: {
[DendronASTDest.HTML]: async ({ extra }) => {
const { resp } = extra;
await checkVFile(resp, "Reprehenderit doloribus.");
await checkNotInVFile(
resp,
"Ut temporibus quidem nihil corporis",
"Ullam optio est quia.",
"Laborum libero quia ducimus.",
"Iure neque alias dolorem.",
"Sint minus fuga omnis non."
);
},
},
});

/** When the anchor is immediately after the last list element, it only references the last element. */
const AFTER_LIST = createProcTests({
name: "immediately after last list element",
Expand Down Expand Up @@ -2053,6 +2096,7 @@ describe("noteRefV2", () => {
...PARAGRAPH_TO_LIST,
...HEADER_TO_BLOCK_ANCHOR,
...BLOCK_ANCHOR_TO_HEADER,
...SINGLE_TOP_LEVEL_LIST,
],
});
});
Expand Down
19 changes: 19 additions & 0 deletions test-workspace/vault/scratch.2021.08.31.004036.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
id: cu0BgWKqOikg2khGwrD1q
title: '004036'
desc: ''
updated: 1630385002240
created: 1630384838453
---

## temporibus

Ut temporibus quidem quis corrupti nihil corporis

- Ad libero molestias voluptas quo cupiditate ut quisquam
- Id quibusdam debitis facilis illum et ratione minima. ^facilis
- In quibusdam quia enim explicabo est quibusdam molestiae. ^DHWVfFIaPjYv

![[#^facilis]]

![[#^DHWVfFIaPjYv]]

0 comments on commit 1ce3a21

Please sign in to comment.