UNPKG

7.3 kBMarkdownView Raw
1# remark-stringify [![Travis][build-badge]][build-status] [![Coverage][coverage-badge]][coverage-status] [![Downloads][dl-badge]][dl] [![Size][size-badge]][size] [![Chat][chat-badge]][chat]
2
3[Compiler][] for [**unified**][unified]. Stringifies an
4[**MDAST**][mdast] syntax tree to markdown. Used in the [**remark**
5processor][processor]. Can be [extended][extend] to change how
6markdown is compiled.
7
8## Installation
9
10[npm][]:
11
12```sh
13npm install remark-stringify
14```
15
16## Usage
17
18```js
19var unified = require('unified')
20var createStream = require('unified-stream')
21var parse = require('remark-parse')
22var toc = require('remark-toc')
23var stringify = require('remark-stringify')
24
25var processor = unified()
26 .use(parse)
27 .use(toc)
28 .use(stringify, {
29 bullet: '*',
30 fence: '~',
31 fences: true,
32 incrementListMarker: false
33 })
34
35process.stdin.pipe(createStream(processor)).pipe(process.stdout)
36```
37
38## Table of Contents
39
40* [API](#api)
41 * [processor.use(stringify\[, options\])](#processorusestringify-options)
42 * [stringify.Compiler](#stringifycompiler)
43* [Extending the Compiler](#extending-the-compiler)
44 * [Compiler#visitors](#compilervisitors)
45 * [function visitor(node\[, parent\])](#function-visitornode-parent)
46* [License](#license)
47
48## API
49
50### `processor.use(stringify[, options])`
51
52Configure the `processor` to stringify [**MDAST**][mdast] syntax trees
53to markdown.
54
55##### `options`
56
57Options are passed directly, or passed later through [`processor.data()`][data].
58
59###### `options.gfm`
60
61Stringify with the required escapes for GFM compatible markdown (`boolean`,
62default: `true`).
63
64* Escape pipes (`|`, for tables)
65* Escape colons (`:`, for literal URLs)
66* Escape tildes (`~`, for strike-through)
67
68###### `options.commonmark`
69
70Stringify for CommonMark compatible markdown (`boolean`, default: `false`).
71
72* Compile adjacent blockquotes separately
73* Escape more characters using slashes, instead of as entities
74
75###### `options.pedantic`
76
77Stringify for pedantic compatible markdown (`boolean`, default: `false`).
78
79* Escape underscores in words
80
81###### `options.entities`
82
83How to stringify entities (`string` or `boolean`, default: `false`):
84
85* `true` — Entities are generated for special HTML characters
86 (`&` > `&`) and non-ASCII characters (`©` > `©`).
87 If named entities are not (widely) supported, numbered character
88 references are used (`’` > `’`)
89* `'numbers'` — Numbered entities are generated (`&` > `&`)
90 for special HTML characters and non-ASCII characters
91* `'escape'` — Special HTML characters are encoded (`&` >
92 `&`, `’` > `’`), non-ASCII characters not (ö persists)
93
94###### `options.setext`
95
96Compile headings, when possible, in Setext-style (`boolean`, default: `false`).
97Uses `=` for level one headings and `-` for level two headings. Other heading
98levels are compiled as ATX (respecting `closeAtx`).
99
100###### `options.closeAtx`
101
102Compile ATX headings with the same amount of closing hashes as opening hashes
103(`boolean`, default: `false`).
104
105###### `options.looseTable`
106
107Create tables without fences: initial and final pipes (`boolean`, default:
108`false`).
109
110###### `options.spacedTable`
111
112Create tables without spacing between pipes and content (`boolean`, default:
113`true`).
114
115###### `options.paddedTable`
116
117Create tables with padding in each cell so that they are the same size
118(`boolean`, default: `true`).
119
120###### `options.stringLength`
121
122Function passed to [`markdown-table`][markdown-table] to detect the length of a
123table cell (`Function`, default: [`s => s.length`][string-length]).
124
125###### `options.fence`
126
127Fence marker to use for code blocks (`'~'` or ``'`'``, default: ``'`'``).
128
129###### `options.fences`
130
131Stringify code blocks without language with fences (`boolean`, default:
132`false`).
133
134###### `options.bullet`
135
136Bullet marker to use for unordered list items (`'-'`, `'*'`, or `'+'`,
137default: `'-'`).
138
139###### `options.listItemIndent`
140
141How to indent the content from list items (`'tab'`, `'mixed'` or `'1'`,
142default: `'tab'`).
143
144* `'tab'`: use tab stops (4 spaces)
145* `'1'`: use one space
146* `'mixed'`: use `1` for tight and `tab` for loose list items
147
148###### `options.incrementListMarker`
149
150Whether to increment ordered list item bullets (`boolean`, default: `true`).
151
152###### `options.rule`
153
154Marker to use for thematic breaks / horizontal rules (`'-'`, `'*'`, or `'_'`,
155default: `'*'`).
156
157###### `options.ruleRepetition`
158
159Number of markers to use for thematic breaks / horizontal rules (`number`,
160default: `3`). Should be `3` or more.
161
162###### `options.ruleSpaces`
163
164Whether to pad thematic break (horizontal rule) markers with spaces (`boolean`,
165default `true`).
166
167###### `options.strong`
168
169Marker to use for importance (`'_'` or `'*'`, default `'*'`).
170
171###### `options.emphasis`
172
173Marker to use for emphasis (`'_'` or `'*'`, default `'_'`).
174
175### `stringify.Compiler`
176
177Access to the raw [compiler][], if you need it.
178
179## Extending the Compiler
180
181If this plugin is used, it adds a [`Compiler`][compiler] constructor
182to the `processor`. Other plugins can change and add visitors on
183the compiler’s prototype to change how markdown is stringified.
184
185The below plugin modifies a [visitor][] to add an extra blank line
186before level two headings.
187
188```js
189module.exports = gap
190
191function gap() {
192 var Compiler = this.Compiler
193 var visitors = Compiler.prototype.visitors
194 var original = visitors.heading
195
196 visitors.heading = heading
197
198 function heading(node) {
199 return (node.depth === 2 ? '\n' : '') + original.apply(this, arguments)
200 }
201}
202```
203
204### `Compiler#visitors`
205
206An object mapping [node][] types to [`visitor`][visitor]s.
207
208### `function visitor(node[, parent])`
209
210Stringify `node`.
211
212###### Parameters
213
214* `node` ([`Node`][node]) — Node to compile
215* `parent` ([`Node`][node], optional) — Parent of `node`
216
217###### Returns
218
219`string`, the compiled given `node`.
220
221## License
222
223[MIT][license] © [Titus Wormer][author]
224
225<!-- Definitions -->
226
227[build-badge]: https://img.shields.io/travis/remarkjs/remark/master.svg
228
229[build-status]: https://travis-ci.org/remarkjs/remark
230
231[coverage-badge]: https://img.shields.io/codecov/c/github/remarkjs/remark.svg
232
233[coverage-status]: https://codecov.io/github/remarkjs/remark
234
235[dl-badge]: https://img.shields.io/npm/dm/remark-stringify.svg
236
237[dl]: https://www.npmjs.com/package/remark-stringify
238
239[size-badge]: https://img.shields.io/bundlephobia/minzip/remark-stringify.svg
240
241[size]: https://bundlephobia.com/result?p=remark-stringify
242
243[chat-badge]: https://img.shields.io/gitter/room/remarkjs/Lobby.svg
244
245[chat]: https://gitter.im/remarkjs/Lobby
246
247[license]: https://github.com/remarkjs/remark/blob/master/license
248
249[author]: http://wooorm.com
250
251[npm]: https://docs.npmjs.com/cli/install
252
253[unified]: https://github.com/unifiedjs/unified
254
255[processor]: https://github.com/remarkjs/remark
256
257[data]: https://github.com/unifiedjs/unified#processordatakey-value
258
259[compiler]: https://github.com/unifiedjs/unified#processorcompiler
260
261[mdast]: https://github.com/syntax-tree/mdast
262
263[node]: https://github.com/syntax-tree/unist#node
264
265[extend]: #extending-the-compiler
266
267[visitor]: #function-visitornode-parent
268
269[markdown-table]: https://github.com/wooorm/markdown-table
270
271[string-length]: https://github.com/wooorm/markdown-table#stringlengthcell