Skip to content

Commit 32385e2

Browse files
dancormierb-kelly
andauthored
fix(markdown-mode): prevent block change from stripping numbers (#140)
* fix: prevent block change from stripping numbers fixes #69 * add matchLeadingBlockCharacters tests * docs(commands): reseat TODOs for match regex * fix(commands): add support for all ordered list markers in matchLeadingBlockCharacters Co-authored-by: Ben Kelly <b-kelly@users.noreply.github.com>
1 parent cb41a05 commit 32385e2

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/commonmark/commands.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,20 @@ function setBlockType(
192192
/**
193193
* Returns any block formatting characters (plus trailing space) at the very start of the passed text
194194
* @param text The text to check for leading block characters
195+
* @internal
195196
*/
196-
function matchLeadingBlockCharacters(text: string) {
197+
export function matchLeadingBlockCharacters(text: string) {
197198
// TODO this might be too aggressive... remove based on a whitelist instead?
198-
// TODO HACK assumes all block types are non-letter characters followed by a single space
199-
return /^[^a-zA-Z]+\s{1}(?=[a-zA-Z_*[!]|$)/.exec(text)?.[0] || "";
199+
// Match ordered list markers; see https://spec.commonmark.org/0.30/#ordered-list-marker
200+
let match = /^(\d+)(?:\.|\))\s/.exec(text)?.[0];
201+
202+
// If text is not an ordered list block, check for other block types
203+
if (!match) {
204+
// TODO HACK assumes all non-ordered list block types are non-letter characters followed by a single space
205+
match = /^[^a-zA-Z0-9]+\s{1}(?=[a-zA-Z0-9_*[!]|$)+/.exec(text)?.[0];
206+
}
207+
208+
return match || "";
200209
}
201210

202211
/**

test/commonmark/commands.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,37 @@ some text`;
521521
});
522522
});
523523
});
524+
525+
describe("matchLeadingBlockCharacters", () => {
526+
it("return no leading characters", () => {
527+
const command = commands.matchLeadingBlockCharacters(
528+
"42 shouldn't be returned"
529+
);
530+
531+
expect(command).toBe("");
532+
});
533+
534+
it("return ordered list leading characters", () => {
535+
let command = commands.matchLeadingBlockCharacters("23. -ol item");
536+
537+
expect(command).toBe("23. ");
538+
539+
command = commands.matchLeadingBlockCharacters("23) -ol item");
540+
541+
expect(command).toBe("23) ");
542+
});
543+
544+
it("return unordered list leading characters", () => {
545+
const command = commands.matchLeadingBlockCharacters("- 1 ul item");
546+
547+
expect(command).toBe("- ");
548+
});
549+
550+
it("return heading leading characters", () => {
551+
const command =
552+
commands.matchLeadingBlockCharacters("## Heading level 2");
553+
554+
expect(command).toBe("## ");
555+
});
556+
});
524557
});

0 commit comments

Comments
 (0)