From a26588835b8e2752e1d1a9480280d34217bb782e Mon Sep 17 00:00:00 2001 From: James Brumond Date: Sat, 29 Apr 2023 17:19:34 -0700 Subject: [PATCH] support for stripping off frontmatter --- bin/strip-frontmatter | 2 ++ bin/strip-frontmatter.js | 2 ++ readme.md | 38 ++++++++++++++++++++++++++++++++++++ src/bin/strip-frontmatter.ts | 2 ++ src/frontmatter.ts | 18 +++++++++++++++++ src/index.ts | 3 ++- 6 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 bin/strip-frontmatter create mode 100644 bin/strip-frontmatter.js create mode 100644 src/bin/strip-frontmatter.ts create mode 100644 src/frontmatter.ts diff --git a/bin/strip-frontmatter b/bin/strip-frontmatter new file mode 100644 index 0000000..f17b3fb --- /dev/null +++ b/bin/strip-frontmatter @@ -0,0 +1,2 @@ +#!/bin/sh +node $(dirname $0)/strip-frontmatter.js $* \ No newline at end of file diff --git a/bin/strip-frontmatter.js b/bin/strip-frontmatter.js new file mode 100644 index 0000000..62e7212 --- /dev/null +++ b/bin/strip-frontmatter.js @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('../build/bin/strip-frontmatter.js'); \ No newline at end of file diff --git a/readme.md b/readme.md index 10b50a1..a124be7 100644 --- a/readme.md +++ b/readme.md @@ -38,6 +38,28 @@ async function main() { } ``` +### Handling Frontmatter + +```ts +import { process_frontmatter } from './build'; + +const raw_content = `--- +title: Example Markdown with Frontmatter +foo: +- bar +- baz +--- + +# This is some markdown +`; + +const { frontmatter, markdown } = process_frontmatter(raw_content); + +console.log(frontmatter.title); // "Example Markdown with Frontmatter" +console.log(frontmatter.foo); // [ "bar", "baz" ] +console.log(markdown); // "\n# This is some markdown\n" +``` + ## Command Line Use @@ -46,6 +68,22 @@ async function main() { echo '# This is some markdown' | ./bin/markdown2html --base-url 'https://example.com' > output.html ``` +### Handling Frontmatter + +```bash +filecontents="--- +title: Example Markdown with Frontmatter +foo: +- bar +- baz +--- + +# This is some markdown +" + +echo "$filecontents" | ./bin/strip-frontmatter frontmatter.json > markdown.md +``` + ## Options diff --git a/src/bin/strip-frontmatter.ts b/src/bin/strip-frontmatter.ts new file mode 100644 index 0000000..00d300d --- /dev/null +++ b/src/bin/strip-frontmatter.ts @@ -0,0 +1,2 @@ + +console.log('strip-frontmatter'); diff --git a/src/frontmatter.ts b/src/frontmatter.ts new file mode 100644 index 0000000..1e49651 --- /dev/null +++ b/src/frontmatter.ts @@ -0,0 +1,18 @@ + +import * as yaml from 'yaml'; + +export function process_frontmatter(document: string) { + if (! document.startsWith('---\n')) { + return { + frontmatter: null, + markdown: document, + }; + } + + const endIndex = document.slice(3).indexOf('\n---\n') + 3; + const frontmatterYaml = document.slice(3, endIndex); + const markdown = document.slice(endIndex + 4); + const frontmatter = yaml.parse(frontmatterYaml); + + return { frontmatter, markdown }; +} diff --git a/src/index.ts b/src/index.ts index 8ecc5ae..a30d753 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ -export * from './render'; +export { process_frontmatter } from './frontmatter'; +export { render_markdown_to_html, MarkdownExtension, MarkdownOptions } from './render';