fix: preserve emoji characters in tag slugification

This commit is contained in:
saberzero1 2026-04-07 16:09:53 +02:00
parent 0e9935791b
commit 85d58e6e6a
No known key found for this signature in database
6 changed files with 56 additions and 14 deletions

View file

@ -1,13 +1,9 @@
import { createRequire } from 'module';
import { classNames } from '@quartz-community/utils/lang';
import { joinSegments, simplifySlug as simplifySlug$1 } from '@quartz-community/utils';
import { jsxs, jsx, Fragment } from 'preact/jsx-runtime';
createRequire(import.meta.url);
// src/util/lang.ts
function classNames(...classes) {
return classes.filter(Boolean).join(" ");
}
function simplifySlug(fp) {
return simplifySlug$1(fp);
}

File diff suppressed because one or more lines are too long

8
dist/index.js vendored
View file

@ -1,4 +1,5 @@
import { createRequire } from 'module';
import { classNames } from '@quartz-community/utils/lang';
import { joinSegments, simplifySlug as simplifySlug$1 } from '@quartz-community/utils';
import { jsxs, jsx, Fragment } from 'preact/jsx-runtime';
@ -10289,7 +10290,7 @@ function coerceToArray(input) {
}
function slugTag(tag) {
return tag.split("/").map(
(segment) => segment.replace(/\s+/g, "-").replace(/[^\w\p{L}\p{M}\p{N}/-]/gu, "").toLowerCase()
(segment) => segment.replace(/\s+/g, "-").replace(/[^\w\p{L}\p{M}\p{N}\p{Extended_Pictographic}/-]/gu, "").toLowerCase()
).join("/");
}
function getFileExtension(fp) {
@ -10453,11 +10454,6 @@ var NoteProperties = (userOpts) => {
}
};
};
// src/util/lang.ts
function classNames(...classes) {
return classes.filter(Boolean).join(" ");
}
function simplifySlug(fp) {
return simplifySlug$1(fp);
}

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -46,7 +46,7 @@ function slugTag(tag: string): string {
.map((segment) =>
segment
.replace(/\s+/g, "-")
.replace(/[^\w\p{L}\p{M}\p{N}/-]/gu, "")
.replace(/[^\w\p{L}\p{M}\p{N}\p{Extended_Pictographic}/-]/gu, "")
.toLowerCase(),
)
.join("/");

50
test/slugTag.test.ts Normal file
View file

@ -0,0 +1,50 @@
import { describe, it, expect } from "vitest";
import { VFile } from "vfile";
import { NoteProperties } from "../src/transformer";
import type { BuildCtx } from "@quartz-community/types";
const runWithTag = (tag: string): string[] => {
const plugin = NoteProperties({});
const ctx = { allSlugs: [] } as unknown as BuildCtx;
const plugins = plugin.markdownPlugins!(ctx);
const transformerFactory = plugins[1] as () => (tree: unknown, file: VFile) => void;
const transformer = transformerFactory();
const escapedTag = tag.replace(/"/g, '\\"');
const markdown = `---\ntags:\n - "${escapedTag}"\n---\ncontent`;
const file = new VFile({
value: new TextEncoder().encode(markdown),
path: "note.md",
});
file.data = {};
transformer(null, file);
return (file.data.frontmatter as { tags: string[] }).tags;
};
describe("slugTag", () => {
it("preserves emoji in tags", () => {
expect(runWithTag("0🌲")).toEqual(["0🌲"]);
});
it("converts spaces to hyphens", () => {
expect(runWithTag("my tag")).toEqual(["my-tag"]);
});
it("strips special characters", () => {
expect(runWithTag("tag?#&")).toEqual(["tag"]);
});
it("keeps nested tag separators", () => {
expect(runWithTag("parent/child")).toEqual(["parent/child"]);
});
it("lowercases tags", () => {
expect(runWithTag("MyTag")).toEqual(["mytag"]);
});
it("preserves multiple emojis", () => {
expect(runWithTag("🎵music🎵")).toEqual(["🎵music🎵"]);
});
});