Skip to content

Commit

Permalink
fix(plugins/checkbox): preserve line mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
moki committed Jun 14, 2023
1 parent 3239932 commit 9ccee18
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/transform/plugins/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
};
const options = Object.assign(defaults, opts);

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

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

Expand All @@ -45,6 +46,7 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
lastId += 1;
token = new state.Token('checkbox_input', 'input', 0);
token.block = true;
token.map = state.tokens[i].map;
token.attrs = [
['type', 'checkbox'],
['id', id],
Expand Down Expand Up @@ -73,20 +75,23 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
*/
token = new state.Token('checkbox_label_close', 'label', -1);
token.block = true;
token.map = state.tokens[i].map;
nodes.push(token);
token = new state.Token('checkbox_close', 'div', -1);
token.block = true;
token.map = state.tokens[i].map;
nodes.push(token);

return nodes;
};
const splitTextToken = function (state: StateCore, matches: RegExpMatchArray) {
const splitTextToken = function (state: StateCore, matches: RegExpMatchArray, i: number) {
let checked = false;
const value = matches[1];
const label = matches[2];
if (value === 'X' || value === 'x') {
checked = true;
}
return createTokens(state, checked, label);
return createTokens(state, checked, label, i);
};
return function (state: StateCore) {
const blockTokens = state.tokens;
Expand All @@ -96,7 +101,7 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions)
continue;
}

blockTokens.splice(i, 3, ...splitTextToken(state, match));
blockTokens.splice(i, 3, ...splitTextToken(state, match, i));
}
};
};

0 comments on commit 9ccee18

Please sign in to comment.