Skip to content

Commit c835191

Browse files
wooormsethvincent
authored andcommitted
Add position patching (#18)
1 parent 52b12b2 commit c835191

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,28 @@ function toMDAST(tree) {
1212
}
1313

1414
function h(node, type, props, children) {
15+
var result;
1516
if (!children && ((typeof props === 'object' && 'length' in props) || typeof props === 'string')) {
1617
children = props;
1718
props = {};
1819
}
1920

20-
var result = augment(node, {type: type});
21+
result = xtend({type: type}, props);
2122

2223
if (typeof children === 'string') {
2324
result.value = children;
2425
} else if (children) {
2526
result.children = children;
2627
}
2728

28-
return xtend(result, props);
29+
return augment(node, result);
2930
}
3031

3132
/* `right` is the finalized MDAST node,
3233
* created from `left`, a HAST node */
3334
function augment(left, right) {
34-
if (left.value) {
35-
right.value = left.value;
35+
if (left.position) {
36+
right.position = left.position;
3637
}
3738

3839
return right;

tests/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,51 @@ test('core', function (t) {
4242
'should ignore unknown voids'
4343
);
4444

45+
var pos = {
46+
left: {
47+
line: 1,
48+
column: 1,
49+
offset: 0
50+
},
51+
right: {
52+
line: 1,
53+
column: 6,
54+
offset: 5
55+
}
56+
};
57+
58+
t.deepEqual(
59+
toMDAST({
60+
type: 'root',
61+
children: [{
62+
type: 'element',
63+
tagName: 'p',
64+
properties: {},
65+
children: [{
66+
type: 'text',
67+
value: 'Alpha',
68+
position: pos
69+
}],
70+
position: pos
71+
}],
72+
position: pos
73+
}),
74+
{
75+
type: 'root',
76+
children: [{
77+
type: 'paragraph',
78+
children: [{
79+
type: 'text',
80+
value: 'Alpha',
81+
position: pos
82+
}],
83+
position: pos
84+
}],
85+
position: pos
86+
},
87+
'should ignore unknown voids'
88+
);
89+
4590
t.end();
4691
});
4792

0 commit comments

Comments
 (0)