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

mdast-util-footnote

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-util-footnote - npm Package Compare versions

Comparing version 0.1.7 to 1.0.0

index.d.ts

171

index.js

@@ -1,2 +0,169 @@

exports.fromMarkdown = require('./from-markdown')
exports.toMarkdown = require('./to-markdown')
/**
* @typedef {import('mdast').Footnote} Footnote
* @typedef {import('mdast').FootnoteReference} FootnoteReference
* @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
* @typedef {import('mdast-util-to-markdown').Map} Map
*/
import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
import {association} from 'mdast-util-to-markdown/lib/util/association.js'
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js'
import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
/** @type {FromMarkdownExtension} */
export const footnoteFromMarkdown = {
canContainEols: ['footnote'],
enter: {
footnoteDefinition: enterFootnoteDefinition,
footnoteDefinitionLabelString: enterFootnoteDefinitionLabelString,
footnoteCall: enterFootnoteCall,
footnoteCallString: enterFootnoteCallString,
inlineNote: enterNote
},
exit: {
footnoteDefinition: exitFootnoteDefinition,
footnoteDefinitionLabelString: exitFootnoteDefinitionLabelString,
footnoteCall: exitFootnoteCall,
footnoteCallString: exitFootnoteCallString,
inlineNote: exitNote
}
}
/** @type {ToMarkdownExtension} */
export const footnoteToMarkdown = {
// This is on by default already.
unsafe: [{character: '[', inConstruct: ['phrasing', 'label', 'reference']}],
handlers: {footnote, footnoteDefinition, footnoteReference}
}
footnoteReference.peek = footnoteReferencePeek
footnote.peek = footnotePeek
/** @type {FromMarkdownHandle} */
function enterFootnoteDefinition(token) {
this.enter(
{type: 'footnoteDefinition', identifier: '', label: '', children: []},
token
)
}
/** @type {FromMarkdownHandle} */
function enterFootnoteDefinitionLabelString() {
this.buffer()
}
/** @type {FromMarkdownHandle} */
function exitFootnoteDefinitionLabelString(token) {
const label = this.resume()
this.stack[this.stack.length - 1].label = label
this.stack[this.stack.length - 1].identifier = normalizeIdentifier(
this.sliceSerialize(token)
).toLowerCase()
}
/** @type {FromMarkdownHandle} */
function exitFootnoteDefinition(token) {
this.exit(token)
}
/** @type {FromMarkdownHandle} */
function enterFootnoteCall(token) {
this.enter({type: 'footnoteReference', identifier: '', label: ''}, token)
}
/** @type {FromMarkdownHandle} */
function enterFootnoteCallString() {
this.buffer()
}
/** @type {FromMarkdownHandle} */
function exitFootnoteCallString(token) {
const label = this.resume()
this.stack[this.stack.length - 1].label = label
this.stack[this.stack.length - 1].identifier = normalizeIdentifier(
this.sliceSerialize(token)
).toLowerCase()
}
/** @type {FromMarkdownHandle} */
function exitFootnoteCall(token) {
this.exit(token)
}
/** @type {FromMarkdownHandle} */
function enterNote(token) {
this.enter({type: 'footnote', children: []}, token)
}
/** @type {FromMarkdownHandle} */
function exitNote(token) {
this.exit(token)
}
/**
* @type {ToMarkdownHandle}
* @param {FootnoteReference} node
*/
function footnoteReference(node, _, context) {
const exit = context.enter('footnoteReference')
const subexit = context.enter('reference')
const reference = safe(context, association(node), {before: '^', after: ']'})
subexit()
exit()
return '[^' + reference + ']'
}
/** @type {ToMarkdownHandle} */
function footnoteReferencePeek() {
return '['
}
/**
* @type {ToMarkdownHandle}
* @param {Footnote} node
*/
function footnote(node, _, context) {
const exit = context.enter('footnote')
const subexit = context.enter('label')
const value =
'^[' + containerPhrasing(node, context, {before: '[', after: ']'}) + ']'
subexit()
exit()
return value
}
/** @type {ToMarkdownHandle} */
function footnotePeek() {
return '^'
}
/**
* @type {ToMarkdownHandle}
* @param {FootnoteDefinition} node
*/
function footnoteDefinition(node, _, context) {
const exit = context.enter('footnoteDefinition')
const subexit = context.enter('label')
const label =
'[^' + safe(context, association(node), {before: '^', after: ']'}) + ']:'
subexit()
const value = indentLines(containerFlow(node, context), map)
exit()
return value
/** @type {Map} */
function map(line, index, blank) {
if (index) {
return (blank ? '' : ' ') + line
}
return (blank ? label : label + ' ') + line
}
}

50

package.json
{
"name": "mdast-util-footnote",
"version": "0.1.7",
"version": "1.0.0",
"description": "mdast extension to parse and serialize footnotes",

@@ -28,33 +28,36 @@ "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": {
"mdast-util-to-markdown": "^0.6.0",
"micromark": "~2.11.0"
"@types/mdast": "^3.0.0",
"mdast-util-to-markdown": "^1.0.0",
"micromark-util-normalize-identifier": "^1.0.0"
},
"devDependencies": {
"mdast-util-from-markdown": "^0.8.0",
"micromark-extension-footnote": "~0.3.0",
"nyc": "^15.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"mdast-util-from-markdown": "^1.0.0",
"micromark-extension-footnote": "^1.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"xo": "^0.36.0"
"type-coverage": "^2.0.0",
"typescript": "^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-footnote

You probably shouldn’t use this package directly, but instead use
[`remark-footnotes`][remark-footnotes] with **[remark][]**.
## When to use this
Use this if you’re dealing with the AST manually.
It might be better to use [`remark-footnotes`][remark-footnotes] 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][]:

@@ -56,16 +63,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-footnote')
var footnote = require('mdast-util-footnote')
import fs from 'node:fs'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {footnote} from 'micromark-extension-footnote'
import {footnoteFromMarkdown, footnoteToMarkdown} from 'mdast-util-footnote'
var doc = fs.readFileSync('example.md')
const doc = fs.readFileSync('example.md')
var tree = fromMarkdown(doc, {
extensions: [syntax({inlineNotes: true})],
mdastExtensions: [footnote.fromMarkdown]
const tree = fromMarkdown(doc, {
extensions: [footnote({inlineNotes: true})],
mdastExtensions: [footnoteFromMarkdown]
})

@@ -75,3 +82,3 @@

var out = toMarkdown(tree, {extensions: [footnote.toMarkdown]})
const out = toMarkdown(tree, {extensions: [footnoteToMarkdown]})

@@ -179,9 +186,9 @@ console.log(out)

### `footnote.fromMarkdown`
This package exports the following identifier: `footnoteFromMarkdown`,
`footnoteToMarkdown`.
There is no default export.
### `footnote.toMarkdown`
### `footnoteFromMarkdown`
> Note: the separate extensions are also available at
> `mdast-util-footnote/from-markdown` and
> `mdast-util-footnote/to-markdown`.
### `footnoteToMarkdown`

@@ -188,0 +195,0 @@ Support footnotes.

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