A TypeScript library to parse front matter from Markdown files.
Just a port of front-matter
Install the package using npm:
npm install ts-frontmatter
You can use the extractor function to parse the front matter and body content from a Markdown string.
import { extractor, FrontMatterResult } from 'ts-frontmatter';
import * as fs from 'fs-extra';
const filePath = 'path/to/your/markdown-file.md';
const fileContent = fs.readFileSync(filePath, 'utf-8');
// Use the extractor function to parse front matter and body
const content: FrontMatterResult = extractor(fileContent);
console.log(content.attributes); // Parsed front matter attributes
console.log(content.body); // The body of the Markdown content
console.log(content.bodyBegin); // Line number where the body begins
const { extractor } = require('ts-frontmatter');
const fs = require('fs-extra');
const filePath = 'path/to/your/markdown-file.md';
const fileContent = fs.readFileSync(filePath, 'utf-8');
// Use the extractor function to parse front matter and body
const content = extractor(fileContent);
console.log(content.attributes); // Parsed front matter attributes
console.log(content.body); // The body of the Markdown content
console.log(content.bodyBegin); // Line number where the body begins
Use the test function to check if a Markdown string contains front matter.
import { test } from 'ts-frontmatter';
const markdownString = `
---
title: "Hello World"
date: "2024-08-06"
---
This is my first blog post written in Markdown.
`;
const hasFrontMatter = test(markdownString);
console.log(hasFrontMatter); // true or false
const { test } = require('ts-frontmatter');
const markdownString = `
---
title: "Hello World"
date: "2024-08-06"
---
This is my first blog post written in Markdown.
`;
const hasFrontMatter = test(markdownString);
console.log(hasFrontMatter); // true or false
extractor(string: string, options?: { allowUnsafe?: boolean }): FrontMatterResult
Parses the front matter and body from the given string.
-
string
: The input string containing front matter and body content. -
options
: Optional settings for the extraction process. Currently supportsallowUnsafe
An object of type FrontMatterResult
containing the parsed front matter attributes, body content, and other metadata.
test(string: string): boolean
Tests if the given string contains front matter.
-
string
: The input string to test.
-
boolean
: True if the string contains front matter, otherwise false.
FrontMatterAttributes
An interface representing the front matter attributes.
interface FrontMatterAttributes {
[key: string]: any;
}
FrontMatterResult
An interface representing the result of parsing front matter and body content.
interface FrontMatterResult {
attributes: FrontMatterAttributes;
body: string;
bodyBegin: number;
frontmatter?: string;
}
Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.
npm test
This project is licensed under the MIT License. See the LICENSE file for details.
Lots of love, Mikke