Skip to content

Commit 9ccee18

Browse files
committed
fix(plugins/checkbox): preserve line mapping
1 parent 3239932 commit 9ccee18

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/transform/plugins/checkbox/checkbox.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
2626
};
2727
const options = Object.assign(defaults, opts);
2828

29-
const createTokens = function (state: StateCore, checked: boolean, label: string) {
29+
const createTokens = function (state: StateCore, checked: boolean, label: string, i: number) {
3030
let token: Token;
3131
const nodes = [];
3232

@@ -35,6 +35,7 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
3535
*/
3636
token = new state.Token('checkbox_open', 'div', 1);
3737
token.block = true;
38+
token.map = state.tokens[i].map;
3839
token.attrs = [['class', options.divClass]];
3940
nodes.push(token);
4041

@@ -45,6 +46,7 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
4546
lastId += 1;
4647
token = new state.Token('checkbox_input', 'input', 0);
4748
token.block = true;
49+
token.map = state.tokens[i].map;
4850
token.attrs = [
4951
['type', 'checkbox'],
5052
['id', id],
@@ -73,20 +75,23 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
7375
*/
7476
token = new state.Token('checkbox_label_close', 'label', -1);
7577
token.block = true;
78+
token.map = state.tokens[i].map;
7679
nodes.push(token);
7780
token = new state.Token('checkbox_close', 'div', -1);
7881
token.block = true;
82+
token.map = state.tokens[i].map;
7983
nodes.push(token);
84+
8085
return nodes;
8186
};
82-
const splitTextToken = function (state: StateCore, matches: RegExpMatchArray) {
87+
const splitTextToken = function (state: StateCore, matches: RegExpMatchArray, i: number) {
8388
let checked = false;
8489
const value = matches[1];
8590
const label = matches[2];
8691
if (value === 'X' || value === 'x') {
8792
checked = true;
8893
}
89-
return createTokens(state, checked, label);
94+
return createTokens(state, checked, label, i);
9095
};
9196
return function (state: StateCore) {
9297
const blockTokens = state.tokens;
@@ -96,7 +101,7 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
96101
continue;
97102
}
98103

99-
blockTokens.splice(i, 3, ...splitTextToken(state, match));
104+
blockTokens.splice(i, 3, ...splitTextToken(state, match, i));
100105
}
101106
};
102107
};

0 commit comments

Comments
 (0)