Skip to content

Commit a7d0a9d

Browse files
authored
simplify tab conversion (#192)
1 parent c5301b6 commit a7d0a9d

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -967,24 +967,16 @@ function collect_options(source: string, options: SnippetOptions) {
967967
return source;
968968
}
969969

970+
/**
971+
* `marked` replaces tabs with four spaces, which is unhelpful.
972+
* This function turns them back into tabs (plus leftover spaces for e.g. `\t * some JSDoc`)
973+
*/
970974
function adjust_tab_indentation(source: string, language: string) {
971-
return (
972-
source
973-
// TODO: what exactly is going on here? The regex finds spaces and replaces them with spaces again?
974-
.replace(/^([\-\+])?((?: )+)/gm, (match, prefix = '', spaces) => {
975-
if ((prefix && language !== 'diff') || language === 'yaml') return match;
976-
977-
// for no good reason at all, marked replaces tabs with spaces
978-
let i = 0;
979-
let tabs = '';
980-
for (; i < spaces.length; i += 4) {
981-
tabs += '\t';
982-
}
983-
tabs += ' '.repeat(i % 4);
984-
return prefix + tabs;
985-
})
986-
.replace(/\*\\\//g, '*/')
987-
);
975+
return source.replace(/^([\-\+])?((?: )+)/gm, (match, prefix = '', spaces) => {
976+
if ((prefix && language !== 'diff') || language === 'yaml') return match;
977+
978+
return prefix + '\t'.repeat(spaces.length / 4) + ' '.repeat(spaces.length % 4);
979+
});
988980
}
989981

990982
function replace_blank_lines(html: string) {

0 commit comments

Comments
 (0)