Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
ts-frontmatter
Advanced tools
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 supports allowUnsafe
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
FAQs
A TypeScript library to parse front matter from Markdown files.
We found that ts-frontmatter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.