Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(publish): Table of Contents is missing user tags, inline code, dashes and underlines #2465

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/engine-server/src/markdown/remark/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {
FrontmatterContent,
Heading,
Image,
InlineCode,
Link,
List,
ListItem,
Expand Down Expand Up @@ -786,6 +787,12 @@ export class AnchorUtils {
case DendronASTTypes.HASHTAG:
headerText.push((node as HashTag).value);
break;
case DendronASTTypes.USERTAG:
headerText.push((node as UserTag).value);
break;
case DendronASTTypes.INLINE_CODE:
headerText.push((node as InlineCode).value);
break;
default:
/* nothing */
}
Expand Down
1 change: 1 addition & 0 deletions packages/engine-server/src/markdown/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export enum DendronASTTypes {
IMAGE = "image",
FRONTMATTER = "yaml",
LINK = "link",
INLINE_CODE = "inlineCode",
FOOTNOTE_DEFINITION = "footnoteDefinition",
FOOTNOTE_REFERENCE = "footnoteReference",
}
Expand Down
41 changes: 41 additions & 0 deletions packages/engine-test-utils/src/presets/engine-server/getAnchors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NoteUtils } from "@dendronhq/common-all";
import {
TestPresetEntryV4,
NOTE_PRESETS_V4,
NoteTestUtilsV4,
} from "@dendronhq/common-test-utils";
import _ from "lodash";

Expand Down Expand Up @@ -68,6 +69,46 @@ const NOTES = {
},
}
),
/** Test for cases where there are headers that have markdown in them, like
* code blocks or wikilinks. Make sure the correct text is extracted, which is
* important because the text will be displayed to the user in the ToC when
* published.
*/
HEADERS_WITH_MARKDOWN: new TestPresetEntryV4(
async ({ vaults, engine }) => {
const note = NoteUtils.getNoteByFnameFromEngine({
fname: "test",
engine,
vault: vaults[0],
});

const { data } = await engine.getAnchors({
note: note!,
});
return [
{
actual: Object.entries(data!).map(([_key, anchor]) => anchor.text),
expected: ["root", "alias", "code", "@user", "#tag"],
},
];
},
{
preSetupHook: async ({ vaults, wsRoot }) => {
await NoteTestUtilsV4.createNote({
fname: "test",
vault: vaults[0],
wsRoot,
body: [
"## [[root]]",
"## [[alias|root]]",
"## `code`",
"## @user",
"## #tag",
].join("\n"),
});
},
}
),
};
export const ENGINE_GET_ANCHORS_PRESETS = {
NOTES,
Expand Down
5 changes: 4 additions & 1 deletion packages/nextjs-template/components/DendronTOC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const DendronTOC = ({
<Link
key={key}
href={`#${key}`}
title={unslug(entry?.text ?? entry?.value)}
// `anchor.text` contains clean, user displayable text for
// headings. It should always exist for exported notes, but we
// have this fallback just in case.
title={entry?.text ?? unslug(entry?.value)}
/>
) : (
<></>
Expand Down
25 changes: 24 additions & 1 deletion test-workspace/vault/dendron.links.heading-anchors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: FSi3bKWQeQXYTjE1PoTB0
title: Heading Anchors
desc: ''
updated: 1633004714929
updated: 1645515843913
created: 1632988925249
---

Expand Down Expand Up @@ -38,3 +38,26 @@ created: 1632988925249

## heading-2
* content-2


## Heading anchors tests

### 1 start `code` end

The heading above should have the word `code` in the middle

### 2 start - end

The heading above should have a dash in the middle

### 3 start _ end

The heading above should have an underscore in the middle

### 4 start #test end

The heading above should have a hashtag in the middle.

### 5 start @example.username end

The heading above should have a user tag in the middle.