From ac238d1d58164071eafeb893992662e4e00c6aec Mon Sep 17 00:00:00 2001 From: Kelly Joseph Price Date: Mon, 23 May 2022 15:11:13 -0700 Subject: [PATCH] feat: add yaml compiler (#509) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 🧰 Changes Adds a compiler for frontmatter. If you try to save a doc in the editor with frontmatter, the save currently fails! Something like the following: ``` --- title: Getting started --- Coming content ``` --- __tests__/flavored-compilers/yaml.test.js | 22 ++++++++++++++++++++++ processor/compile/index.js | 1 + processor/compile/yaml.js | 8 ++++++++ 3 files changed, 31 insertions(+) create mode 100644 __tests__/flavored-compilers/yaml.test.js create mode 100644 processor/compile/yaml.js diff --git a/__tests__/flavored-compilers/yaml.test.js b/__tests__/flavored-compilers/yaml.test.js new file mode 100644 index 000000000..877f1f03b --- /dev/null +++ b/__tests__/flavored-compilers/yaml.test.js @@ -0,0 +1,22 @@ +import { mdast, md } from '../../index'; + +describe('yaml compiler', () => { + it('correctly writes out yaml', () => { + const txt = ` +--- +title: This is test +author: A frontmatter test +--- + +Document content! + `; + + expect(md(mdast(txt))).toBe(`--- +title: This is test +author: A frontmatter test +--- + +Document content! +`); + }); +}); diff --git a/processor/compile/index.js b/processor/compile/index.js index 1ae2e9f50..8910c1a76 100644 --- a/processor/compile/index.js +++ b/processor/compile/index.js @@ -13,3 +13,4 @@ export { default as rdmePinCompiler } from './pin'; export { default as rdmeVarCompiler } from './var'; export { default as tableCompiler } from './table'; export { default as tableHeadCompiler } from './table-head'; +export { default as yamlCompiler } from './yaml'; diff --git a/processor/compile/yaml.js b/processor/compile/yaml.js new file mode 100644 index 000000000..e493b80ca --- /dev/null +++ b/processor/compile/yaml.js @@ -0,0 +1,8 @@ +module.exports = function YamlCompiler() { + const { Compiler } = this; + const { visitors } = Compiler.prototype; + + visitors.yaml = function compile(node) { + return `---\n${node.value}\n---`; + }; +};