Skip to content

Commit 6e8fd17

Browse files
committed
Add improved docs
1 parent 74ed52a commit 6e8fd17

File tree

2 files changed

+106
-10
lines changed

2 files changed

+106
-10
lines changed

lib/index.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
* @typedef {Extract<NlcstNode, import('unist').Parent>} NlcstParent
1717
* @typedef {HastRoot | HastContent} HastNode
1818
*
19-
*
2019
* @typedef {{
2120
* tokenizeSentencePlugins: Array<(node: NlcstSentence) => void>,
2221
* tokenizeParagraphPlugins: Array<(node: NlcstParagraph) => void>,
2322
* parse(value: string | null | undefined): NlcstRoot
2423
* tokenizeParagraph(value: string | null | undefined): NlcstParagraph
2524
* tokenize(value: string | null | undefined): Array<NlcstSentenceContent>
2625
* }} ParserInstance
26+
* nlcst parser.
27+
*
28+
* For example, `parse-dutch`, `parse-english`, or `parse-latin`.
29+
*
2730
* @typedef {new () => ParserInstance} ParserConstructor
31+
* Create a new parser.
2832
*/
2933

3034
import {embedded} from 'hast-util-embedded'
@@ -80,6 +84,50 @@ const terminalMarker = /^([!.?\u2026\u203D]+)$/
8084
/**
8185
* Turn a hast tree into an nlcst tree.
8286
*
87+
* > 👉 **Note**: `tree` must have positional info and `file` must be a `VFile`
88+
* > corresponding to `tree`.
89+
*
90+
* ##### Notes
91+
*
92+
* ###### Implied paragraphs
93+
*
94+
* The algorithm supports implicit and explicit paragraphs, such as:
95+
*
96+
* ```html
97+
* <article>
98+
* An implicit paragraph.
99+
* <h1>An explicit paragraph.</h1>
100+
* </article>
101+
* ```
102+
*
103+
* Overlapping paragraphs are also supported (see the tests or the HTML spec for
104+
* more info).
105+
*
106+
* ###### Ignored nodes
107+
*
108+
* Some elements are ignored and their content will not be present in
109+
* **[nlcst][]**: `<script>`, `<style>`, `<svg>`, `<math>`, `<del>`.
110+
*
111+
* To ignore other elements, add a `data-nlcst` attribute with a value of `ignore`:
112+
*
113+
* ```html
114+
* <p>This is <span data-nlcst="ignore">hidden</span>.</p>
115+
* <p data-nlcst="ignore">Completely hidden.</p>
116+
* ```
117+
*
118+
* ###### Source nodes
119+
*
120+
* `<code>` elements are mapped to [`Source`][nlcst-source] nodes in
121+
* **[nlcst][]**.
122+
*
123+
* To mark other elements as source, add a `data-nlcst` attribute with a value
124+
* of `source`:
125+
*
126+
* ```html
127+
* <p>This is <span data-nlcst="source">marked as source</span>.</p>
128+
* <p data-nlcst="source">Completely marked.</p>
129+
* ```
130+
*
83131
* @param {HastNode} tree
84132
* hast tree to transform.
85133
* @param {VFile} file

readme.md

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* [Use](#use)
1919
* [API](#api)
2020
* [`toNlcst(tree, file, Parser)`](#tonlcsttree-file-parser)
21+
* [`ParserConstructor`](#parserconstructor)
22+
* [`ParserInstance`](#parserinstance)
2123
* [Types](#types)
2224
* [Compatibility](#compatibility)
2325
* [Security](#security)
@@ -46,7 +48,7 @@ same at a higher-level (easier) abstraction.
4648
## Install
4749

4850
This package is [ESM only][esm].
49-
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
51+
In Node.js (version 14.14+ and or 16.0+), install with [npm][]:
5052

5153
```sh
5254
npm install hast-util-to-nlcst
@@ -119,17 +121,25 @@ RootNode[2] (1:1-6:1, 0-134)
119121

120122
## API
121123

122-
This package exports the identifier `toNlcst`.
124+
This package exports the identifier [`toNlcst`][tonlcst].
123125
There is no default export.
124126

125127
### `toNlcst(tree, file, Parser)`
126128

127-
[hast][] utility to transform to [nlcst][].
129+
Turn a hast tree into an nlcst tree.
130+
131+
> 👉 **Note**: `tree` must have positional info and `file` must be a `VFile`
132+
> corresponding to `tree`.
133+
134+
##### Parameters
128135

129-
> 👉 **Note**: `tree` must have positional info, `file` must be a [vfile][]
130-
> corresponding to `tree`, and `Parser` must be a parser such as
131-
> [`parse-english`][parse-english], [`parse-dutch`][parse-dutch], or
132-
> [`parse-latin`][parse-latin].
136+
* `tree` ([`HastNode`][hast-node])
137+
— hast tree to transform
138+
* `file` ([`VFile`][vfile])
139+
— virtual file
140+
* `Parser` ([`ParserConstructor`][parserconstructor] or
141+
[`ParserInstance`][parserinstance])
142+
— parser to use.
133143

134144
##### Returns
135145

@@ -176,16 +186,46 @@ of `source`:
176186
<p data-nlcst="source">Completely marked.</p>
177187
```
178188

189+
### `ParserConstructor`
190+
191+
Create a new parser (TypeScript type).
192+
193+
###### Type
194+
195+
```ts
196+
type ParserConstructor = new () => ParserInstance
197+
```
198+
199+
### `ParserInstance`
200+
201+
nlcst parser (TypeScript type).
202+
203+
For example, [`parse-dutch`][parse-dutch], [`parse-english`][parse-english], or
204+
[`parse-latin`][parse-latin].
205+
206+
###### Type
207+
208+
```ts
209+
type ParserInstance = type ParserInstance = {
210+
tokenizeSentencePlugins: Array<(node: NlcstSentence) => void>
211+
tokenizeParagraphPlugins: Array<(node: NlcstParagraph) => void>
212+
parse(value: string | null | undefined): NlcstRoot
213+
tokenizeParagraph(value: string | null | undefined): NlcstParagraph
214+
tokenize(value: string | null | undefined): Array<NlcstSentenceContent>
215+
}
216+
```
217+
179218
## Types
180219
181220
This package is fully typed with [TypeScript][].
182-
It exports the additional types `ParserConstructor` and `ParserInstance`.
221+
It exports the additional types [`ParserConstructor`][parserconstructor] and
222+
[`ParserInstance`][parserinstance].
183223
184224
## Compatibility
185225
186226
Projects maintained by the unified collective are compatible with all maintained
187227
versions of Node.js.
188-
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
228+
As of now, that is Node.js 14.14+ and 16.0+.
189229
Our projects sometimes work with older versions, but this is not guaranteed.
190230
191231
## Security
@@ -270,6 +310,8 @@ abide by its terms.
270310
271311
[hast]: https://github.com/syntax-tree/hast
272312
313+
[hast-node]: https://github.com/syntax-tree/hast#nodes
314+
273315
[nlcst]: https://github.com/syntax-tree/nlcst
274316
275317
[nlcst-node]: https://github.com/syntax-tree/nlcst#nodes
@@ -285,3 +327,9 @@ abide by its terms.
285327
[parse-latin]: https://github.com/wooorm/parse-latin
286328
287329
[parse-dutch]: https://github.com/wooorm/parse-dutch
330+
331+
[tonlcst]: #tonlcsttree-file-parser
332+
333+
[parserconstructor]: #parserconstructor
334+
335+
[parserinstance]: #parserinstance

0 commit comments

Comments
 (0)