course-renderer
Version:
Manages CA School Courses file system storage and HTML conversion
35 lines (26 loc) • 996 B
text/typescript
import * as cheerio from 'cheerio'
import * as MarkdownIt from 'markdown-it'
import Token from '../markdown/Token'
export function transform(tokens: Token[], includeList: boolean) {
const md = new MarkdownIt({ html: true });
if (!includeList) {
md.disable(['list'])
}
render(tokens, md)
return tokens
}
function render(tokens: Token[], md: MarkdownIt.MarkdownIt) {
tokens.forEach((token) => {
if (token.children instanceof Array) {
render(token.children, md)
}
if (token.content && token.content.length > 0) {
token.content = md.render(token.content).replace(/\n$/, '') // Remove trailing new line added by markdown-it
}
if (token.type === 'SS' || token.type === 'MS') {
token.choices.forEach((val, index) => {
token.choices[index] = md.render(val).replace(/\n$/, '') // Remove trailing new line added by markdown-it
})
}
})
}