Skip to content

Commit

Permalink
Add registry for construct names
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 3, 2023
1 parent ab0136c commit 35a9ccc
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
coverage/
node_modules/
*.d.ts
lib/**/*.d.ts
test/**/*.d.ts
*.log
.DS_Store
yarn.lock
324 changes: 324 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
import type {Info, State} from './lib/types.js'

/**
* Interface of registered constructs.
*
* When working on extensions that use new constructs, extend the corresponding
* interface to register its name:
*
* ```ts
* declare module 'mdast-util-to-markdown' {
* interface ConstructNameMap {
* // Register a new construct name (value is used, key should match it).
* gfmStrikethrough: 'gfmStrikethrough'
* }
* }
* ```
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface ConstructNameMap {
/**
* Whole autolink.
*
* ```markdown
* > | <https://example.com> and <admin@example.com>
* ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
* ```
*/
autolink: 'autolink'
/**
* Whole block quote.
*
* ```markdown
* > | > a
* ^^^
* > | b
* ^
* ```
*/
blockquote: 'blockquote'
/**
* Whole code (indented).
*
* ```markdown
* ␠␠␠␠console.log(1)
* ^^^^^^^^^^^^^^^^^^
* ```
*/
codeIndented: 'codeIndented'
/**
* Whole code (fenced).
*
* ````markdown
* > | ```js
* ^^^^^
* > | console.log(1)
* ^^^^^^^^^^^^^^
* > | ```
* ^^^
* ````
*/
codeFenced: 'codeFenced'
/**
* Code (fenced) language, when fenced with grave accents.
*
* ````markdown
* > | ```js
* ^^
* | console.log(1)
* | ```
* ````
*/
codeFencedLangGraveAccent: 'codeFencedLangGraveAccent'
/**
* Code (fenced) language, when fenced with tildes.
*
* ````markdown
* > | ~~~js
* ^^
* | console.log(1)
* | ~~~
* ````
*/
codeFencedLangTilde: 'codeFencedLangTilde'
/**
* Code (fenced) meta string, when fenced with grave accents.
*
* ````markdown
* > | ```js eval
* ^^^^
* | console.log(1)
* | ```
* ````
*/
codeFencedMetaGraveAccent: 'codeFencedMetaGraveAccent'
/**
* Code (fenced) meta string, when fenced with tildes.
*
* ````markdown
* > | ~~~js eval
* ^^^^
* | console.log(1)
* | ~~~
* ````
*/
codeFencedMetaTilde: 'codeFencedMetaTilde'
/**
* Whole definition.
*
* ```markdown
* > | [a]: b "c"
* ^^^^^^^^^^
* ```
*/
definition: 'definition'
/**
* Destination (literal) (occurs in definition, image, link).
*
* ```markdown
* > | [a]: <b> "c"
* ^^^
* > | a ![b](<c> "d") e
* ^^^
* ```
*/
destinationLiteral: 'destinationLiteral'
/**
* Destination (raw) (occurs in definition, image, link).
*
* ```markdown
* > | [a]: b "c"
* ^
* > | a ![b](c "d") e
* ^
* ```
*/
destinationRaw: 'destinationRaw'
/**
* Emphasis.
*
* ```markdown
* > | *a*
* ^^^
* ```
*/
emphasis: 'emphasis'
/**
* Whole heading (atx).
*
* ```markdown
* > | # alpha
* ^^^^^^^
* ```
*/
headingAtx: 'headingAtx'
/**
* Whole heading (setext).
*
* ```markdown
* > | alpha
* ^^^^^
* > | =====
* ^^^^^
* ```
*/
headingSetext: 'headingSetext'
/**
* Whole image.
*
* ```markdown
* > | ![a](b)
* ^^^^^^^
* > | ![c]
* ^^^^
* ```
*/
image: 'image'
/**
* Whole image reference.
*
* ```markdown
* > | ![a]
* ^^^^
* ```
*/
imageReference: 'imageReference'
/**
* Label (occurs in definitions, image reference, image, link reference,
* link).
*
* ```markdown
* > | [a]: b "c"
* ^^^
* > | a [b] c
* ^^^
* > | a ![b][c] d
* ^^^^
* > | a [b](c) d
* ^^^
* ```
*/
label: 'label'
/**
* Whole link.
*
* ```markdown
* > | [a](b)
* ^^^^^^
* > | [c]
* ^^^
* ```
*/
link: 'link'
/**
* Whole link reference.
*
* ```markdown
* > | [a]
* ^^^
* ```
*/
linkReference: 'linkReference'
/**
* List.
*
* ```markdown
* > | * a
* ^^^
* > | 1. b
* ^^^^
* ```
*/
list: 'list'
/**
* List item.
*
* ```markdown
* > | * a
* ^^^
* > | 1. b
* ^^^^
* ```
*/
listItem: 'listItem'
/**
* Paragraph.
*
* ```markdown
* > | a b
* ^^^
* > | c.
* ^^
* ```
*/
paragraph: 'paragraph'
/**
* Phrasing (occurs in headings, paragraphs, etc).
*
* ```markdown
* > | a
* ^
* ```
*/
phrasing: 'phrasing'
/**
* Reference (occurs in image, link).
*
* ```markdown
* > | [a][]
* ^^
* ```
*/
reference: 'reference'
/**
* Strong.
*
* ```markdown
* > | **a**
* ^^^^^
* ```
*/
strong: 'strong'
/**
* Title using single quotes (occurs in definition, image, link).
*
* ```markdown
* > | [a](b 'c')
* ^^^
* ```
*/
titleApostrophe: 'titleApostrophe'
/**
* Title using double quotes (occurs in definition, image, link).
*
* ```markdown
* > | [a](b "c")
* ^^^
* ```
*/
titleQuote: 'titleQuote'
}

/**
* Construct names for things generated by `mdast-util-to-markdown`.
*
* This is an enum of strings, each being a semantic label, useful to know when
* serializing whether we’re for example in a double (`"`) or single (`'`)
* quoted title.
*/
export type ConstructName = ConstructNameMap[keyof ConstructNameMap]

export {toMarkdown} from './lib/index.js'
export type {
Handle,
Handlers,
Info,
Join,
Options,
State,
Unsafe,
Map
} from './lib/types.js'
// Deprecated.
export type SafeOptions = Info
export type Context = State
20 changes: 0 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
/**
* @typedef {import('./lib/types.js').Info} SafeOptions
* To do: remove next major: renamed because it doesn’t really reflect
* options, but instead info on the surrounding of the generated thing.
* @typedef {import('./lib/types.js').State} Context
* To do: remove next major: renamed because the word state more clearly
* represents one thing that is passed down to everything and is changed.
*/

/**
* @typedef {import('./lib/types.js').Handle} Handle
* @typedef {import('./lib/types.js').Handlers} Handlers
* @typedef {import('./lib/types.js').Info} Info
* @typedef {import('./lib/types.js').Join} Join
* @typedef {import('./lib/types.js').Options} Options
* @typedef {import('./lib/types.js').State} State
* @typedef {import('./lib/types.js').Unsafe} Unsafe
* @typedef {import('./lib/util/indent-lines.js').Map} Map
*/

export {toMarkdown} from './lib/index.js'
4 changes: 2 additions & 2 deletions lib/handle/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function code(node, _, state, info) {
let value = tracker.move(sequence)

if (node.lang) {
const subexit = state.enter('codeFencedLang' + suffix)
const subexit = state.enter(`codeFencedLang${suffix}`)
value += tracker.move(
safe(state, node.lang, {
before: value,
Expand All @@ -51,7 +51,7 @@ export function code(node, _, state, info) {
}

if (node.lang && node.meta) {
const subexit = state.enter('codeFencedMeta' + suffix)
const subexit = state.enter(`codeFencedMeta${suffix}`)
value += tracker.move(' ')
value += tracker.move(
safe(state, node.meta, {
Expand Down
2 changes: 1 addition & 1 deletion lib/handle/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function definition(node, _, state, info) {
subexit()

if (node.title) {
subexit = state.enter('title' + suffix)
subexit = state.enter(`title${suffix}`)
value += tracker.move(' ' + quote)
value += tracker.move(
safe(state, node.title, {
Expand Down
2 changes: 1 addition & 1 deletion lib/handle/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function image(node, _, state, info) {
subexit()

if (node.title) {
subexit = state.enter('title' + suffix)
subexit = state.enter(`title${suffix}`)
value += tracker.move(' ' + quote)
value += tracker.move(
safe(state, node.title, {
Expand Down

0 comments on commit 35a9ccc

Please sign in to comment.