Skip to content

Instantly share code, notes, and snippets.

@Eyas
Created January 23, 2020 03:11
Show Gist options
  • Save Eyas/2db12f6c551707e9182ac6b565f89591 to your computer and use it in GitHub Desktop.
Save Eyas/2db12f6c551707e9182ac6b565f89591 to your computer and use it in GitHub Desktop.
function parseComment(comment: string): string {
const result: string[] = [];
const parser = new Parser({
ontext: text => result.push(replace(text)),
onopentag: (tag, attrs) => {
switch (tag) {
case 'a': result.push(`{@link ${attrs['href']} `); break;
case 'em': case 'i': result.push('_'); break;
case 'strong': case 'b': reslt.push('__'); break;
// ...
default: throw new Error(`Unknown tag "${tag}".`);
},
onclosetag: tag => {
case 'a': result.push('}'); break;
case 'em': case 'i': result.push('_'); break;
case 'strong': case 'b': reslt.push('__'); break;
// ...
default: throw new Error(`Unknown tag "${tag}".`);
}
}
});
parser.write(comment);
parser.end();
// ... turn result into 'lines'
return lines.length === 1 ? `* lines[0] ` :
('*\n *' + lines.join('\n *') + '\n ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment