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 e112bad
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 46 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(

Check failure on line 84 in packages/remark-obsidian/internal-link/internal-link.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/internal-link/internal-link.test.ts > [[Internal Link#^Abc123]]

AssertionError: expected '<p><a href="/internal-link#^abc123">I…' to equal '<p><a href="/internal-link#abc123">In…' Expected: "<p><a href="/internal-link#abc123">Internal Link > ^Abc123</a></p>" Received: "<p><a href="/internal-link#^abc123">Internal Link > ^Abc123</a></p>" ❯ packages/remark-obsidian/internal-link/internal-link.test.ts:84:10
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>');
});
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(

Check failure on line 51 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > [[Internal Links#^Abc-123]]

AssertionError: expected '<p><a href="/internal-links#^abc-123"…' to contain '<a href="/internal-links#abc-123">Int…' Expected: "<a href="/internal-links#abc-123">Internal Links > ^Abc-123</a>" Received: "<p><a href="/internal-links#^abc-123">Internal Links > ^Abc-123</a></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:51:16
'<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>');

Check failure on line 58 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > [[Internal Links#^Abc-123|Alias]]

AssertionError: expected '<p><a href="/internal-links#^abc-123"…' to contain '<a href="/internal-links#abc-123">Ali…' Expected: "<a href="/internal-links#abc-123">Alias</a>" Received: "<p><a href="/internal-links#^abc-123">Alias</a></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:58:16
});

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>');

Check failure on line 63 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > [[#Main Section]]

AssertionError: expected '<p><a href="/#main-section">Main Sect…' to contain '<a href="#main-section">Main Section<…' Expected: "<a href="#main-section">Main Section</a>" Received: "<p><a href="/#main-section">Main Section</a></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:63:16
});

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>');

Check failure on line 68 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > [[#Main Section|Alias]]

AssertionError: expected '<p><a href="/#main-section">Alias</a>…' to contain '<a href="#main-section">Alias</a>' Expected: "<a href="#main-section">Alias</a>" Received: "<p><a href="/#main-section">Alias</a></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:68:16
});

test("[[#Main Section#Sub Section]]", async () => {
const { code } = await md.render("[[#Main Section#Sub Section]]");
expect(code).toContain(

Check failure on line 73 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > [[#Main Section#Sub Section]]

AssertionError: expected '<p><a href="/#main-section#sub-sectio…' to contain '<a href="#main-section#sub-section">M…' Expected: "<a href="#main-section#sub-section">Main Section > Sub Section</a>" Received: "<p><a href="/#main-section#sub-section">Main Section > Sub Section</a></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:73:16
'<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>');

Check failure on line 80 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > [[#^Abc-123]]

AssertionError: expected '<p><a href="/#^abc-123">^Abc-123</a><…' to contain '<a href="#abc-123">^Abc-123</a>' Expected: "<a href="#abc-123">^Abc-123</a>" Received: "<p><a href="/#^abc-123">^Abc-123</a></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:80:16
});

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>');

Check failure on line 85 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > [[#^Abc-123|Alias]]

AssertionError: expected '<p><a href="/#^abc-123">Alias</a></p>' to contain '<a href="#abc-123">Alias</a>' Expected: "<a href="#abc-123">Alias</a>" Received: "<p><a href="/#^abc-123">Alias</a></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:85:16
});

// 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(

Check failure on line 221 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > ![[Internal Link#^block-id]]

AssertionError: expected '<p><object data="/internal-link#^bloc…' to contain '<p><object data="/internal-link#block…' Expected: "<p><object data="/internal-link#block-id"></object></p>" Received: "<p><object data="/internal-link#^block-id"></object></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:221:16
'<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(

Check failure on line 228 in packages/remark-obsidian/remark-obsidian.test.ts

View workflow job for this annotation

GitHub Actions / test

packages/remark-obsidian/remark-obsidian.test.ts > ![[Internal Link#^block-id|Alias]]

AssertionError: expected '<p><object data="/internal-link#^bloc…' to contain '<p><object data="/internal-link#block…' Expected: "<p><object data="/internal-link#block-id" title="Alias"></object></p>" Received: "<p><object data="/internal-link#^block-id" title="Alias"></object></p>" ❯ packages/remark-obsidian/remark-obsidian.test.ts:228:16
'<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 e112bad

Please sign in to comment.