diff options
| author | Asko Nõmm <asko@nmm.ee> | 2025-01-25 22:12:30 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-25 22:12:30 +0200 |
| commit | 160cb5a7c2a733ee9c1eaa69008148ba9c21125d (patch) | |
| tree | 7d5649d027444443e4aedda7293627d37d956d00 /src | |
| parent | 7f3a7e9770623743fde1cc9fde8aea7d476a5700 (diff) | |
| parent | d8d3118a9bdfe822292d1f4d28d2879aaa1ae86f (diff) | |
Merge pull request #3 from askonomm/2-if-the-content-starts-and-ends-with-----add-content-item-frontmatter
Implement FrontMatter parsing.
Diffstat (limited to 'src')
| -rw-r--r-- | src/flatmatter.test.ts | 16 | ||||
| -rw-r--r-- | src/flatmatter.ts | 27 | ||||
| -rw-r--r-- | src/serializers/to_json.test.ts | 4 | ||||
| -rw-r--r-- | src/serializers/to_json.ts | 4 | ||||
| -rw-r--r-- | src/serializers/to_object.ts | 4 |
5 files changed, 36 insertions, 19 deletions
diff --git a/src/flatmatter.test.ts b/src/flatmatter.test.ts index 4c349aa..bbe26d9 100644 --- a/src/flatmatter.test.ts +++ b/src/flatmatter.test.ts @@ -1,16 +1,22 @@ -import FlatMatter from "./flatmatter"; +import FlatMatter from "./flatmatter.ts"; import {assert} from "vitest"; +import ToObject from "./serializers/to_object.ts"; test('Line has no value separator', () => { - expect(() => new FlatMatter('test')) - .toThrowError("Line on index 0 doesn't have a value separator."); + assert.throws(() => new FlatMatter('test'), "Line on index 0 doesn't have a value separator."); }) test('Line can only have one value separator', () => { - expect(() => new FlatMatter('test: this: that')) - .toThrowError("Line on index 0 has multiple value separators.") + assert.throws(() => new FlatMatter('test: this: that'), 'Line on index 0 has multiple value separators.') }) test('String values can have colon characters', () => { assert.doesNotThrow(() => new FlatMatter('test: "this : that"'), Error) +}) + +test('FrontMatter creates a new content entry', () => { + const fm = new FlatMatter(`---\nthis: true\n---\n\nMarkdown goes here.\n\nAnd here.`); + const result = fm.serialize(new ToObject()); + + assert.deepEqual(result, {"this": true, "content": "Markdown goes here.\n\nAnd here."}); })
\ No newline at end of file diff --git a/src/flatmatter.ts b/src/flatmatter.ts index 3bf8704..8ffee6f 100644 --- a/src/flatmatter.ts +++ b/src/flatmatter.ts @@ -1,9 +1,6 @@ +import { EOL } from "node:os"; import {trimChar} from "./utils.ts"; -export type Matter = { - [key: string]: Matter | unknown; -}; - export type ParsedValue = { value: unknown; computeActions: ComputeAction[]; @@ -15,7 +12,7 @@ export type ComputeAction = { }; export interface Serializer { - serialize(parsedConfig: Matter): unknown; + serialize(parsedConfig: Record<string, unknown>): unknown; } export interface FlatMatterFn { @@ -26,7 +23,7 @@ export interface FlatMatterFn { export default class FlatMatter { private content: string; - private parsedConfig: Matter = {}; + private parsedConfig: Record<string, unknown> = {}; private functions: FlatMatterFn[]; constructor(content: string, functions: FlatMatterFn[] = []) { @@ -37,9 +34,23 @@ export default class FlatMatter { private parse(): void { const lines = this.content.split(/\r?\n/); + let frontMatterBreakCount = 0; for (let i = 0; i < lines.length; i++) { - this.parseLine(i, lines[i]); + if (lines[i].trim() === "---" && frontMatterBreakCount < 2) { + frontMatterBreakCount++; + continue; + } + + + // FlatMatter ends, Markdown begins + if (frontMatterBreakCount < 2) { + this.parseLine(i, lines[i]); + continue; + } + + this.parsedConfig.content = lines.slice(i).join(EOL).trim(); + break; } } @@ -60,7 +71,7 @@ export default class FlatMatter { const config = keys.reduceRight((acc, key) => { return {[key]: acc}; - }, this.computeValue(parsedValue)) as Matter; + }, this.computeValue(parsedValue)) as Record<string, unknown>; this.parsedConfig = {...this.parsedConfig, ...config}; } diff --git a/src/serializers/to_json.test.ts b/src/serializers/to_json.test.ts index 01f73e0..22f7ba8 100644 --- a/src/serializers/to_json.test.ts +++ b/src/serializers/to_json.test.ts @@ -1,5 +1,5 @@ -import FlatMatter from "../flatmatter"; -import ToJson from "./to_json"; +import FlatMatter from "../flatmatter.ts"; +import ToJson from "./to_json.ts"; test("Single-level configuration", () => { const fm = new FlatMatter( diff --git a/src/serializers/to_json.ts b/src/serializers/to_json.ts index f915a88..76148db 100644 --- a/src/serializers/to_json.ts +++ b/src/serializers/to_json.ts @@ -1,7 +1,7 @@ -import type { Matter, Serializer } from "../flatmatter.ts"; +import type { Serializer } from "../flatmatter.ts"; export default class ToJson implements Serializer { - serialize(parsedConfig: Matter): string { + serialize(parsedConfig: Record<string, unknown>): string { return JSON.stringify(parsedConfig); } } diff --git a/src/serializers/to_object.ts b/src/serializers/to_object.ts index 1b07144..a7161cc 100644 --- a/src/serializers/to_object.ts +++ b/src/serializers/to_object.ts @@ -1,7 +1,7 @@ -import type { Matter, Serializer } from "../flatmatter.ts"; +import type {Serializer} from "../flatmatter.ts"; export default class ToObject implements Serializer { - serialize(parsedConfig: Matter): Matter { + serialize(parsedConfig: Record<string, unknown>): Record<string, unknown> { return parsedConfig; } } |
