Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-frontmatter

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-frontmatter

A TypeScript library to parse front matter from Markdown files.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

ts-frontmatter

A TypeScript library to parse front matter from Markdown files.

Just a port of front-matter

Installation

Install the package using npm:

npm install ts-frontmatter

Usage

Extracting Front Matter and Body

You can use the extractor function to parse the front matter and body content from a Markdown string.

TypeScript Example
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
JavaScript Example
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
Testing for Front Matter

Use the test function to check if a Markdown string contains front matter.

TypeScript test Example
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
JavaScript test Example
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
API

extractor(string: string, options?: { allowUnsafe?: boolean }): FrontMatterResult

Parses the front matter and body from the given string.

Parameters
  • string: The input string containing front matter and body content.
  • options: Optional settings for the extraction process. Currently supports allowUnsafe
Returns

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.

Parameters
  • string: The input string to test.
Returns
  • boolean: True if the string contains front matter, otherwise false.

Types

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;
}

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.

Running Tests
npm test

License

This project is licensed under the MIT License. See the LICENSE file for details.


Lots of love, Mikke

Keywords

FAQs

Package last updated on 06 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc