0
# micromark-util-resolve-all
1
2
micromark-util-resolve-all is a utility for the micromark parser ecosystem that provides functionality to resolve subtokens through event manipulation. This utility is essential for handling complex markdown structures like media (links, images) and attention (bold, italic) that aren't parsed left-to-right but require post-processing to match openings with closings and convert leftovers to plain text.
3
4
## Package Information
5
6
- **Package Name**: micromark-util-resolve-all
7
- **Package Type**: npm
8
- **Language**: JavaScript with TypeScript type definitions
9
- **Installation**: `npm install micromark-util-resolve-all`
10
11
## Core Imports
12
13
```javascript
14
import { resolveAll } from "micromark-util-resolve-all";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const { resolveAll } = require("micromark-util-resolve-all");
21
```
22
23
For Deno:
24
25
```javascript
26
import { resolveAll } from "https://esm.sh/micromark-util-resolve-all@2";
27
```
28
29
For browsers:
30
31
```html
32
<script type="module">
33
import { resolveAll } from "https://esm.sh/micromark-util-resolve-all@2?bundle";
34
</script>
35
```
36
37
## Basic Usage
38
39
```javascript
40
import { push } from "micromark-util-chunked";
41
import { resolveAll } from "micromark-util-resolve-all";
42
43
/**
44
* Example resolver that processes attention sequences (emphasis/strong)
45
* @type {import('micromark-util-types').Resolver}
46
*/
47
function resolveAllAttention(events, context) {
48
let index = -1;
49
let open;
50
const nextEvents = [];
51
52
// Walk through all events looking for closable attention sequences
53
while (++index < events.length) {
54
if (
55
events[index][0] === 'enter' &&
56
events[index][1].type === 'attentionSequence' &&
57
events[index][1]._close
58
) {
59
open = index;
60
61
// Find the matching opener by walking backward
62
while (open--) {
63
if (
64
events[open][0] === 'enter' &&
65
events[open][1].type === 'attentionSequence' &&
66
events[open][1]._open
67
// Additional matching logic would go here
68
) {
69
// Process events between opener and closer using resolveAll
70
const betweenEvents = resolveAll(
71
context.parser.constructs.insideSpan.null,
72
events.slice(open + 1, index),
73
context
74
);
75
76
// Add the resolved events to the result
77
push(nextEvents, betweenEvents);
78
break;
79
}
80
}
81
}
82
}
83
84
return nextEvents;
85
}
86
```
87
88
## Capabilities
89
90
### Event Resolution
91
92
Calls all `resolveAll` functions from constructs to manipulate parsing events. This is used internally by micromark to handle complex parsing scenarios where initial tokenization needs post-processing.
93
94
```javascript { .api }
95
/**
96
* Call all resolveAll functions from constructs to manipulate events
97
* @param {Array<{resolveAll?: Resolver | undefined}>} constructs - List of constructs, optionally with resolveAll resolvers
98
* @param {Array<Event>} events - List of events to be processed
99
* @param {TokenizeContext} context - Context used by tokenize function
100
* @returns {Array<Event>} Modified/processed events
101
*/
102
function resolveAll(constructs, events, context);
103
```
104
105
**Parameters:**
106
107
- **constructs**: `Array<{resolveAll?: Resolver | undefined}>` - List of constructs, where each construct may optionally contain a `resolveAll` resolver function
108
- **events**: `Array<Event>` - List of parsing events to be processed
109
- **context**: `TokenizeContext` - Context used by tokenize function
110
111
**Returns:**
112
113
- `Array<Event>` - The processed events after all resolvers have been applied
114
115
**Behavior:**
116
117
- Iterates through all provided constructs
118
- For each construct that has a `resolveAll` function, calls it with the current events and context
119
- Ensures each resolver is only called once (tracked internally to prevent duplicates)
120
- Returns the final processed events array after all resolvers have been applied
121
122
## Types
123
124
### Event
125
126
Represents a parsing event in micromark's tokenization process.
127
128
```typescript { .api }
129
/**
130
* A parsing event tuple containing entry/exit type, token, and context
131
*/
132
type Event = ['enter' | 'exit', Token, TokenizeContext];
133
```
134
135
### Resolver
136
137
Function type for event manipulation functions.
138
139
```typescript { .api }
140
/**
141
* Function that takes events and manipulates them
142
* @param events - List of events
143
* @param context - Tokenize context
144
* @returns The given, modified, events
145
*/
146
type Resolver = (
147
events: Array<Event>,
148
context: TokenizeContext
149
) => Array<Event>;
150
```
151
152
### TokenizeContext
153
154
Interface representing the tokenization context used during parsing.
155
156
```typescript { .api }
157
/**
158
* A context object that helps with tokenizing markdown constructs
159
*/
160
interface TokenizeContext {
161
/** The previous character code */
162
previous: Code;
163
164
/** Current character code */
165
code: Code;
166
167
/** Whether we're currently interrupting */
168
interrupt?: boolean | undefined;
169
170
/** The current construct (constructs that are not partial are set here) */
171
currentConstruct?: Construct | undefined;
172
173
/** State shared between container parsing phases */
174
containerState?: ContainerState | undefined;
175
176
/** Current list of events */
177
events: Array<Event>;
178
179
/** The relevant parsing context */
180
parser: ParseContext;
181
182
/** Get the chunks that span a token */
183
sliceStream: (token: Pick<Token, 'end' | 'start'>) => Array<Chunk>;
184
185
/** Get the source text that spans a token */
186
sliceSerialize: (
187
token: Pick<Token, 'end' | 'start'>,
188
expandTabs?: boolean | undefined
189
) => string;
190
}
191
```
192
193
### Code
194
195
Represents a character code in micromark's processing.
196
197
```typescript { .api }
198
/**
199
* A character code.
200
*
201
* This is often the same as what String#charCodeAt() yields but micromark
202
* adds meaning to certain other values.
203
*
204
* null represents the end of the input stream (called eof).
205
* Negative integers are used instead of certain sequences of characters
206
* (such as line endings and tabs).
207
*/
208
type Code = number | null;
209
```
210
211
### Token
212
213
Represents a token in the micromark parsing process.
214
215
```typescript { .api }
216
/**
217
* A token: a span of chunks.
218
* Tokens are passed in events to the compiler.
219
* The chunks they span are then passed through the flow tokenizer.
220
*/
221
interface Token {
222
/** Token type */
223
type: TokenType;
224
225
/** Point where the token starts */
226
start: Point;
227
228
/** Point where the token ends */
229
end: Point;
230
231
/** The previous token in a list of linked tokens */
232
previous?: Token | undefined;
233
234
/** The next token in a list of linked tokens */
235
next?: Token | undefined;
236
}
237
```
238
239
## Usage Patterns
240
241
This utility is primarily used when creating custom micromark extensions that need to:
242
243
- Handle complex markdown structures that require post-processing
244
- Resolve relationships between opening and closing tokens
245
- Process nested constructs within matched pairs
246
- Convert unmatched tokens to plain text
247
248
Common use cases include:
249
- **Attention processing**: Matching `*` and `_` tokens for emphasis and strong emphasis
250
- **Media processing**: Matching `[` and `]` tokens for links and images
251
- **Custom extension development**: Any extension that needs to resolve token relationships after initial parsing
252
253
## Compatibility
254
255
- **micromark**: Compatible with micromark@^3
256
- **Node.js**: Requires Node.js 16+
257
- **ESM only**: This package is ESM only
258
- **Browsers**: Works in browsers via ESM
259
- **Deno**: Works in Deno
260
- **unified ecosystem**: Part of the unified ecosystem for markdown processing