Socket
Socket
Sign inDemoInstall

mdast-util-frontmatter

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-util-frontmatter - npm Package Compare versions

Comparing version 0.2.0 to 1.0.0

index.d.ts

126

index.js

@@ -1,2 +0,124 @@

exports.fromMarkdown = require('./from-markdown')
exports.toMarkdown = require('./to-markdown')
/**
* @typedef {import('mdast').Literal} Literal
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
* @typedef {import('mdast-util-to-markdown/lib/types.js').Options} ToMarkdownExtension
* @typedef {import('mdast-util-to-markdown/lib/types.js').Handle} ToMarkdownHandle
* @typedef {import('mdast-util-to-markdown/lib/util/indent-lines.js').Map} Map
*
* @typedef {import('micromark-extension-frontmatter/matters.js').Options} Options
* @typedef {import('micromark-extension-frontmatter/matters.js').Matter} Matter
* @typedef {import('micromark-extension-frontmatter/matters.js').Info} Info
*/
import {matters} from 'micromark-extension-frontmatter/matters.js'
/**
* @param {Options} [options]
* @returns {FromMarkdownExtension}
*/
export function frontmatterFromMarkdown(options) {
const settings = matters(options)
/** @type {FromMarkdownExtension['enter']} */
const enter = {}
/** @type {FromMarkdownExtension['exit']} */
const exit = {}
let index = -1
while (++index < settings.length) {
const matter = settings[index]
enter[matter.type] = opener(matter)
exit[matter.type] = close
exit[matter.type + 'Value'] = value
}
return {enter, exit}
}
/**
* @param {Matter} matter
* @returns {FromMarkdownHandle} enter
*/
function opener(matter) {
return open
/** @type {FromMarkdownHandle} */
function open(token) {
// @ts-expect-error: custom.
this.enter({type: matter.type, value: ''}, token)
this.buffer()
}
}
/** @type {FromMarkdownHandle} */
function close(token) {
const data = this.resume()
// Remove the initial and final eol.
this.exit(token).value = data.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
}
/** @type {FromMarkdownHandle} */
function value(token) {
this.config.enter.data.call(this, token)
this.config.exit.data.call(this, token)
}
/**
* @param {Options} [options]
* @returns {ToMarkdownExtension}
*/
export function frontmatterToMarkdown(options) {
/** @type {ToMarkdownExtension['unsafe']} */
const unsafe = []
/** @type {ToMarkdownExtension['handlers']} */
const handlers = {}
const settings = matters(options)
let index = -1
while (++index < settings.length) {
const matter = settings[index]
handlers[matter.type] = handler(matter)
unsafe.push({atBreak: true, character: fence(matter, 'open').charAt(0)})
}
return {unsafe, handlers}
}
/**
* @param {Matter} matter
* @returns {(node: Literal) => string} enter
*/
function handler(matter) {
const open = fence(matter, 'open')
const close = fence(matter, 'close')
return handle
/**
* @type {ToMarkdownHandle}
* @param {Literal} node
*/
function handle(node) {
return open + (node.value ? '\n' + node.value : '') + '\n' + close
}
}
/**
* @param {Matter} matter
* @param {'open'|'close'} prop
* @returns {string}
*/
function fence(matter, prop) {
return matter.marker
? pick(matter.marker, prop).repeat(3)
: // @ts-expect-error: They’re mutually exclusive.
pick(matter.fence, prop)
}
/**
* @param {Info|string} schema
* @param {'open'|'close'} prop
* @returns {string}
*/
function pick(schema, prop) {
return typeof schema === 'string' ? schema : schema[prop]
}

52

package.json
{
"name": "mdast-util-frontmatter",
"version": "0.2.0",
"version": "1.0.0",
"description": "mdast extension to parse and serialize frontmatter (YAML, TOML, etc)",

@@ -29,32 +29,35 @@ "license": "MIT",

],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"from-markdown.js",
"index.js",
"to-markdown.js"
"index.d.ts",
"index.js"
],
"dependencies": {
"micromark-extension-frontmatter": "^0.2.0"
"micromark-extension-frontmatter": "^1.0.0"
},
"devDependencies": {
"mdast-util-from-markdown": "^0.5.0",
"mdast-util-to-markdown": "^0.3.0",
"nyc": "^15.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"mdast-util-from-markdown": "^1.0.0",
"mdast-util-to-markdown": "^1.0.0",
"prettier": "^2.0.0",
"remark-cli": "^8.0.0",
"remark-preset-wooorm": "^7.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"xo": "^0.33.0"
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"unist-util-remove-position": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run test-coverage"
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
"test": "npm run build && npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {

@@ -69,4 +72,3 @@ "tabWidth": 2,

"xo": {
"prettier": true,
"esnext": false
"prettier": true
},

@@ -77,3 +79,9 @@ "remarkConfig": {

]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}

@@ -16,7 +16,14 @@ # mdast-util-frontmatter

You probably shouldn’t use this package directly, but instead use
[`remark-frontmatter`][remark-frontmatter] with **[remark][]**.
## When to use this
Use this if you’re dealing with the AST manually.
It’s probably nicer to use [`remark-frontmatter`][remark-frontmatter] with
**[remark][]**, which includes this but provides a nicer interface and
makes it easier to combine with hundreds of plugins.
## Install
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
[npm][]:

@@ -40,16 +47,16 @@

And our script, `example.js`, looks as follows:
And our module, `example.js`, looks as follows:
```js
var fs = require('fs')
var fromMarkdown = require('mdast-util-from-markdown')
var toMarkdown = require('mdast-util-to-markdown')
var syntax = require('micromark-extension-frontmatter')
var frontmatter = require('mdast-util-frontmatter')
import fs from 'node:fs'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {frontmatter} from 'micromark-extension-frontmatter'
import {frontmatterFromMarkdown, frontmatterToMarkdown} from 'mdast-util-frontmatter'
var doc = fs.readFileSync('example.md')
const doc = fs.readFileSync('example.md')
var tree = fromMarkdown(doc, {
extensions: [syntax(['yaml', 'toml'])],
mdastExtensions: [frontmatter.fromMarkdown(['yaml', 'toml'])]
const tree = fromMarkdown(doc, {
extensions: [frontmatter(['yaml', 'toml'])],
mdastExtensions: [frontmatterFromMarkdown(['yaml', 'toml'])]
})

@@ -59,3 +66,3 @@

var out = toMarkdown({extensions: [frontmatter.toMarkdown(['yaml', 'toml'])]})
const out = toMarkdown(tree, {extensions: [frontmatterToMarkdown(['yaml', 'toml'])]})

@@ -91,9 +98,9 @@ console.log(out)

### `frontmatter.fromMarkdown([options])`
This package exports the following identifiers: `frontmatterFromMarkdown`,
`frontmatterToMarkdown`.
There is no default export.
### `frontmatter.toMarkdown([options])`
### `frontmatterFromMarkdown([options])`
> Note: the separate extensions are also available at
> `mdast-util-frontmatter/from-markdown` and
> `mdast-util-frontmatter/to-markdown`.
### `frontmatterToMarkdown([options])`

@@ -138,5 +145,5 @@ Support frontmatter (YAML, TOML, and more).

[build-badge]: https://img.shields.io/travis/syntax-tree/mdast-util-frontmatter.svg
[build-badge]: https://github.com/syntax-tree/mdast-util-frontmatter/workflows/main/badge.svg
[build]: https://travis-ci.org/syntax-tree/mdast-util-frontmatter
[build]: https://github.com/syntax-tree/mdast-util-frontmatter/actions

@@ -143,0 +150,0 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/mdast-util-frontmatter.svg

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