@flex-development/docmark-util-subtokenize 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +28 -0
- package/README.md +86 -0
- package/dist/index.d.mts +64 -0
- package/dist/index.mjs +120 -0
- package/package.json +60 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, Flex Development, LLC All rights reserved.
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without modification,
|
|
6
|
+
are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
|
16
|
+
may be used to endorse or promote products derived from this software without
|
|
17
|
+
specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
20
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
21
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
23
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
24
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
25
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
26
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
27
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# docmark-util-subtokenize
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/@flex-development/docmark-util-subtokenize)
|
|
4
|
+
[](https://www.npmcharts.com/compare/@flex-development/docmark-util-subtokenize?interval=30)
|
|
5
|
+
[](https://packagephobia.now.sh/result?p=@flex-development/docmark-util-subtokenize)
|
|
6
|
+
[](https://github.com/voxpelli/badges-cjs-esm)
|
|
7
|
+
|
|
8
|
+
[docmark][] utility to tokenize subtokens.
|
|
9
|
+
|
|
10
|
+
## Contents
|
|
11
|
+
|
|
12
|
+
- [What is this?](#what-is-this)
|
|
13
|
+
- [When should I use this?](#when-should-i-use-this)
|
|
14
|
+
- [Install](#install)
|
|
15
|
+
- [Use](#use)
|
|
16
|
+
- [API](#api)
|
|
17
|
+
- [Types](#types)
|
|
18
|
+
- [Contribute](#contribute)
|
|
19
|
+
|
|
20
|
+
## What is this?
|
|
21
|
+
|
|
22
|
+
**TODO**: what is this?
|
|
23
|
+
|
|
24
|
+
## When should I use this?
|
|
25
|
+
|
|
26
|
+
This package is useful when extending docmark.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
This package is [ESM only][esm].
|
|
31
|
+
|
|
32
|
+
In Node.js with [yarn][]:
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
yarn add @flex-development/docmark-util-subtokenize
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
<blockquote>
|
|
39
|
+
<small>
|
|
40
|
+
See <a href='https://yarnpkg.com/protocol/git'>Git - Protocols | Yarn</a>
|
|
41
|
+
for details regarding installing from Git.
|
|
42
|
+
</small>
|
|
43
|
+
</blockquote>
|
|
44
|
+
|
|
45
|
+
In Deno with [`esm.sh`][esmsh]:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { subcontent, subtokenize } from 'https://esm.sh/@flex-development/docmark-util-subtokenize'
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
In browsers with [`esm.sh`][esmsh]:
|
|
52
|
+
|
|
53
|
+
```html
|
|
54
|
+
<script type="module">
|
|
55
|
+
import { subcontent, subtokenize } from 'https://esm.sh/@flex-development/docmark-util-subtokenize'
|
|
56
|
+
</script>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Use
|
|
60
|
+
|
|
61
|
+
**TODO**: use
|
|
62
|
+
|
|
63
|
+
## API
|
|
64
|
+
|
|
65
|
+
**TODO**: api
|
|
66
|
+
|
|
67
|
+
## Types
|
|
68
|
+
|
|
69
|
+
This package is fully typed with [TypeScript][].
|
|
70
|
+
|
|
71
|
+
## Contribute
|
|
72
|
+
|
|
73
|
+
See [`CONTRIBUTING.md`](../../CONTRIBUTING.md).
|
|
74
|
+
|
|
75
|
+
This project has a [code of conduct](../../CODE_OF_CONDUCT.md).
|
|
76
|
+
By interacting with this repository, organization, or community you agree to abide by its terms.
|
|
77
|
+
|
|
78
|
+
[docmark]: ../../README.md
|
|
79
|
+
|
|
80
|
+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
|
81
|
+
|
|
82
|
+
[esmsh]: https://esm.sh
|
|
83
|
+
|
|
84
|
+
[typescript]: https://www.typescriptlang.org
|
|
85
|
+
|
|
86
|
+
[yarn]: https://yarnpkg.com
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Event } from '@flex-development/docmark-util-types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file subcontent
|
|
5
|
+
* @module docmark-util-subtokenize/subcontent
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Tokenize embedded content for a single token.
|
|
10
|
+
*
|
|
11
|
+
* This algorithm has three phases:
|
|
12
|
+
*
|
|
13
|
+
* 1. Feed linked chunk tokens to a child tokenizer
|
|
14
|
+
* 2. Determine which child events belong to each linked token
|
|
15
|
+
* 3. Replace `chunk*` events with their corresponding child events
|
|
16
|
+
*
|
|
17
|
+
* @this {void}
|
|
18
|
+
*
|
|
19
|
+
* @param {Event[]} events
|
|
20
|
+
* The parent event stream
|
|
21
|
+
* @param {number} eventIndex
|
|
22
|
+
* The index of the first chunk `enter` event
|
|
23
|
+
* @return {undefined}
|
|
24
|
+
*/
|
|
25
|
+
declare function subcontent(this: void, events: Event[], eventIndex: number): undefined;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @file subtokenize
|
|
29
|
+
* @module docmark-util-subtokenize/subtokenize
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Tokenize embedded content.
|
|
34
|
+
*
|
|
35
|
+
* Some tokens declare a {@linkcode ContentType}.
|
|
36
|
+
* These tokens do not contain fully parsed content themselves.
|
|
37
|
+
* Tokens with a `chunk*` (i.e. `chunkMarkdown`, `chunkDocument`, `chunkFlow`)
|
|
38
|
+
* type and a `contentType` act as containers for another tokenizer.
|
|
39
|
+
*
|
|
40
|
+
* For example:
|
|
41
|
+
*
|
|
42
|
+
* ```text
|
|
43
|
+
* comment
|
|
44
|
+
* └─ chunkComment
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* A `chunkComment` token may contain markdown, block tags, inline tags, and
|
|
48
|
+
* other syntax. This function replaces those chunk tokens with the events
|
|
49
|
+
* produced by their child tokenizer.
|
|
50
|
+
*
|
|
51
|
+
* Linked chunks are handled as one logical stream, but child events are spliced
|
|
52
|
+
* back into the same positions as the original chunk tokens. This preserves
|
|
53
|
+
* surrounding events such as ones for `commentLinePrefix`.
|
|
54
|
+
*
|
|
55
|
+
* @this {void}
|
|
56
|
+
*
|
|
57
|
+
* @param {Event[]} events
|
|
58
|
+
* The current list of events
|
|
59
|
+
* @return {boolean}
|
|
60
|
+
* Whether subtokens (embedded content) were found
|
|
61
|
+
*/
|
|
62
|
+
declare function subtokenize(this: void, events: Event[]): boolean;
|
|
63
|
+
|
|
64
|
+
export { subcontent, subtokenize };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { codes, ev } from '@flex-development/docmark-util-symbol';
|
|
2
|
+
import { splice } from '@flex-development/mark-util-chunked';
|
|
3
|
+
import { ok } from 'devlop';
|
|
4
|
+
|
|
5
|
+
function eolset(code, start) {
|
|
6
|
+
return start + (code === codes.carriageReturnLineFeed ? 2 : 1);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function subcontent(events, eventIndex) {
|
|
10
|
+
ok(events[eventIndex], 'expected `events[eventIndex]`');
|
|
11
|
+
const [event, token, context] = events[eventIndex];
|
|
12
|
+
ok(event === ev.enter, 'expected enter event');
|
|
13
|
+
ok(token.contentType, 'expected `token.contentType`');
|
|
14
|
+
ok(/^chunk[A-Z][a-z]+$/.test(token.type), 'expected chunk token');
|
|
15
|
+
const child = token._tokenizer && 'write' in token._tokenizer
|
|
16
|
+
? token._tokenizer
|
|
17
|
+
: context.parser[token.contentType](token.start);
|
|
18
|
+
const positions = [];
|
|
19
|
+
let current = token;
|
|
20
|
+
let position = eventIndex - 1;
|
|
21
|
+
let previous;
|
|
22
|
+
while (current) {
|
|
23
|
+
while (++position < events.length) {
|
|
24
|
+
ok(events[position], 'expected `events[j]`');
|
|
25
|
+
const [event, eventToken] = events[position];
|
|
26
|
+
if (event === ev.enter && eventToken === current) {
|
|
27
|
+
positions.push(position);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
ok(position < events.length, 'expected `enter` event for linked token');
|
|
32
|
+
ok(!previous || current.previous === previous, 'expected previous to match');
|
|
33
|
+
ok(!previous || previous.next === current, 'expected next to match');
|
|
34
|
+
if (!current._tokenizer) {
|
|
35
|
+
const chunks = context.sliceStream(current);
|
|
36
|
+
if (current.previous)
|
|
37
|
+
child.defineSkip(current.start);
|
|
38
|
+
if (!current.next)
|
|
39
|
+
chunks.push(codes.eos);
|
|
40
|
+
void child.write(chunks);
|
|
41
|
+
}
|
|
42
|
+
previous = current;
|
|
43
|
+
current = current.next;
|
|
44
|
+
}
|
|
45
|
+
const seen = new WeakSet();
|
|
46
|
+
let childIndex = child.events.length - 1;
|
|
47
|
+
for (let i = positions.length - 1; i >= 0; i--) {
|
|
48
|
+
ok(positions[i] !== undefined, 'expected `enter` index for linked token');
|
|
49
|
+
position = positions[i];
|
|
50
|
+
const bucket = [];
|
|
51
|
+
const linked = events[position][1];
|
|
52
|
+
ok(linked, 'expected `linked` token');
|
|
53
|
+
ok(linked.contentType, 'expected linked token with `contentType`');
|
|
54
|
+
for (let j = childIndex; j >= 0; j--) {
|
|
55
|
+
ok(child.events[j], 'expected `child.events[j]`');
|
|
56
|
+
const [event, token, self] = child.events[j];
|
|
57
|
+
if (token.start.line === token.end.line) {
|
|
58
|
+
ok(token.start.offset >= linked.start.offset, 'expected same line token to start after or at linked chunk start');
|
|
59
|
+
ok(token.end.offset <= linked.end.offset, 'expected same line token to end before or at linked chunk end');
|
|
60
|
+
bucket.unshift([event, token, self]);
|
|
61
|
+
seen.add(token);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (token.start._bufferIndex < 0 &&
|
|
65
|
+
token.end._bufferIndex < 0 &&
|
|
66
|
+
token.end.offset !== token.start.offset) {
|
|
67
|
+
if (seen.has(token)) {
|
|
68
|
+
ok(event === ev.enter, 'expected line ending `enter` event');
|
|
69
|
+
bucket.unshift([event, token, self]);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (token.end.column !== 1) {
|
|
73
|
+
const stream = self.sliceStream(token);
|
|
74
|
+
ok(stream.length === 1, 'expected `token` to span one chunk');
|
|
75
|
+
ok(stream[0] !== undefined, 'expected chunk');
|
|
76
|
+
ok(stream[0] !== codes.eos, 'expected no end of stream code');
|
|
77
|
+
ok(typeof stream[0] === 'number', 'expected character code chunk');
|
|
78
|
+
token.end.column = 1;
|
|
79
|
+
token.end.offset = eolset(stream[0], token.start.offset);
|
|
80
|
+
}
|
|
81
|
+
ok(event === ev.exit, 'expected line ending `exit` event');
|
|
82
|
+
}
|
|
83
|
+
if (event === ev.exit) {
|
|
84
|
+
if (
|
|
85
|
+
token.end.offset <= linked.start.offset ||
|
|
86
|
+
token.end.offset > linked.end.offset) {
|
|
87
|
+
childIndex = j;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
bucket.unshift([event, token, self]);
|
|
91
|
+
seen.add(token);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
ok(seen.has(token), 'expected to have seen multiline `token`');
|
|
95
|
+
bucket.unshift([event, token, self]);
|
|
96
|
+
}
|
|
97
|
+
splice(events, position, 2, bucket);
|
|
98
|
+
}
|
|
99
|
+
child.events = [];
|
|
100
|
+
return void child.events;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function subtokenize(events) {
|
|
104
|
+
let found = false;
|
|
105
|
+
let index = -1;
|
|
106
|
+
while (++index < events.length) {
|
|
107
|
+
ok(events[index], 'expected `events[index]`');
|
|
108
|
+
const [event, token] = events[index];
|
|
109
|
+
if (event !== ev.enter)
|
|
110
|
+
continue;
|
|
111
|
+
if (!token.contentType)
|
|
112
|
+
continue;
|
|
113
|
+
subcontent(events, index);
|
|
114
|
+
found = true;
|
|
115
|
+
index--;
|
|
116
|
+
}
|
|
117
|
+
return !found;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { subcontent, subtokenize };
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flex-development/docmark-util-subtokenize",
|
|
3
|
+
"description": "docmark utility to tokenize subtokens",
|
|
4
|
+
"version": "1.0.0-alpha.1",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"docmark",
|
|
7
|
+
"tokenize",
|
|
8
|
+
"typescript",
|
|
9
|
+
"util",
|
|
10
|
+
"utility"
|
|
11
|
+
],
|
|
12
|
+
"license": "BSD-3-Clause",
|
|
13
|
+
"homepage": "https://github.com/flex-development/docmark-util-types/tree/main/packages/flex-development/docmark-util-subtokenize",
|
|
14
|
+
"repository": {
|
|
15
|
+
"directory": "packages/docmark-util-subtokenize",
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/flex-development/docmark.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": "https://github.com/flex-development/docmark/issues",
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Lexus Drumgold",
|
|
22
|
+
"url": "https://github.com/unicornware"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"diff-dst-prefix": "docmark-util-subtokenize",
|
|
27
|
+
"diff-src-prefix": "docmark-util-subtokenize",
|
|
28
|
+
"directory": "./",
|
|
29
|
+
"executableFiles": [],
|
|
30
|
+
"node-options": null,
|
|
31
|
+
"pack-destination": ".",
|
|
32
|
+
"parseable": true,
|
|
33
|
+
"prefer-dedupe": true,
|
|
34
|
+
"provenance": true,
|
|
35
|
+
"tag-version-prefix": "docmark-util-subtokenize"
|
|
36
|
+
},
|
|
37
|
+
"type": "module",
|
|
38
|
+
"files": [
|
|
39
|
+
"LICENSE.md",
|
|
40
|
+
"README.md",
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"docmark": "./src/index.mts",
|
|
46
|
+
"default": "./dist/index.mjs"
|
|
47
|
+
},
|
|
48
|
+
"./package.json": "./package.json"
|
|
49
|
+
},
|
|
50
|
+
"module": "./dist/index.mjs",
|
|
51
|
+
"types": "./dist/index.d.mts",
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@flex-development/docmark-util-symbol": "1.0.0-alpha.1",
|
|
54
|
+
"@flex-development/docmark-util-types": "1.0.0-alpha.1",
|
|
55
|
+
"@flex-development/mark-util-chunked": "1.0.0",
|
|
56
|
+
"devlop": "1.1.0"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": false,
|
|
59
|
+
"build": "524f7d653272a777cde27efaac4656fb90cd4855"
|
|
60
|
+
}
|