Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions packages/remark-wiki-link/test/isSupportedFileFormat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ describe("isSupportedFileFormat", () => {
const filePath = "image.xyz";
expect(isSupportedFileFormat(filePath)).toStrictEqual([false, "xyz"]);
});

test("should return [true, <extension>] for a path with supported file extension", () => {
const filePath = "image.csv";
expect(isSupportedFileFormat(filePath)).toStrictEqual([false, "csv"]);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already covered by the test with the same title above.

Suggested change
test("should return [true, <extension>] for a path with supported file extension", () => {
const filePath = "image.csv";
expect(isSupportedFileFormat(filePath)).toStrictEqual([false, "csv"]);
});

});
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(
'<p><a href="data.csv" class="internal new" download="data.csv">data.csv</a></p>'
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it a regular a tag and not FlatUiTable? Isn't it the aim of this PR?

});

test("parses a CSV file embed of unsupported file format", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test("parses a CSV file embed of unsupported file format", () => {
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(
'<p><a href="data.csv" class="internal" download="data.csv">data.csv</a></p>'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, why does it include a regular a tag?

);
});

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(
'<p><a href="data.csv" class="internal new" download="data.csv">My CSV File</a></p>'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

If anything, alias should be used as table title. But I think you can just ignore it for now.

);
});
// 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