77 * @typedef {import('./types.js').Options } Options
88 * @typedef {import('./types.js').Parent } Parent
99 * @typedef {import('./types.js').PhrasingContent } PhrasingContent
10+ * @typedef {import('./types.js').SafeConfig } SafeConfig
1011 * @typedef {import('./types.js').State } State
1112 * @typedef {import('./types.js').TrackFields } TrackFields
1213 */
@@ -19,6 +20,7 @@ import {unsafe} from './unsafe.js'
1920import { containerPhrasing } from './util/container-phrasing.js'
2021import { containerFlow } from './util/container-flow.js'
2122import { indentLines } from './util/indent-lines.js'
23+ import { safe } from './util/safe.js'
2224
2325/**
2426 * Turn an mdast syntax tree into markdown.
@@ -37,6 +39,7 @@ export function toMarkdown(tree, options = {}) {
3739 indentLines,
3840 containerPhrasing : containerPhrasingBound ,
3941 containerFlow : containerFlowBound ,
42+ safe : safeBound ,
4043 stack : [ ] ,
4144 unsafe : [ ] ,
4245 join : [ ] ,
@@ -150,3 +153,32 @@ function containerPhrasingBound(parent, info) {
150153function containerFlowBound ( parent , info ) {
151154 return containerFlow ( parent , this , info )
152155}
156+
157+ /**
158+ * Make a string safe for embedding in markdown constructs.
159+ *
160+ * In markdown, almost all punctuation characters can, in certain cases,
161+ * result in something.
162+ * Whether they do is highly subjective to where they happen and in what
163+ * they happen.
164+ *
165+ * To solve this, `mdast-util-to-markdown` tracks:
166+ *
167+ * * Characters before and after something;
168+ * * What “constructs” we are in.
169+ *
170+ * This information is then used by this function to escape or encode
171+ * special characters.
172+ *
173+ * @this {State}
174+ * Info passed around about the current state.
175+ * @param {string | null | undefined } value
176+ * Raw value to make safe.
177+ * @param {SafeConfig } config
178+ * Configuration.
179+ * @returns {string }
180+ * Serialized markdown safe for embedding.
181+ */
182+ function safeBound ( value , config ) {
183+ return safe ( this , value , config )
184+ }
0 commit comments