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

CSV embed using wiki-link transclusion format #1287

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions packages/remark-wiki-link/src/lib/fromMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
width: '100%',
src: `${hrefTemplate(link)}#toolbar=0`,
};
} else if (format === 'csv') {
// CSV support
wikiLink.data.hName = 'FlatUiTable';
wikiLink.data.hProperties = {
data: { url: hrefTemplate(link) },
};
} else {
const hasDimensions = alias && /^\d+(x\d+)?$/.test(alias);
// Take the target as alt text except if alt name was provided [[target|alt text]]
Expand Down
5 changes: 5 additions & 0 deletions packages/remark-wiki-link/src/lib/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ function html(opts: HtmlOptions = {}) {
link
)}#toolbar=0" class="${classNames}" />`
);
} else if (format === 'csv') {
// CSV support
this.tag(
`<FlatUiTable data={{ url: "${hrefTemplate(link)}" }} />`
);
} else {
const hasDimensions = alias && /^\d+(x\d+)?$/.test(alias);
// Take the target as alt text except if alt name was provided [[target|alt text]]
Expand Down
1 change: 1 addition & 0 deletions packages/remark-wiki-link/src/lib/isSupportedFileFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const supportedFileFormats = [
"avif",
"ico",
"pdf",
"csv",
];

export const isSupportedFileFormat = (filePath: string): [boolean, string] => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,44 @@ describe("micromark-extension-wiki-link", () => {
'<p><img src="My Image.jpg" alt="My Image.jpg" class="internal" width="200" height="200" /></p>'
);
});

// CSV tests
test("parses a CSV file embed of supported file format", () => {
const serialized = micromark("![[data.csv]]", "ascii", {
extensions: [syntax()],
htmlExtensions: [html() as any], // TODO type fix
});
expect(serialized).toBe(
'<FlatUiTable data={{ url: "data.csv" }} />'
);
});

test("leaves a CSV file embed of unsupported file format as plain text", () => {
const serialized = micromark("![[data.xyz]]", "ascii", {
extensions: [syntax()],
htmlExtensions: [html() as any], // TODO type fix
});
expect(serialized).toBe('<p>![[data.xyz]]</p>');
});

test("parses a CSV file embed with a matching permalink", () => {
const serialized = micromark("![[data.csv]]", "ascii", {
extensions: [syntax()],
htmlExtensions: [html({ permalinks: ["data.csv"] }) as any], // TODO type fix
});
expect(serialized).toBe(
'<FlatUiTable data={{ url: "data.csv" }} />'
);
});
// Ignore the table alias test
// test("parses a CSV file embed with an alias", () => {
// const serialized = micromark("![[data.csv|My CSV File]]", "ascii", {
// extensions: [syntax()],
// htmlExtensions: [html() as any], // TODO type fix
// });
// expect(serialized).toBe(
// '<FlatUiTable data={{ url: "data.csv" }} />'
// );
// });
// TODO: Fix alt attribute
test("Can identify the dimensions of the image if exists", () => {
const serialized = micromark("![[My Image.jpg|200x200]]", "ascii", {
Expand Down
17 changes: 17 additions & 0 deletions packages/remark-wiki-link/test/remarkWikiLink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,23 @@ describe("remark-wiki-link", () => {
);
});
});

test("parses a CSV embed", () => {
const processor = unified().use(markdown).use(wikiLinkPlugin);

let ast = processor.parse("![[My Data.csv]]");
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?.target).toEqual("My Data.csv");
expect(node.data?.permalink).toEqual("My Data.csv");
expect(node.data?.hName).toEqual("FlatUiTable");
expect((node.data?.hProperties as any).data).toEqual({ url: "My Data.csv" });
});
});
});

describe("Links with special characters", () => {
Expand Down