Skip to content

Commit 16c95f5

Browse files
committed
Add support for transforms in extensions
1 parent a78fe71 commit 16c95f5

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

Diff for: lib/index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function compiler(options) {
3636
var settings = options || {}
3737
var config = configure(
3838
{
39+
transforms: [],
3940
canContainEols: [
4041
'emphasis',
4142
'fragment',
@@ -145,7 +146,8 @@ function compiler(options) {
145146
return compile
146147

147148
function compile(events) {
148-
var stack = [{type: 'root', children: []}]
149+
var tree = {type: 'root', children: []}
150+
var stack = [tree]
149151
var tokenStack = []
150152
var listStack = []
151153
var index = -1
@@ -207,7 +209,7 @@ function compiler(options) {
207209
}
208210

209211
// Figure out `root` position.
210-
stack[0].position = {
212+
tree.position = {
211213
start: point(
212214
events.length ? events[0][1].start : {line: 1, column: 1, offset: 0}
213215
),
@@ -218,7 +220,12 @@ function compiler(options) {
218220
)
219221
}
220222

221-
return stack[0]
223+
index = -1
224+
while (++index < config.transforms.length) {
225+
tree = config.transforms[index](tree) || tree
226+
}
227+
228+
return tree
222229
}
223230

224231
function prepareList(events, start, length) {
@@ -803,7 +810,7 @@ function extension(config, extension) {
803810
for (key in extension) {
804811
left = own.call(config, key) ? config[key] : (config[key] = {})
805812

806-
if (key === 'canContainEols') {
813+
if (key === 'canContainEols' || key === 'transforms') {
807814
config[key] = [].concat(left, extension[key])
808815
} else {
809816
Object.assign(left, extension[key])

Diff for: test/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ test('mdast-util-from-markdown', function (t) {
123123
this.exit(token)
124124
}
125125

126+
t.deepEqual(
127+
fromMarkdown('*a*', {
128+
mdastExtensions: [{transforms: [transform]}]
129+
}).children[0].children,
130+
[
131+
{
132+
type: 'strong',
133+
children: [
134+
{
135+
type: 'text',
136+
value: 'a',
137+
position: {
138+
start: {line: 1, column: 2, offset: 1},
139+
end: {line: 1, column: 3, offset: 2}
140+
}
141+
}
142+
],
143+
position: {
144+
start: {line: 1, column: 1, offset: 0},
145+
end: {line: 1, column: 4, offset: 3}
146+
}
147+
}
148+
],
149+
'should support `transforms` in extensions'
150+
)
151+
152+
function transform(tree) {
153+
tree.children[0].children[0].type = 'strong'
154+
}
155+
126156
t.throws(
127157
function () {
128158
fromMarkdown('a', {

0 commit comments

Comments
 (0)