micromark-factory-label
Advanced tools
Comparing version 1.0.2 to 1.1.0
/** | ||
* Parse labels. | ||
* | ||
* > 👉 **Note**: labels in markdown are capped at 999 characters in the string. | ||
* | ||
* ###### Examples | ||
* | ||
* ```markdown | ||
* [a] | ||
* [a | ||
* b] | ||
* [a\]b] | ||
* ``` | ||
* | ||
* @this {TokenizeContext} | ||
* Tokenize context. | ||
* @param {Effects} effects | ||
* Context. | ||
* @param {State} ok | ||
* State switched to when successful. | ||
* @param {State} nok | ||
* @param {string} type | ||
* @param {string} markerType | ||
* @param {string} stringType | ||
* State switched to when unsuccessful. | ||
* @param {TokenType} type | ||
* Type of the whole label (`[a]`). | ||
* @param {TokenType} markerType | ||
* Type for the markers (`[` and `]`). | ||
* @param {TokenType} stringType | ||
* Type for the identifier (`a`). | ||
* @returns {State} | ||
* Start state. | ||
*/ | ||
export function factoryLabel( | ||
this: import('micromark-util-types').TokenizeContext, | ||
effects: Effects, | ||
ok: State, | ||
nok: State, | ||
type: string, | ||
markerType: string, | ||
stringType: string | ||
type: TokenType, | ||
markerType: TokenType, | ||
stringType: TokenType | ||
): State | ||
export type Effects = import('micromark-util-types').Effects | ||
export type State = import('micromark-util-types').State | ||
export type TokenizeContext = import('micromark-util-types').TokenizeContext | ||
export type State = import('micromark-util-types').State | ||
export type TokenType = import('micromark-util-types').TokenType |
105
dev/index.js
/** | ||
* @typedef {import('micromark-util-types').Effects} Effects | ||
* @typedef {import('micromark-util-types').State} State | ||
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext | ||
* @typedef {import('micromark-util-types').State} State | ||
* @typedef {import('micromark-util-types').TokenType} TokenType | ||
*/ | ||
import {ok as assert} from 'uvu/assert' | ||
import {markdownLineEnding, markdownSpace} from 'micromark-util-character' | ||
@@ -12,12 +12,34 @@ import {codes} from 'micromark-util-symbol/codes.js' | ||
import {types} from 'micromark-util-symbol/types.js' | ||
import {ok as assert} from 'uvu/assert' | ||
/** | ||
* Parse labels. | ||
* | ||
* > 👉 **Note**: labels in markdown are capped at 999 characters in the string. | ||
* | ||
* ###### Examples | ||
* | ||
* ```markdown | ||
* [a] | ||
* [a | ||
* b] | ||
* [a\]b] | ||
* ``` | ||
* | ||
* @this {TokenizeContext} | ||
* Tokenize context. | ||
* @param {Effects} effects | ||
* Context. | ||
* @param {State} ok | ||
* State switched to when successful. | ||
* @param {State} nok | ||
* @param {string} type | ||
* @param {string} markerType | ||
* @param {string} stringType | ||
* State switched to when unsuccessful. | ||
* @param {TokenType} type | ||
* Type of the whole label (`[a]`). | ||
* @param {TokenType} markerType | ||
* Type for the markers (`[` and `]`). | ||
* @param {TokenType} stringType | ||
* Type for the identifier (`a`). | ||
* @returns {State} | ||
* Start state. | ||
*/ | ||
@@ -29,7 +51,16 @@ // eslint-disable-next-line max-params | ||
/** @type {boolean} */ | ||
let data | ||
let seen | ||
return start | ||
/** @type {State} */ | ||
/** | ||
* Start of label. | ||
* | ||
* ```markdown | ||
* > | [a] | ||
* ^ | ||
* ``` | ||
* | ||
* @type {State} | ||
*/ | ||
function start(code) { | ||
@@ -45,17 +76,26 @@ assert(code === codes.leftSquareBracket, 'expected `[`') | ||
/** @type {State} */ | ||
/** | ||
* In label, at something, before something else. | ||
* | ||
* ```markdown | ||
* > | [a] | ||
* ^ | ||
* ``` | ||
* | ||
* @type {State} | ||
*/ | ||
function atBreak(code) { | ||
if ( | ||
size > constants.linkReferenceSizeMax || | ||
code === codes.eof || | ||
code === codes.leftSquareBracket || | ||
(code === codes.rightSquareBracket && !data) || | ||
/* To do: remove in the future once we’ve switched from | ||
* `micromark-extension-footnote` to `micromark-extension-gfm-footnote`, | ||
* which doesn’t need this */ | ||
/* Hidden footnotes hook */ | ||
(code === codes.rightSquareBracket && !seen) || | ||
// To do: remove in the future once we’ve switched from | ||
// `micromark-extension-footnote` to `micromark-extension-gfm-footnote`, | ||
// which doesn’t need this. | ||
// Hidden footnotes hook. | ||
/* c8 ignore next 3 */ | ||
(code === codes.caret && | ||
!size && | ||
'_hiddenFootnoteSupport' in self.parser.constructs) || | ||
size > constants.linkReferenceSizeMax | ||
'_hiddenFootnoteSupport' in self.parser.constructs) | ||
) { | ||
@@ -74,2 +114,3 @@ return nok(code) | ||
// To do: indent? Link chunks and EOLs together? | ||
if (markdownLineEnding(code)) { | ||
@@ -83,7 +124,16 @@ effects.enter(types.lineEnding) | ||
effects.enter(types.chunkString, {contentType: constants.contentTypeString}) | ||
return label(code) | ||
return labelInside(code) | ||
} | ||
/** @type {State} */ | ||
function label(code) { | ||
/** | ||
* In label, in text. | ||
* | ||
* ```markdown | ||
* > | [a] | ||
* ^ | ||
* ``` | ||
* | ||
* @type {State} | ||
*/ | ||
function labelInside(code) { | ||
if ( | ||
@@ -101,7 +151,16 @@ code === codes.eof || | ||
effects.consume(code) | ||
data = data || !markdownSpace(code) | ||
return code === codes.backslash ? labelEscape : label | ||
if (!seen) seen = !markdownSpace(code) | ||
return code === codes.backslash ? labelEscape : labelInside | ||
} | ||
/** @type {State} */ | ||
/** | ||
* After `\`, at a special character. | ||
* | ||
* ```markdown | ||
* > | [a\*a] | ||
* ^ | ||
* ``` | ||
* | ||
* @type {State} | ||
*/ | ||
function labelEscape(code) { | ||
@@ -115,7 +174,7 @@ if ( | ||
size++ | ||
return label | ||
return labelInside | ||
} | ||
return label(code) | ||
return labelInside(code) | ||
} | ||
} |
/** | ||
* Parse labels. | ||
* | ||
* > 👉 **Note**: labels in markdown are capped at 999 characters in the string. | ||
* | ||
* ###### Examples | ||
* | ||
* ```markdown | ||
* [a] | ||
* [a | ||
* b] | ||
* [a\]b] | ||
* ``` | ||
* | ||
* @this {TokenizeContext} | ||
* Tokenize context. | ||
* @param {Effects} effects | ||
* Context. | ||
* @param {State} ok | ||
* State switched to when successful. | ||
* @param {State} nok | ||
* @param {string} type | ||
* @param {string} markerType | ||
* @param {string} stringType | ||
* State switched to when unsuccessful. | ||
* @param {TokenType} type | ||
* Type of the whole label (`[a]`). | ||
* @param {TokenType} markerType | ||
* Type for the markers (`[` and `]`). | ||
* @param {TokenType} stringType | ||
* Type for the identifier (`a`). | ||
* @returns {State} | ||
* Start state. | ||
*/ | ||
export function factoryLabel( | ||
this: import('micromark-util-types').TokenizeContext, | ||
effects: Effects, | ||
ok: State, | ||
nok: State, | ||
type: string, | ||
markerType: string, | ||
stringType: string | ||
type: TokenType, | ||
markerType: TokenType, | ||
stringType: TokenType | ||
): State | ||
export type Effects = import('micromark-util-types').Effects | ||
export type State = import('micromark-util-types').State | ||
export type TokenizeContext = import('micromark-util-types').TokenizeContext | ||
export type State = import('micromark-util-types').State | ||
export type TokenType = import('micromark-util-types').TokenType |
115
index.js
/** | ||
* @typedef {import('micromark-util-types').Effects} Effects | ||
* @typedef {import('micromark-util-types').State} State | ||
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext | ||
* @typedef {import('micromark-util-types').State} State | ||
* @typedef {import('micromark-util-types').TokenType} TokenType | ||
*/ | ||
import {markdownLineEnding, markdownSpace} from 'micromark-util-character' | ||
/** | ||
* Parse labels. | ||
* | ||
* > 👉 **Note**: labels in markdown are capped at 999 characters in the string. | ||
* | ||
* ###### Examples | ||
* | ||
* ```markdown | ||
* [a] | ||
* [a | ||
* b] | ||
* [a\]b] | ||
* ``` | ||
* | ||
* @this {TokenizeContext} | ||
* Tokenize context. | ||
* @param {Effects} effects | ||
* Context. | ||
* @param {State} ok | ||
* State switched to when successful. | ||
* @param {State} nok | ||
* @param {string} type | ||
* @param {string} markerType | ||
* @param {string} stringType | ||
* State switched to when unsuccessful. | ||
* @param {TokenType} type | ||
* Type of the whole label (`[a]`). | ||
* @param {TokenType} markerType | ||
* Type for the markers (`[` and `]`). | ||
* @param {TokenType} stringType | ||
* Type for the identifier (`a`). | ||
* @returns {State} | ||
*/ | ||
// eslint-disable-next-line max-params | ||
* Start state. | ||
*/ // eslint-disable-next-line max-params | ||
export function factoryLabel(effects, ok, nok, type, markerType, stringType) { | ||
@@ -23,7 +44,15 @@ const self = this | ||
/** @type {boolean} */ | ||
let data | ||
let seen | ||
return start | ||
/** @type {State} */ | ||
/** | ||
* Start of label. | ||
* | ||
* ```markdown | ||
* > | [a] | ||
* ^ | ||
* ``` | ||
* | ||
* @type {State} | ||
*/ | ||
function start(code) { | ||
@@ -37,24 +66,30 @@ effects.enter(type) | ||
} | ||
/** @type {State} */ | ||
/** | ||
* In label, at something, before something else. | ||
* | ||
* ```markdown | ||
* > | [a] | ||
* ^ | ||
* ``` | ||
* | ||
* @type {State} | ||
*/ | ||
function atBreak(code) { | ||
if ( | ||
size > 999 || | ||
code === null || | ||
code === 91 || | ||
(code === 93 && !data) || | ||
/* To do: remove in the future once we’ve switched from | ||
* `micromark-extension-footnote` to `micromark-extension-gfm-footnote`, | ||
* which doesn’t need this */ | ||
/* Hidden footnotes hook */ | ||
(code === 93 && !seen) || | ||
// To do: remove in the future once we’ve switched from | ||
// `micromark-extension-footnote` to `micromark-extension-gfm-footnote`, | ||
// which doesn’t need this. | ||
// Hidden footnotes hook. | ||
/* c8 ignore next 3 */ | ||
(code === 94 && | ||
!size && | ||
'_hiddenFootnoteSupport' in self.parser.constructs) || | ||
size > 999 | ||
'_hiddenFootnoteSupport' in self.parser.constructs) | ||
) { | ||
return nok(code) | ||
} | ||
if (code === 93) { | ||
@@ -69,2 +104,3 @@ effects.exit(stringType) | ||
// To do: indent? Link chunks and EOLs together? | ||
if (markdownLineEnding(code)) { | ||
@@ -76,11 +112,19 @@ effects.enter('lineEnding') | ||
} | ||
effects.enter('chunkString', { | ||
contentType: 'string' | ||
}) | ||
return label(code) | ||
return labelInside(code) | ||
} | ||
/** @type {State} */ | ||
function label(code) { | ||
/** | ||
* In label, in text. | ||
* | ||
* ```markdown | ||
* > | [a] | ||
* ^ | ||
* ``` | ||
* | ||
* @type {State} | ||
*/ | ||
function labelInside(code) { | ||
if ( | ||
@@ -96,9 +140,17 @@ code === null || | ||
} | ||
effects.consume(code) | ||
data = data || !markdownSpace(code) | ||
return code === 92 ? labelEscape : label | ||
if (!seen) seen = !markdownSpace(code) | ||
return code === 92 ? labelEscape : labelInside | ||
} | ||
/** @type {State} */ | ||
/** | ||
* After `\`, at a special character. | ||
* | ||
* ```markdown | ||
* > | [a\*a] | ||
* ^ | ||
* ``` | ||
* | ||
* @type {State} | ||
*/ | ||
function labelEscape(code) { | ||
@@ -108,7 +160,6 @@ if (code === 91 || code === 92 || code === 93) { | ||
size++ | ||
return label | ||
return labelInside | ||
} | ||
return label(code) | ||
return labelInside(code) | ||
} | ||
} |
{ | ||
"name": "micromark-factory-label", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "micromark factory to parse labels (found in media, definitions)", | ||
@@ -37,2 +37,3 @@ "license": "MIT", | ||
"exports": { | ||
"types": "./dev/index.d.ts", | ||
"development": "./dev/index.js", | ||
@@ -48,3 +49,3 @@ "default": "./index.js" | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" \"{dev/,lib/}**/*.d.ts\" && tsc && micromark-build && type-coverage" | ||
"build": "micromark-build" | ||
}, | ||
@@ -51,0 +52,0 @@ "xo": false, |
112
readme.md
@@ -11,6 +11,8 @@ # micromark-factory-label | ||
micromark factory to parse labels (found in media, definitions). | ||
[micromark][] factory to parse labels (found in media, definitions). | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
@@ -20,2 +22,4 @@ * [Use](#use) | ||
* [`factoryLabel(…)`](#factorylabel) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
@@ -25,5 +29,14 @@ * [Contribute](#contribute) | ||
## What is this? | ||
This package exposes states to parse labels. | ||
## When should I use this? | ||
This package is useful when you are making your own micromark extensions. | ||
## Install | ||
[npm][]: | ||
This package is [ESM only][esm]. | ||
In Node.js (version 16+), install with [npm][]: | ||
@@ -34,2 +47,16 @@ ```sh | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {factoryLabel} from 'https://esm.sh/micromark-factory-label@1' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {factoryLabel} from 'https://esm.sh/micromark-factory-label@1?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -44,3 +71,6 @@ | ||
// A micromark tokenizer that uses the factory: | ||
/** @type {Tokenizer} */ | ||
/** | ||
* @this {TokenizeContext} | ||
* @type {Tokenizer} | ||
*/ | ||
function tokenizeDefinition(effects, ok, nok) { | ||
@@ -72,3 +102,3 @@ return start | ||
This module exports the following identifiers: `factoryLabel`. | ||
This module exports the identifier [`factoryLabel`][api-factory-label]. | ||
There is no default export. | ||
@@ -78,18 +108,6 @@ | ||
Note that labels in markdown are capped at 999 characters in the string. | ||
Parse labels. | ||
###### Parameters | ||
> 👉 **Note**: labels in markdown are capped at 999 characters in the string. | ||
* `this` (`TokenizeContext`) — Tokenize context | ||
* `effects` (`Effects`) — Context | ||
* `ok` (`State`) — State switched to when successful | ||
* `nok` (`State`) — State switched to when not successful | ||
* `type` (`string`) — Token type for whole (`[a]`) | ||
* `markerType` (`string`) — Token type for the markers (`[` and `]`) | ||
* `stringType` (`string`) — Token type for the identifier (`a`) | ||
###### Returns | ||
`State`. | ||
###### Examples | ||
@@ -104,4 +122,40 @@ | ||
###### Parameters | ||
* `this` (`TokenizeContext`) | ||
— tokenize context | ||
* `effects` (`Effects`) | ||
— context | ||
* `ok` (`State`) | ||
— state switched to when successful | ||
* `nok` (`State`) | ||
— state switched to when unsuccessful | ||
* `type` (`string`) | ||
— type of the whole label (`[a]`) | ||
* `markerType` (`string`) | ||
— type for the markers (`[` and `]`) | ||
* `stringType` (`string`) | ||
— type for the identifier (`a`) | ||
###### Returns | ||
Start state (`State`). | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports no additional types. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 16+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
This package works with `micromark` version 3+. | ||
## Security | ||
This package is safe. | ||
See [`security.md`][securitymd] in [`micromark/.github`][health] for how to | ||
@@ -138,5 +192,5 @@ submit a security report. | ||
[bundle-size-badge]: https://img.shields.io/bundlephobia/minzip/micromark-factory-label.svg | ||
[bundle-size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=micromark-factory-label | ||
[bundle-size]: https://bundlephobia.com/result?p=micromark-factory-label | ||
[bundle-size]: https://bundlejs.com/?q=micromark-factory-label | ||
@@ -151,2 +205,6 @@ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
@@ -162,8 +220,14 @@ | ||
[securitymd]: https://github.com/micromark/.github/blob/HEAD/security.md | ||
[securitymd]: https://github.com/micromark/.github/blob/main/security.md | ||
[contributing]: https://github.com/micromark/.github/blob/HEAD/contributing.md | ||
[contributing]: https://github.com/micromark/.github/blob/main/contributing.md | ||
[support]: https://github.com/micromark/.github/blob/HEAD/support.md | ||
[support]: https://github.com/micromark/.github/blob/main/support.md | ||
[coc]: https://github.com/micromark/.github/blob/HEAD/code-of-conduct.md | ||
[coc]: https://github.com/micromark/.github/blob/main/code-of-conduct.md | ||
[typescript]: https://www.typescriptlang.org | ||
[micromark]: https://github.com/micromark/micromark | ||
[api-factory-label]: #factorylabel |
16539
402
223