From 90c4a1a09c7832eabdbab5581a64065423e4b69a Mon Sep 17 00:00:00 2001 From: Ola Rubaj <52197250+olayway@users.noreply.github.com> Date: Thu, 10 Aug 2023 21:09:52 +0200 Subject: [PATCH] [#31,utils/parseFile][s]: obsidian-style tags list support --- .changeset/flat-bikes-pull.md | 5 +++++ src/utils/parseFile.spec.ts | 9 ++++++--- src/utils/parseFile.ts | 5 +++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .changeset/flat-bikes-pull.md diff --git a/.changeset/flat-bikes-pull.md b/.changeset/flat-bikes-pull.md new file mode 100644 index 0000000..5f2e315 --- /dev/null +++ b/.changeset/flat-bikes-pull.md @@ -0,0 +1,5 @@ +--- +"mddb": patch +--- + +Add support for Obsidian style tags list in frontmatter (e.g. `tags: a,b,c`). diff --git a/src/utils/parseFile.spec.ts b/src/utils/parseFile.spec.ts index 5541afc..28de0e3 100644 --- a/src/utils/parseFile.spec.ts +++ b/src/utils/parseFile.spec.ts @@ -2,7 +2,8 @@ import { parseFile } from "./parseFile"; const source = `--- title: Hello World -tags: [hello, world] +authors: [John Doe, Jane Doe] +tags: a, b, c --- # Hello World [[Some Link]] @@ -15,7 +16,8 @@ describe("parseFile", () => { it("should parse a file returning metadata and wiki links", () => { const expectedMetadata = { title: "Hello World", - tags: ["hello", "world"], + authors: ["John Doe", "Jane Doe"], + tags: ["a", "b", "c"], }; const expectedLinks = [ { linkType: "normal", linkSrc: "Some Link" }, @@ -31,7 +33,8 @@ describe("parseFile", () => { it("should parse a file returning metadata and wiki links with Obsidian-style shortest paths", () => { const expectedMetadata = { title: "Hello World", - tags: ["hello", "world"], + authors: ["John Doe", "Jane Doe"], + tags: ["a", "b", "c"], }; const expectedLinks = [ { linkType: "normal", linkSrc: "/some/folder/Some Link" }, diff --git a/src/utils/parseFile.ts b/src/utils/parseFile.ts index bb47396..6a2d21a 100644 --- a/src/utils/parseFile.ts +++ b/src/utils/parseFile.ts @@ -5,6 +5,11 @@ export function parseFile(source: string, options?: { permalinks?: string[] }) { // Metadata const { data: metadata } = matter(source); + // Obsidian style tags i.e. tags: tag1, tag2, tag3 + if (metadata.tags && typeof metadata.tags === "string") { + metadata.tags = metadata.tags.split(",").map((tag: string) => tag.trim()); + } + // Links const links = extractWikiLinks(source, { permalinks: options?.permalinks });