1.0.0 • Published 1 year ago
mdast-util-inline-spoiler v1.0.0
mdast-util-inline-spoiler
Thanks to benrbray/remark-cite for a
remarkplugin boilerplate.
Extension for mdast-util-from-markdown and
mdast-util-to-markdown to support Discord-style spoilers. Converts the token stream produced by micromark-extension-inline-spoiler into an abstract syntax tree.
Using remark? You probably shouldn’t use this package directly, but instead use remark-inline-spoiler. See micromark-extension-inline-spoiler for a full description of the supported syntax.
Install
Install mdast-util-inline-spoiler on npm, yarn or bun.
npm install mdast-util-inline-spoiler
yarn add mdast-util-inline-spoiler
bun install mdast-util-inline-spoilerUsage
Markdown to AST
import fromMarkdown from 'mdast-util-from-markdown'
import { spoilerSyntax } from 'micromark-extension-inline-spoiler'
import { spoilerFromMarkdown } from 'mdast-util-inline-spoiler'
let ast = fromMarkdown('||Don\'t spoil this||', {
extensions: [spoilerSyntax()],
mdastExtensions: [spoilerFromMarkdown]
})The corresponding node in the abstract syntax tree has the form below, where value contains the hidden content:
{
"type": "spoiler",
"value": "Don't spoil this"
}AST to Markdown
Taking the ast from the previous example,
import fromMarkdown from 'mdast-util-from-markdown'
import { spoilerToMarkdown } from 'mdast-util-inline-spoiler'
let markdownString = toMarkdown(ast, {
extensions: [spoilerToMarkdown({})]
}).trim();The result will be:
||Don't spoil this||1.0.0
1 year ago