micromark-util-subtokenize
Advanced tools
Comparing version 1.0.2 to 1.1.0
/** | ||
* Tokenize subcontent. | ||
* | ||
* @param {Event[]} events | ||
* @param {Array<Event>} events | ||
* List of events. | ||
* @returns {boolean} | ||
* Whether subtokens were found. | ||
*/ | ||
export function subtokenize( | ||
events: import('micromark-util-types').Event[] | ||
events: Array<import('micromark-util-types').Event> | ||
): boolean | ||
export type Token = import('micromark-util-types').Token | ||
export type Chunk = import('micromark-util-types').Chunk | ||
export type Event = import('micromark-util-types').Event | ||
export type Token = import('micromark-util-types').Token |
/** | ||
* @typedef {import('micromark-util-types').Token} Token | ||
* @typedef {import('micromark-util-types').Chunk} Chunk | ||
* @typedef {import('micromark-util-types').Event} Event | ||
* @typedef {import('micromark-util-types').Token} Token | ||
*/ | ||
import {ok as assert} from 'uvu/assert' | ||
import {splice} from 'micromark-util-chunked' | ||
import {codes} from 'micromark-util-symbol/codes.js' | ||
import {types} from 'micromark-util-symbol/types.js' | ||
import {ok as assert} from 'uvu/assert' | ||
@@ -15,4 +15,6 @@ /** | ||
* | ||
* @param {Event[]} events | ||
* @param {Array<Event>} events | ||
* List of events. | ||
* @returns {boolean} | ||
* Whether subtokens were found. | ||
*/ | ||
@@ -25,3 +27,3 @@ export function subtokenize(events) { | ||
let event | ||
/** @type {number|undefined} */ | ||
/** @type {number | undefined} */ | ||
let lineIndex | ||
@@ -32,7 +34,7 @@ /** @type {number} */ | ||
let otherEvent | ||
/** @type {Event[]} */ | ||
/** @type {Array<Event>} */ | ||
let parameters | ||
/** @type {Event[]} */ | ||
/** @type {Array<Event>} */ | ||
let subevents | ||
/** @type {boolean|undefined} */ | ||
/** @type {boolean | undefined} */ | ||
let more | ||
@@ -133,3 +135,3 @@ | ||
* | ||
* @param {Event[]} events | ||
* @param {Array<Event>} events | ||
* @param {number} eventIndex | ||
@@ -142,3 +144,3 @@ * @returns {Record<string, number>} | ||
let startPosition = eventIndex - 1 | ||
/** @type {number[]} */ | ||
/** @type {Array<number>} */ | ||
const startPositions = [] | ||
@@ -149,12 +151,12 @@ assert(token.contentType, 'expected `contentType` on subtokens') | ||
const childEvents = tokenizer.events | ||
/** @type {[number, number][]} */ | ||
/** @type {Array<[number, number]>} */ | ||
const jumps = [] | ||
/** @type {Record<string, number>} */ | ||
const gaps = {} | ||
/** @type {Chunk[]} */ | ||
/** @type {Array<Chunk>} */ | ||
let stream | ||
/** @type {Token|undefined} */ | ||
/** @type {Token | undefined} */ | ||
let previous | ||
let index = -1 | ||
/** @type {Token|undefined} */ | ||
/** @type {Token | undefined} */ | ||
let current = token | ||
@@ -161,0 +163,0 @@ let adjust = 0 |
/** | ||
* Tokenize subcontent. | ||
* | ||
* @param {Event[]} events | ||
* @param {Array<Event>} events | ||
* List of events. | ||
* @returns {boolean} | ||
* Whether subtokens were found. | ||
*/ | ||
export function subtokenize( | ||
events: import('micromark-util-types').Event[] | ||
events: Array<import('micromark-util-types').Event> | ||
): boolean | ||
export type Token = import('micromark-util-types').Token | ||
export type Chunk = import('micromark-util-types').Chunk | ||
export type Event = import('micromark-util-types').Event | ||
export type Token = import('micromark-util-types').Token |
107
index.js
/** | ||
* @typedef {import('micromark-util-types').Token} Token | ||
* @typedef {import('micromark-util-types').Chunk} Chunk | ||
* @typedef {import('micromark-util-types').Event} Event | ||
* @typedef {import('micromark-util-types').Token} Token | ||
*/ | ||
import {splice} from 'micromark-util-chunked' | ||
/** | ||
* Tokenize subcontent. | ||
* | ||
* @param {Event[]} events | ||
* @param {Array<Event>} events | ||
* List of events. | ||
* @returns {boolean} | ||
* Whether subtokens were found. | ||
*/ | ||
@@ -19,23 +21,15 @@ export function subtokenize(events) { | ||
/** @type {Event} */ | ||
let event | ||
/** @type {number|undefined} */ | ||
/** @type {number | undefined} */ | ||
let lineIndex | ||
/** @type {number} */ | ||
let otherIndex | ||
/** @type {Event} */ | ||
let otherEvent | ||
/** @type {Event[]} */ | ||
/** @type {Array<Event>} */ | ||
let parameters | ||
/** @type {Event[]} */ | ||
/** @type {Array<Event>} */ | ||
let subevents | ||
/** @type {boolean|undefined} */ | ||
/** @type {boolean | undefined} */ | ||
let more | ||
while (++index < events.length) { | ||
@@ -45,6 +39,6 @@ while (index in jumps) { | ||
} | ||
event = events[index] | ||
event = events[index] // Add a hook for the GFM tasklist extension, which needs to know if text | ||
// Add a hook for the GFM tasklist extension, which needs to know if text | ||
// is in the first content of a list item. | ||
if ( | ||
@@ -57,3 +51,2 @@ index && | ||
otherIndex = 0 | ||
if ( | ||
@@ -65,3 +58,2 @@ otherIndex < subevents.length && | ||
} | ||
if ( | ||
@@ -75,3 +67,2 @@ otherIndex < subevents.length && | ||
} | ||
if (subevents[otherIndex][1].type === 'chunkText') { | ||
@@ -83,4 +74,5 @@ subevents[otherIndex][1]._isInFirstContentOfListItem = true | ||
} | ||
} // Enter. | ||
} | ||
// Enter. | ||
if (event[0] === 'enter') { | ||
@@ -92,10 +84,9 @@ if (event[1].contentType) { | ||
} | ||
} // Exit. | ||
} | ||
// Exit. | ||
else if (event[1]._container) { | ||
otherIndex = index | ||
lineIndex = undefined | ||
while (otherIndex--) { | ||
otherEvent = events[otherIndex] | ||
if ( | ||
@@ -109,3 +100,2 @@ otherEvent[1].type === 'lineEnding' || | ||
} | ||
otherEvent[1].type = 'lineEnding' | ||
@@ -118,7 +108,7 @@ lineIndex = otherIndex | ||
} | ||
if (lineIndex) { | ||
// Fix position. | ||
event[1].end = Object.assign({}, events[lineIndex][1].start) // Switch container exit w/ line endings. | ||
event[1].end = Object.assign({}, events[lineIndex][1].start) | ||
// Switch container exit w/ line endings. | ||
parameters = events.slice(lineIndex, index) | ||
@@ -130,13 +120,12 @@ parameters.unshift(event) | ||
} | ||
return !more | ||
} | ||
/** | ||
* Tokenize embedded tokens. | ||
* | ||
* @param {Event[]} events | ||
* @param {Array<Event>} events | ||
* @param {number} eventIndex | ||
* @returns {Record<string, number>} | ||
*/ | ||
function subcontent(events, eventIndex) { | ||
@@ -146,4 +135,3 @@ const token = events[eventIndex][1] | ||
let startPosition = eventIndex - 1 | ||
/** @type {number[]} */ | ||
/** @type {Array<number>} */ | ||
const startPositions = [] | ||
@@ -153,23 +141,19 @@ const tokenizer = | ||
const childEvents = tokenizer.events | ||
/** @type {[number, number][]} */ | ||
/** @type {Array<[number, number]>} */ | ||
const jumps = [] | ||
/** @type {Record<string, number>} */ | ||
const gaps = {} | ||
/** @type {Chunk[]} */ | ||
/** @type {Array<Chunk>} */ | ||
let stream | ||
/** @type {Token|undefined} */ | ||
/** @type {Token | undefined} */ | ||
let previous | ||
let index = -1 | ||
/** @type {Token|undefined} */ | ||
/** @type {Token | undefined} */ | ||
let current = token | ||
let adjust = 0 | ||
let start = 0 | ||
const breaks = [start] // Loop forward through the linked tokens to pass them in order to the | ||
const breaks = [start] | ||
// Loop forward through the linked tokens to pass them in order to the | ||
// subtokenizer. | ||
while (current) { | ||
@@ -180,34 +164,28 @@ // Find the position of the event for this token. | ||
} | ||
startPositions.push(startPosition) | ||
if (!current._tokenizer) { | ||
stream = context.sliceStream(current) | ||
if (!current.next) { | ||
stream.push(null) | ||
} | ||
if (previous) { | ||
tokenizer.defineSkip(current.start) | ||
} | ||
if (current._isInFirstContentOfListItem) { | ||
tokenizer._gfmTasklistFirstContentOfListItem = true | ||
} | ||
tokenizer.write(stream) | ||
if (current._isInFirstContentOfListItem) { | ||
tokenizer._gfmTasklistFirstContentOfListItem = undefined | ||
} | ||
} // Unravel the next token. | ||
} | ||
// Unravel the next token. | ||
previous = current | ||
current = current.next | ||
} // Now, loop back through all events (and linked tokens), to figure out which | ||
} | ||
// Now, loop back through all events (and linked tokens), to figure out which | ||
// parts belong where. | ||
current = token | ||
while (++index < childEvents.length) { | ||
@@ -222,4 +200,4 @@ if ( | ||
start = index + 1 | ||
breaks.push(start) // Help GC. | ||
breaks.push(start) | ||
// Help GC. | ||
current._tokenizer = undefined | ||
@@ -229,8 +207,10 @@ current.previous = undefined | ||
} | ||
} // Help GC. | ||
} | ||
tokenizer.events = [] // If there’s one more token (which is the cases for lines that end in an | ||
// Help GC. | ||
tokenizer.events = [] | ||
// If there’s one more token (which is the cases for lines that end in an | ||
// EOF), that’s perfect: the last point we found starts it. | ||
// If there isn’t then make sure any remaining content is added to it. | ||
if (current) { | ||
@@ -242,7 +222,7 @@ // Help GC. | ||
breaks.pop() | ||
} // Now splice the events from the subtokenizer into the current events, | ||
} | ||
// Now splice the events from the subtokenizer into the current events, | ||
// moving back to front so that splice indices aren’t affected. | ||
index = breaks.length | ||
while (index--) { | ||
@@ -254,5 +234,3 @@ const slice = childEvents.slice(breaks[index], breaks[index + 1]) | ||
} | ||
index = -1 | ||
while (++index < jumps.length) { | ||
@@ -262,4 +240,3 @@ gaps[adjust + jumps[index][0]] = adjust + jumps[index][1] | ||
} | ||
return gaps | ||
} |
{ | ||
"name": "micromark-util-subtokenize", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "micromark utility to tokenize subtokens", | ||
@@ -38,2 +38,3 @@ "license": "MIT", | ||
"exports": { | ||
"types": "./dev/index.d.ts", | ||
"development": "./dev/index.js", | ||
@@ -49,3 +50,3 @@ "default": "./index.js" | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" \"{dev/,lib/}**/*.d.ts\" && tsc && micromark-build && type-coverage" | ||
"build": "micromark-build" | ||
}, | ||
@@ -52,0 +53,0 @@ "xo": false, |
@@ -11,6 +11,8 @@ # micromark-util-subtokenize | ||
micromark utility to tokenize subtokens. | ||
[micromark][] utility to tokenize subtokens. | ||
## 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) | ||
* [`subtokenize(events)`](#subtokenizeevents) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
@@ -25,5 +29,14 @@ * [Contribute](#contribute) | ||
## What is this? | ||
This package exposes a micromark internal that you probably don’t need. | ||
## When should I use this? | ||
This package might be 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 {subtokenize} from 'https://esm.sh/micromark-util-subtokenize@1' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {subtokenize} from 'https://esm.sh/micromark-util-subtokenize@1?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -54,3 +81,3 @@ | ||
This module exports the following identifiers: `subtokenize`. | ||
This module exports the identifiers [`subtokenize`][api-subtokenize]. | ||
There is no default export. | ||
@@ -64,10 +91,26 @@ | ||
* `events` (`Event[]`) — List of events | ||
* `events` (`Array<Event>`) | ||
— list of events | ||
###### Returns | ||
`boolean` — Whether subtokens were found. | ||
Whether subtokens were found (`boolean`). | ||
## 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 | ||
@@ -104,5 +147,5 @@ submit a security report. | ||
[bundle-size-badge]: https://img.shields.io/bundlephobia/minzip/micromark-util-subtokenize.svg | ||
[bundle-size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=micromark-util-subtokenize | ||
[bundle-size]: https://bundlephobia.com/result?p=micromark-util-subtokenize | ||
[bundle-size]: https://bundlejs.com/?q=micromark-util-subtokenize | ||
@@ -117,2 +160,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 | ||
@@ -128,8 +175,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-subtokenize]: #subtokenizeevents |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20022
467
180