Skip to content

Commit 35a9ccc

Browse files
committed
Add registry for construct names
1 parent ab0136c commit 35a9ccc

12 files changed

+393
-40
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
coverage/
22
node_modules/
3-
*.d.ts
3+
lib/**/*.d.ts
4+
test/**/*.d.ts
45
*.log
56
.DS_Store
67
yarn.lock

index.d.ts

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
import type {Info, State} from './lib/types.js'
2+
3+
/**
4+
* Interface of registered constructs.
5+
*
6+
* When working on extensions that use new constructs, extend the corresponding
7+
* interface to register its name:
8+
*
9+
* ```ts
10+
* declare module 'mdast-util-to-markdown' {
11+
* interface ConstructNameMap {
12+
* // Register a new construct name (value is used, key should match it).
13+
* gfmStrikethrough: 'gfmStrikethrough'
14+
* }
15+
* }
16+
* ```
17+
*/
18+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
19+
export interface ConstructNameMap {
20+
/**
21+
* Whole autolink.
22+
*
23+
* ```markdown
24+
* > | <https://example.com> and <admin@example.com>
25+
* ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
26+
* ```
27+
*/
28+
autolink: 'autolink'
29+
/**
30+
* Whole block quote.
31+
*
32+
* ```markdown
33+
* > | > a
34+
* ^^^
35+
* > | b
36+
* ^
37+
* ```
38+
*/
39+
blockquote: 'blockquote'
40+
/**
41+
* Whole code (indented).
42+
*
43+
* ```markdown
44+
* ␠␠␠␠console.log(1)
45+
* ^^^^^^^^^^^^^^^^^^
46+
* ```
47+
*/
48+
codeIndented: 'codeIndented'
49+
/**
50+
* Whole code (fenced).
51+
*
52+
* ````markdown
53+
* > | ```js
54+
* ^^^^^
55+
* > | console.log(1)
56+
* ^^^^^^^^^^^^^^
57+
* > | ```
58+
* ^^^
59+
* ````
60+
*/
61+
codeFenced: 'codeFenced'
62+
/**
63+
* Code (fenced) language, when fenced with grave accents.
64+
*
65+
* ````markdown
66+
* > | ```js
67+
* ^^
68+
* | console.log(1)
69+
* | ```
70+
* ````
71+
*/
72+
codeFencedLangGraveAccent: 'codeFencedLangGraveAccent'
73+
/**
74+
* Code (fenced) language, when fenced with tildes.
75+
*
76+
* ````markdown
77+
* > | ~~~js
78+
* ^^
79+
* | console.log(1)
80+
* | ~~~
81+
* ````
82+
*/
83+
codeFencedLangTilde: 'codeFencedLangTilde'
84+
/**
85+
* Code (fenced) meta string, when fenced with grave accents.
86+
*
87+
* ````markdown
88+
* > | ```js eval
89+
* ^^^^
90+
* | console.log(1)
91+
* | ```
92+
* ````
93+
*/
94+
codeFencedMetaGraveAccent: 'codeFencedMetaGraveAccent'
95+
/**
96+
* Code (fenced) meta string, when fenced with tildes.
97+
*
98+
* ````markdown
99+
* > | ~~~js eval
100+
* ^^^^
101+
* | console.log(1)
102+
* | ~~~
103+
* ````
104+
*/
105+
codeFencedMetaTilde: 'codeFencedMetaTilde'
106+
/**
107+
* Whole definition.
108+
*
109+
* ```markdown
110+
* > | [a]: b "c"
111+
* ^^^^^^^^^^
112+
* ```
113+
*/
114+
definition: 'definition'
115+
/**
116+
* Destination (literal) (occurs in definition, image, link).
117+
*
118+
* ```markdown
119+
* > | [a]: <b> "c"
120+
* ^^^
121+
* > | a ![b](<c> "d") e
122+
* ^^^
123+
* ```
124+
*/
125+
destinationLiteral: 'destinationLiteral'
126+
/**
127+
* Destination (raw) (occurs in definition, image, link).
128+
*
129+
* ```markdown
130+
* > | [a]: b "c"
131+
* ^
132+
* > | a ![b](c "d") e
133+
* ^
134+
* ```
135+
*/
136+
destinationRaw: 'destinationRaw'
137+
/**
138+
* Emphasis.
139+
*
140+
* ```markdown
141+
* > | *a*
142+
* ^^^
143+
* ```
144+
*/
145+
emphasis: 'emphasis'
146+
/**
147+
* Whole heading (atx).
148+
*
149+
* ```markdown
150+
* > | # alpha
151+
* ^^^^^^^
152+
* ```
153+
*/
154+
headingAtx: 'headingAtx'
155+
/**
156+
* Whole heading (setext).
157+
*
158+
* ```markdown
159+
* > | alpha
160+
* ^^^^^
161+
* > | =====
162+
* ^^^^^
163+
* ```
164+
*/
165+
headingSetext: 'headingSetext'
166+
/**
167+
* Whole image.
168+
*
169+
* ```markdown
170+
* > | ![a](b)
171+
* ^^^^^^^
172+
* > | ![c]
173+
* ^^^^
174+
* ```
175+
*/
176+
image: 'image'
177+
/**
178+
* Whole image reference.
179+
*
180+
* ```markdown
181+
* > | ![a]
182+
* ^^^^
183+
* ```
184+
*/
185+
imageReference: 'imageReference'
186+
/**
187+
* Label (occurs in definitions, image reference, image, link reference,
188+
* link).
189+
*
190+
* ```markdown
191+
* > | [a]: b "c"
192+
* ^^^
193+
* > | a [b] c
194+
* ^^^
195+
* > | a ![b][c] d
196+
* ^^^^
197+
* > | a [b](c) d
198+
* ^^^
199+
* ```
200+
*/
201+
label: 'label'
202+
/**
203+
* Whole link.
204+
*
205+
* ```markdown
206+
* > | [a](b)
207+
* ^^^^^^
208+
* > | [c]
209+
* ^^^
210+
* ```
211+
*/
212+
link: 'link'
213+
/**
214+
* Whole link reference.
215+
*
216+
* ```markdown
217+
* > | [a]
218+
* ^^^
219+
* ```
220+
*/
221+
linkReference: 'linkReference'
222+
/**
223+
* List.
224+
*
225+
* ```markdown
226+
* > | * a
227+
* ^^^
228+
* > | 1. b
229+
* ^^^^
230+
* ```
231+
*/
232+
list: 'list'
233+
/**
234+
* List item.
235+
*
236+
* ```markdown
237+
* > | * a
238+
* ^^^
239+
* > | 1. b
240+
* ^^^^
241+
* ```
242+
*/
243+
listItem: 'listItem'
244+
/**
245+
* Paragraph.
246+
*
247+
* ```markdown
248+
* > | a b
249+
* ^^^
250+
* > | c.
251+
* ^^
252+
* ```
253+
*/
254+
paragraph: 'paragraph'
255+
/**
256+
* Phrasing (occurs in headings, paragraphs, etc).
257+
*
258+
* ```markdown
259+
* > | a
260+
* ^
261+
* ```
262+
*/
263+
phrasing: 'phrasing'
264+
/**
265+
* Reference (occurs in image, link).
266+
*
267+
* ```markdown
268+
* > | [a][]
269+
* ^^
270+
* ```
271+
*/
272+
reference: 'reference'
273+
/**
274+
* Strong.
275+
*
276+
* ```markdown
277+
* > | **a**
278+
* ^^^^^
279+
* ```
280+
*/
281+
strong: 'strong'
282+
/**
283+
* Title using single quotes (occurs in definition, image, link).
284+
*
285+
* ```markdown
286+
* > | [a](b 'c')
287+
* ^^^
288+
* ```
289+
*/
290+
titleApostrophe: 'titleApostrophe'
291+
/**
292+
* Title using double quotes (occurs in definition, image, link).
293+
*
294+
* ```markdown
295+
* > | [a](b "c")
296+
* ^^^
297+
* ```
298+
*/
299+
titleQuote: 'titleQuote'
300+
}
301+
302+
/**
303+
* Construct names for things generated by `mdast-util-to-markdown`.
304+
*
305+
* This is an enum of strings, each being a semantic label, useful to know when
306+
* serializing whether we’re for example in a double (`"`) or single (`'`)
307+
* quoted title.
308+
*/
309+
export type ConstructName = ConstructNameMap[keyof ConstructNameMap]
310+
311+
export {toMarkdown} from './lib/index.js'
312+
export type {
313+
Handle,
314+
Handlers,
315+
Info,
316+
Join,
317+
Options,
318+
State,
319+
Unsafe,
320+
Map
321+
} from './lib/types.js'
322+
// Deprecated.
323+
export type SafeOptions = Info
324+
export type Context = State

index.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1 @@
1-
/**
2-
* @typedef {import('./lib/types.js').Info} SafeOptions
3-
* To do: remove next major: renamed because it doesn’t really reflect
4-
* options, but instead info on the surrounding of the generated thing.
5-
* @typedef {import('./lib/types.js').State} Context
6-
* To do: remove next major: renamed because the word state more clearly
7-
* represents one thing that is passed down to everything and is changed.
8-
*/
9-
10-
/**
11-
* @typedef {import('./lib/types.js').Handle} Handle
12-
* @typedef {import('./lib/types.js').Handlers} Handlers
13-
* @typedef {import('./lib/types.js').Info} Info
14-
* @typedef {import('./lib/types.js').Join} Join
15-
* @typedef {import('./lib/types.js').Options} Options
16-
* @typedef {import('./lib/types.js').State} State
17-
* @typedef {import('./lib/types.js').Unsafe} Unsafe
18-
* @typedef {import('./lib/util/indent-lines.js').Map} Map
19-
*/
20-
211
export {toMarkdown} from './lib/index.js'

lib/handle/code.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function code(node, _, state, info) {
3838
let value = tracker.move(sequence)
3939

4040
if (node.lang) {
41-
const subexit = state.enter('codeFencedLang' + suffix)
41+
const subexit = state.enter(`codeFencedLang${suffix}`)
4242
value += tracker.move(
4343
safe(state, node.lang, {
4444
before: value,
@@ -51,7 +51,7 @@ export function code(node, _, state, info) {
5151
}
5252

5353
if (node.lang && node.meta) {
54-
const subexit = state.enter('codeFencedMeta' + suffix)
54+
const subexit = state.enter(`codeFencedMeta${suffix}`)
5555
value += tracker.move(' ')
5656
value += tracker.move(
5757
safe(state, node.meta, {

lib/handle/definition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function definition(node, _, state, info) {
6262
subexit()
6363

6464
if (node.title) {
65-
subexit = state.enter('title' + suffix)
65+
subexit = state.enter(`title${suffix}`)
6666
value += tracker.move(' ' + quote)
6767
value += tracker.move(
6868
safe(state, node.title, {

lib/handle/image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function image(node, _, state, info) {
5959
subexit()
6060

6161
if (node.title) {
62-
subexit = state.enter('title' + suffix)
62+
subexit = state.enter(`title${suffix}`)
6363
value += tracker.move(' ' + quote)
6464
value += tracker.move(
6565
safe(state, node.title, {

0 commit comments

Comments
 (0)