Skip to content

Commit dca8bc6

Browse files
rscharfedscho
authored andcommitted
Format checkboxes
GitHub task lists are represented in HTML as unordered lists with checkboxes. These checkboxes are currently ignored when converting the HTML to text. This affects PRs with task lists (e.g. git/git#1359); the resulting text list has no indication anymore whether an item was checked (done) or not. Format checkboxes by turning checked ones into "[x]" and unchecked ones into "[ ]" to retain that information during the conversion. Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 54bf231 commit dca8bc6

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/markdown-renderer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export function md2text(markdown: string, columns = 76): string {
3636
.replace(/(^|\n)(\n)(?!$)/g, "$1>$2"); // quote empty
3737
}});
3838
},
39+
checkBoxFormatter: (elem, _walk, builder) => {
40+
const attribs = elem.attribs as { checked?: string };
41+
builder.addInline(attribs.checked === undefined ? "[ ]" : "[x]");
42+
},
3943
},
4044
selectors: [
4145
{
@@ -72,6 +76,10 @@ export function md2text(markdown: string, columns = 76): string {
7276
},
7377
format: "blockFormatter"
7478
},
79+
{
80+
selector: "input[type=checkbox]",
81+
format: "checkBoxFormatter"
82+
},
7583
],
7684
};
7785

tests/markdown-renderer.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,13 @@ const bq7 = `Over 56 levels still has 20 char (exact):
109109
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3 5 7 9 1 3 5 7 9 1
110110
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3 5 7`);
111111
});
112+
113+
test("task lists are rendered correctly", () => {
114+
const taskList = `This is a task list:
115+
* [x] done item
116+
* [ ] item still to do`;
117+
expect(md2text(taskList)).toEqual(`This is a task list:
118+
119+
* [x] done item
120+
* [ ] item still to do`);
121+
});

0 commit comments

Comments
 (0)