Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement own frontmatter parser (#17)
Most open source solutions use js-yaml which is less preferred solution these days and bloated with other deps. Here added own frontmatter parser based on https://github.com/vfile/vfile-matter
- Loading branch information
Showing
8 changed files
with
84 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { frontmatter } from "./frontmatter"; | ||
|
||
const someYaml = "---\nkey: value\nlist:\n - 1\n - 2\n---"; | ||
const someData = { key: "value", list: [1, 2] }; | ||
const doc = "Here is a document\nMore of the document\nOther lines\n"; | ||
const both = someYaml + "\n" + doc; | ||
|
||
test("should parse and strip frontmatter", () => { | ||
expect(frontmatter(both)).toEqual({ | ||
data: someData, | ||
content: doc, | ||
}); | ||
}); | ||
|
||
test("should support no matter", () => { | ||
expect(frontmatter(doc)).toEqual({ | ||
data: {}, | ||
content: doc, | ||
}); | ||
}); | ||
|
||
test("should strip matter completely", () => { | ||
expect(frontmatter(someYaml)).toEqual({ | ||
data: someData, | ||
content: "", | ||
}); | ||
}); | ||
|
||
test("should handle thematic breaks", () => { | ||
const extra = "Here is a thematic break\n---\nEnd"; | ||
expect(frontmatter(both + extra)).toEqual({ | ||
data: someData, | ||
content: doc + extra, | ||
}); | ||
}); | ||
|
||
test("should support additional newline before closing matter", () => { | ||
expect(frontmatter("---\nkey: value\n\n---\n" + doc)).toEqual({ | ||
data: { key: "value" }, | ||
content: doc, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// inspired by https://github.com/vfile/vfile-matter | ||
|
||
import { parse } from "yaml"; | ||
|
||
export const frontmatter = (content: string) => { | ||
const match = /^---(?:\r?\n|\r)(?:([\s\S]*?)(?:\r?\n|\r))?---(?:\r?\n|\r|$)/.exec(content); | ||
if (match) { | ||
return { | ||
data: parse(match[1]), | ||
content: content.slice(match[0].length), | ||
}; | ||
} | ||
return { | ||
data: {}, | ||
content, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters