Skip to content

Commit

Permalink
Fixup tests; configure vitest to use ts paths
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph authored and gitbutler-client committed Aug 28, 2024
1 parent 50d1293 commit 6a6685d
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 48 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"hono": "^4.5.4",
"mime": "^4.0.4",
"tailwindcss": "^3.4.7",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"vite-tsconfig-paths": "^5.0.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240729.0",
Expand Down
23 changes: 10 additions & 13 deletions packages/remark-obsidian/embed/embed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ test("![[Internal Link]]", () => {
htmlExtensions: [html()],
});

assert.equal(
serialized,
'<p><embed inline-content src="Internal Link" /></p>'
);
assert.equal(serialized, '<p><object data="/internal-link"></object></p>');
});

test("![[Internal Link.pdf]]", () => {
Expand All @@ -26,7 +23,7 @@ test("![[Internal Link.pdf]]", () => {

assert.equal(
serialized,
'<p><embed src="Internal Link.pdf" type="application/pdf" /></p>'
'<p><object data="/assets/internal-link.pdf" type="application/pdf"></object></p>'
);
});

Expand All @@ -38,7 +35,7 @@ test("![[Internal Link.pdf#width=100&height=200]]", () => {

assert.equal(
serialized,
'<p><embed src="Internal Link.pdf" type="application/pdf" width="100" height="200" /></p>'
'<p><object data="/assets/internal-link.pdf" type="application/pdf" width="100" height="200"></object></p>'
);
});

Expand All @@ -50,7 +47,7 @@ test("![[Internal Link.pdf#page=5]]", () => {

assert.equal(
serialized,
'<p><embed src="Internal Link.pdf" type="application/pdf" page="5" /></p>'
'<p><object data="/assets/internal-link.pdf" type="application/pdf" page="5"></object></p>'
);
});

Expand All @@ -62,7 +59,7 @@ test("![[Internal Link.pdf#page=5&zoom=2]]", () => {

assert.equal(
serialized,
'<p><embed src="Internal Link.pdf" type="application/pdf" page="5" zoom="2" /></p>'
'<p><object data="/assets/internal-link.pdf" type="application/pdf" page="5" zoom="2"></object></p>'
);
});

Expand All @@ -74,7 +71,7 @@ test("![[Internal Link.jpg]]", () => {

assert.equal(
serialized,
'<p><embed src="Internal Link.jpg" type="image/jpeg" /></p>'
'<p><object data="/assets/internal-link.jpg" type="image/jpeg"></object></p>'
);
});

Expand All @@ -86,7 +83,7 @@ test("![[Internal Link.mp3]]", () => {

assert.equal(
serialized,
'<p><embed src="Internal Link.mp3" type="audio/mpeg" /></p>'
'<p><object data="/assets/internal-link.mp3" type="audio/mpeg"></object></p>'
);
});

Expand All @@ -98,7 +95,7 @@ test("![[Internal Link.mp4]]", () => {

assert.equal(
serialized,
'<p><embed src="Internal Link.mp4" type="video/mp4" /></p>'
'<p><object data="/assets/internal-link.mp4" type="video/mp4"></object></p>'
);
});

Expand All @@ -110,7 +107,7 @@ test("![[Internal Link#Section]]", () => {

assert.equal(
serialized,
'<p><embed inline-content src="Internal Link#Section" /></p>'
'<p><object data="/internal-link#section"></object></p>'
);
});

Expand All @@ -122,6 +119,6 @@ test("![[Internal Link#^block-id]]", () => {

assert.equal(
serialized,
'<p><embed inline-content src="Internal Link#block-id" /></p>'
'<p><object data="/internal-link#block-id"></object></p>'
);
});
25 changes: 11 additions & 14 deletions packages/remark-obsidian/embed/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { CompileData, Handle, HtmlExtension } from "micromark-util-types";
import mime from "mime";
import { safeTpl } from "../parser-utils";
import { slugify } from "../internal-link/utils";
import { slugify } from "~/utils";

export function html(): HtmlExtension {
const enterEmbed: Handle = function () {
Expand Down Expand Up @@ -71,36 +71,33 @@ export function html(): HtmlExtension {
? `#${embed.headings.at(-1)}`
: "";

this.tag(`<object data="${embed.value}${anchor}"></object>`);
this.tag(
`<object data="/${slugify(embed.value)}${slugify(anchor)}"></object>`
);
return;
}

console.log("extension?", embed.extension);
if (embed.extension) {
embed.value = `/assets/${slugify(embed.value)}`;
}

if (embed.extension === "pdf") {
const params =
embed.pdfParams &&
Object.entries(embed.pdfParams)
.map(([key, value]: [string, unknown]) => `${key}="${value}"`)
.join(" ") + " ";
.join(" ");
this.tag(
`<object data="/assets/${embed.value}" type="application/pdf" ${
params ?? ""
}></object>`
`<object data="/assets/${slugify(
embed.value
)}" type="application/pdf"${safeTpl` ${params}`}></object>`
);
return;
}

const width = safeTpl`width="${embed.dimensions?.[0]}" `;
const width = safeTpl` width="${embed.dimensions?.[0]}" `;
const height = safeTpl`height="${embed.dimensions?.[1]}" `;

this.tag(
`<object src="/assets/${embed.value}" type="${mime.getType(
`<object data="/assets/${slugify(embed.value)}" type="${mime.getType(
embed.extension
)}" ${width}${height}/>`
)}"${width}${height}></object>`
);
};

Expand Down
16 changes: 8 additions & 8 deletions packages/remark-obsidian/internal-link/internal-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test("[[Internal Link#^Abc123]]", () => {

assert.equal(
serialized,
'<p><a href="/internal-link#^abc123">Internal Link > ^Abc123</a></p>'
'<p><a href="/internal-link#abc123">Internal Link > ^Abc123</a></p>'
);
});

Expand All @@ -93,7 +93,7 @@ test("[[Internal Link#^Abc123|Alias]]", () => {
htmlExtensions: [html()],
});

assert.equal(serialized, '<p><a href="/internal-link#^abc123">Alias</a></p>');
assert.equal(serialized, '<p><a href="/internal-link#abc123">Alias</a></p>');
});

test("[[#Main Section]]", () => {
Expand All @@ -102,7 +102,7 @@ test("[[#Main Section]]", () => {
htmlExtensions: [html()],
});

assert.equal(serialized, '<p><a href="/#main-section">Main Section</a></p>');
assert.equal(serialized, '<p><a href="#main-section">Main Section</a></p>');
});

test("[[#Main Section|Alias]]", () => {
Expand All @@ -111,7 +111,7 @@ test("[[#Main Section|Alias]]", () => {
htmlExtensions: [html()],
});

assert.equal(serialized, '<p><a href="/#main-section">Alias</a></p>');
assert.equal(serialized, '<p><a href="#main-section">Alias</a></p>');
});

test("[[#Main Section#Sub Section]]", () => {
Expand All @@ -122,7 +122,7 @@ test("[[#Main Section#Sub Section]]", () => {

assert.equal(
serialized,
'<p><a href="/#main-section#sub-section">Main Section > Sub Section</a></p>'
'<p><a href="#main-section#sub-section">Main Section > Sub Section</a></p>'
);
});

Expand All @@ -134,7 +134,7 @@ test("[[#Main Section#Sub Section|Alias]]", () => {

assert.equal(
serialized,
'<p><a href="/#main-section#sub-section">Alias</a></p>'
'<p><a href="#main-section#sub-section">Alias</a></p>'
);
});

Expand All @@ -144,7 +144,7 @@ test("[[#^Abc123]]", () => {
htmlExtensions: [html()],
});

assert.equal(serialized, '<p><a href="/#^abc123">^Abc123</a></p>');
assert.equal(serialized, '<p><a href="#abc123">^Abc123</a></p>');
});

test("[[#^Abc123|Alias]]", () => {
Expand All @@ -153,5 +153,5 @@ test("[[#^Abc123|Alias]]", () => {
htmlExtensions: [html()],
});

assert.equal(serialized, '<p><a href="/#^abc123">Alias</a></p>');
assert.equal(serialized, '<p><a href="#abc123">Alias</a></p>');
});
5 changes: 3 additions & 2 deletions packages/remark-obsidian/internal-link/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { slugify } from "~/utils";
import type { InternalLinkNode } from "../types";

export const slugify = (s: string) => s.toLowerCase().replace(/\s+/g, "-");
export { slugify } from "~/utils";

export const displayName = (internalLink: InternalLinkNode) => {
let displayName = internalLink.alias ?? internalLink.value ?? "";
Expand Down Expand Up @@ -29,6 +30,6 @@ export const href = (
return slugify(
internalLink.value
? `/${internalLink.value}${blockOrHeadings}`
: `/${blockOrHeadings}`
: `${blockOrHeadings}`
);
};
18 changes: 9 additions & 9 deletions packages/remark-obsidian/remark-obsidian.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,40 @@ test("[[Internal Links#Main Section#Sub Section|Alias]]", async () => {
test("[[Internal Links#^Abc-123]]", async () => {
const { code } = await md.render("[[Internal Links#^Abc-123]]");
expect(code).toContain(
'<a href="/internal-links#^abc-123">Internal Links > ^Abc-123</a>'
'<a href="/internal-links#abc-123">Internal Links > ^Abc-123</a>'
);
});

test("[[Internal Links#^Abc-123|Alias]]", async () => {
const { code } = await md.render("[[Internal Links#^Abc-123|Alias]]");
expect(code).toContain('<a href="/internal-links#^abc-123">Alias</a>');
expect(code).toContain('<a href="/internal-links#abc-123">Alias</a>');
});

test("[[#Main Section]]", async () => {
const { code } = await md.render("[[#Main Section]]");
expect(code).toContain('<a href="/#main-section">Main Section</a>');
expect(code).toContain('<a href="#main-section">Main Section</a>');
});

test("[[#Main Section|Alias]]", async () => {
const { code } = await md.render("[[#Main Section|Alias]]");
expect(code).toContain('<a href="/#main-section">Alias</a>');
expect(code).toContain('<a href="#main-section">Alias</a>');
});

test("[[#Main Section#Sub Section]]", async () => {
const { code } = await md.render("[[#Main Section#Sub Section]]");
expect(code).toContain(
'<a href="/#main-section#sub-section">Main Section > Sub Section</a>'
'<a href="#main-section#sub-section">Main Section > Sub Section</a>'
);
});

test("[[#^Abc-123]]", async () => {
const { code } = await md.render("[[#^Abc-123]]");
expect(code).toContain('<a href="/#^abc-123">^Abc-123</a>');
expect(code).toContain('<a href="#abc-123">^Abc-123</a>');
});

test("[[#^Abc-123|Alias]]", async () => {
const { code } = await md.render("[[#^Abc-123|Alias]]");
expect(code).toContain('<a href="/#^abc-123">Alias</a>');
expect(code).toContain('<a href="#abc-123">Alias</a>');
});

// New embed tests
Expand Down Expand Up @@ -219,13 +219,13 @@ test("![[Internal Link#Section|Alias]]", async () => {
test("![[Internal Link#^block-id]]", async () => {
const { code } = await md.render("![[Internal Link#^block-id]]");
expect(code).toContain(
'<p><object data="/internal-link#^block-id"></object></p>'
'<p><object data="/internal-link#block-id"></object></p>'
);
});

test("![[Internal Link#^block-id|Alias]]", async () => {
const { code } = await md.render("![[Internal Link#^block-id|Alias]]");
expect(code).toContain(
'<p><object data="/internal-link#^block-id" title="Alias"></object></p>'
'<p><object data="/internal-link#block-id" title="Alias"></object></p>'
);
});
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const slugify = (s: string) => s.toLowerCase().replace(/\s+/g, "-");
export const slugify = (s: string) =>
s
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^a-z0-9-#\/\.]/g, "");

export const isValidSha256 = (hashString: string) =>
/^[a-fA-F0-9]{64}$/.test(hashString);
9 changes: 9 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
plugins: [tsconfigPaths()],
test: {
// Add any additional test configurations here if needed
},
});

0 comments on commit 6a6685d

Please sign in to comment.