From 5d7e0a0db279a787efaff1154daf522b56a2fb07 Mon Sep 17 00:00:00 2001 From: Tim Green Date: Tue, 22 Nov 2022 23:29:50 +1100 Subject: [PATCH] feat(parser): support global tags in frontmatter. --- packages/core/src/model.ts | 1 + packages/core/src/parser.ts | 11 +++++-- packages/core/test/fixtures/parser/tags.md | 21 +++++++++++++ packages/core/test/fixtures/parser/tags.yml | 35 +++++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 packages/core/test/fixtures/parser/tags.md create mode 100644 packages/core/test/fixtures/parser/tags.yml diff --git a/packages/core/src/model.ts b/packages/core/src/model.ts index 28c7342..9d3c367 100644 --- a/packages/core/src/model.ts +++ b/packages/core/src/model.ts @@ -13,6 +13,7 @@ export interface NoteModel { export interface IFrontmatterConfig { deckName?: string; models?: Record; + tags?: string[]; } export interface IDeck { diff --git a/packages/core/src/parser.ts b/packages/core/src/parser.ts index fd39701..379c9a5 100644 --- a/packages/core/src/parser.ts +++ b/packages/core/src/parser.ts @@ -14,12 +14,12 @@ import YAML from "yaml"; import { rehypeAnkiMathjax } from "./lib/rehype-anki-mathjax"; import { remarkTagLink, TagLink } from "./lib/remark-tag-link"; -import { INotePosition } from "./model"; import { BASIC_MODEL, IDeck, IFrontmatterConfig, INote, + INotePosition, MediaInfo, MediaName, } from "./model"; @@ -169,6 +169,7 @@ export class Parser { private async toNote( nodes: Array, + frontmatterConfig?: IFrontmatterConfig, dirpath?: string, ): Promise { const heading = nodes[0] as Heading; @@ -182,7 +183,9 @@ export class Parser { } const cardTag = headingTags[0]!!; - const tags = headingTags.slice(1); + const localTags = headingTags.slice(1); + const globalTags = frontmatterConfig?.tags || []; + const tags = Array.from(new Set(localTags.concat(globalTags))); if ( !Parser.isBasicCardTag(cardTag) && !Parser.extractCustomModelName(cardTag) @@ -301,7 +304,9 @@ export class Parser { ); const notes = ( - await Promise.all(noteMds.map((it) => this.toNote(it, dirpath))) + await Promise.all( + noteMds.map((it) => this.toNote(it, frontmatterConfig, dirpath)), + ) ).filter((n) => !!n) as Array; return { diff --git a/packages/core/test/fixtures/parser/tags.md b/packages/core/test/fixtures/parser/tags.md new file mode 100644 index 0000000..2d1282f --- /dev/null +++ b/packages/core/test/fixtures/parser/tags.md @@ -0,0 +1,21 @@ +--- +tags: + - global-tag-1 + - global-tag-2 +--- + +# Ignored + +ignored + +# Note 1 #card #local-tag-1 + +n1 text + +## n1 h2 + +n1 text + +# #card Note 2 #local-tag-2 + +## n2 h2 diff --git a/packages/core/test/fixtures/parser/tags.yml b/packages/core/test/fixtures/parser/tags.yml new file mode 100644 index 0000000..3f5e62d --- /dev/null +++ b/packages/core/test/fixtures/parser/tags.yml @@ -0,0 +1,35 @@ +deckName: Default +frontmatterConfig: + tags: + - global-tag-1 + - global-tag-2 +notes: + - modelName: Basic + inOrderFields: [Front, Back] + values: + Front: "Note 1 \n " + Back: |- +

n1 text

+

n1 h2

+

n1 text

+ tags: + - local-tag-1 + - global-tag-1 + - global-tag-2 + medias: {} + position: + startLine: 11 + endLine: 17 + - modelName: Basic + inOrderFields: [Front, Back] + values: + Front: " Note 2 " + Back:

n2 h2

+ tags: + - local-tag-2 + - global-tag-1 + - global-tag-2 + medias: {} + position: + startLine: 19 + endLine: 21