Skip to content

Commit

Permalink
feat(parser): support global tags in frontmatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
timgreen committed Nov 22, 2022
1 parent edd5f15 commit 5d7e0a0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface NoteModel {
export interface IFrontmatterConfig {
deckName?: string;
models?: Record<ModelName, NoteModel>;
tags?: string[];
}

export interface IDeck {
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -169,6 +169,7 @@ export class Parser {

private async toNote(
nodes: Array<Content>,
frontmatterConfig?: IFrontmatterConfig,
dirpath?: string,
): Promise<INote | undefined> {
const heading = nodes[0] as Heading;
Expand All @@ -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)
Expand Down Expand Up @@ -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<INote>;

return {
Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/fixtures/parser/tags.md
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions packages/core/test/fixtures/parser/tags.yml
Original file line number Diff line number Diff line change
@@ -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: |-
<p>n1 text</p>
<h2>n1 h2</h2>
<p>n1 text</p>
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: <h2>n2 h2</h2>
tags:
- local-tag-2
- global-tag-1
- global-tag-2
medias: {}
position:
startLine: 19
endLine: 21

0 comments on commit 5d7e0a0

Please sign in to comment.