Skip to content

Commit

Permalink
[#1005, remark-wiki-link]: Parse note embeds as regular wiki links (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
olayway authored Aug 14, 2023
1 parent 84cc6cf commit 6418dbb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-worms-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@portaljs/remark-wiki-link': minor
---

Parse note embeds as regular wiki links (until we add proper support for note embeds).
31 changes: 21 additions & 10 deletions packages/remark-wiki-link/src/lib/fromMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const defaultWikiLinkResolver = (target: string) => {

export interface FromMarkdownOptions {
pathFormat?:
| "raw" // default; use for regular relative or absolute paths
| "obsidian-absolute" // use for Obsidian-style absolute paths (with no leading slash)
| "obsidian-short"; // use for Obsidian-style shortened paths (shortest path possible)
| "raw" // default; use for regular relative or absolute paths
| "obsidian-absolute" // use for Obsidian-style absolute paths (with no leading slash)
| "obsidian-short"; // use for Obsidian-style shortened paths (shortest path possible)
permalinks?: string[]; // list of permalinks to match possible permalinks of a wiki link against
wikiLinkResolver?: (name: string) => string[]; // function to resolve wiki links to an array of possible permalinks
newClassName?: string; // class name to add to links that don't have a matching permalink
Expand Down Expand Up @@ -125,13 +125,24 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
if (isEmbed) {
const [isSupportedFormat, format] = isSupportedFileFormat(target);
if (!isSupportedFormat) {
wikiLink.data.hName = "p";
wikiLink.data.hChildren = [
{
type: "text",
value: `![[${target}]]`,
},
];
// Temporarily render note transclusion as a regular wiki link
if (!format) {
wikiLink.data.hName = "a";
wikiLink.data.hProperties = {
className: classNames + " " + "transclusion",
href: hrefTemplate(link) + headingId,
};
wikiLink.data.hChildren = [{ type: "text", value: displayName }];

} else {
wikiLink.data.hName = "p";
wikiLink.data.hChildren = [
{
type: "text",
value: `![[${target}]]`,
},
];
}
} else if (format === "pdf") {
wikiLink.data.hName = "iframe";
wikiLink.data.hProperties = {
Expand Down
17 changes: 13 additions & 4 deletions packages/remark-wiki-link/src/lib/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const defaultWikiLinkResolver = (target: string) => {

export interface HtmlOptions {
pathFormat?:
| "raw" // default; use for regular relative or absolute paths
| "obsidian-absolute" // use for Obsidian-style absolute paths (with no leading slash)
| "obsidian-short"; // use for Obsidian-style shortened paths (shortest path possible)
| "raw" // default; use for regular relative or absolute paths
| "obsidian-absolute" // use for Obsidian-style absolute paths (with no leading slash)
| "obsidian-short"; // use for Obsidian-style shortened paths (shortest path possible)
permalinks?: string[]; // list of permalinks to match possible permalinks of a wiki link against
wikiLinkResolver?: (name: string) => string[]; // function to resolve wiki links to an array of possible permalinks
newClassName?: string; // class name to add to links that don't have a matching permalink
Expand Down Expand Up @@ -108,7 +108,16 @@ function html(opts: HtmlOptions = {}) {
if (isEmbed) {
const [isSupportedFormat, format] = isSupportedFileFormat(target);
if (!isSupportedFormat) {
this.raw(`![[${target}]]`);
// Temporarily render note transclusion as a regular wiki link
if (!format) {
this.tag(
`<a href="${hrefTemplate(link + headingId)}" class="${classNames} transclusion">`
);
this.raw(displayName);
this.tag("</a>");
} else {
this.raw(`![[${target}]]`);
}
} else if (format === "pdf") {
this.tag(
`<iframe width="100%" src="${hrefTemplate(
Expand Down
12 changes: 12 additions & 0 deletions packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,16 @@ describe("micromark-extension-wiki-link", () => {
expect(serialized).toBe('<p><a href="/" class="internal">/index</a></p>');
});
});

describe("transclusions", () => {
test("parsers a transclusion as a regular wiki link", () => {
const serialized = micromark("![[Some Page]]", "ascii", {
extensions: [syntax()],
htmlExtensions: [html() as any], // TODO type fix
});
expect(serialized).toBe(
'<p><a href="Some Page" class="internal new transclusion">Some Page</a></p>'
);
});
});
});
24 changes: 24 additions & 0 deletions packages/remark-wiki-link/test/remarkWikiLink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,28 @@ describe("remark-wiki-link", () => {
});
});
});

describe("transclusions", () => {
test("replaces a transclusion with a regular wiki link", () => {
const processor = unified().use(markdown).use(wikiLinkPlugin);

let ast = processor.parse("![[Some Page]]");
ast = processor.runSync(ast);

expect(select("wikiLink", ast)).not.toEqual(null);

visit(ast, "wikiLink", (node: Node) => {
expect(node.data?.isEmbed).toEqual(true);
expect(node.data?.exists).toEqual(false);
expect(node.data?.permalink).toEqual("Some Page");
expect(node.data?.alias).toEqual(null);
expect(node.data?.hName).toEqual("a");
expect((node.data?.hProperties as any).className).toEqual(
"internal new transclusion"
);
expect((node.data?.hProperties as any).href).toEqual("Some Page");
expect((node.data?.hChildren as any)[0].value).toEqual("Some Page");
});
});
});
});

0 comments on commit 6418dbb

Please sign in to comment.