'); } else { this.sb.append('
'); }; this.renderInlines(inlines); this.sb.append('
\n'); } case Heading {level, inlines, id}: { this.writeIndent(); this.sb.append('\n'); this.pushIndent(); this.renderBlocks(blocks); this.popIndent(); this.writeIndent(); this.sb.append('\n'); } case ListBlock {ordered, startNumber, tight, items}: { this.writeIndent(); if (ordered) { if (startNumber != 1) { this.sb.append('
');
} else {
this.sb.append('');
};
this.escapeHtml(literal);
this.sb.append('
\n');
}
case IndentedCodeBlock {literal}: {
this.writeIndent();
this.sb.append('');
this.escapeHtml(literal);
this.sb.append('
\n');
}
case HtmlBlock {literal}: {
if (this.safe) {
if (this.escapeRawHtml) {
this.writeIndent();
this.sb.append('');
this.escapeHtml(literal);
this.sb.append('
\n');
};
} else {
this.writeIndent();
this.sb.append(literal);
this.sb.appendByte(0x0A);
};
}
case ThematicBreak: {
this.writeIndent();
this.sb.append('
\n');
}
case CustomBlock as custom: {
for (let renderer in this.#renderers) {
if (renderer.renderBlock(custom, this)) {
return;
}
}
}
};
}
#renderListItem(item: ListItem, tight: boolean): void {
var hasCheckbox = false;
var isChecked = false;
if (let Some {value} = item.checked) {
hasCheckbox = true;
isChecked = value;
}
this.writeIndent();
if (hasCheckbox) {
this.sb.append('- ');
} else {
this.sb.append('
- ');
}
let checkboxHtml = if (isChecked) ' ' else ' ';
if (item.blocks.length == 0) {
if (hasCheckbox) {
this.sb.append(checkboxHtml);
}
this.sb.append('
\n');
return;
}
if (tight) {
var idx = 0;
var indentedBody = false;
while (idx < item.blocks.length) {
let child = item.blocks[idx];
if (let Paragraph {inlines} = child) {
if (idx == 0 && hasCheckbox) {
this.sb.append(checkboxHtml);
} else if (idx > 0) {
this.writeIndent();
}
this.renderInlines(inlines);
if (idx + 1 < item.blocks.length) {
this.sb.appendByte(0x0A);
if (!indentedBody) {
this.pushIndent();
indentedBody = true;
}
}
} else {
if (idx == 0) {
if (hasCheckbox) {
this.sb.append(checkboxHtml);
}
this.sb.appendByte(0x0A);
this.pushIndent();
indentedBody = true;
}
this.renderBlock(child);
}
idx += 1;
}
if (indentedBody) {
this.popIndent();
if (!(item.blocks[item.blocks.length - 1] is Paragraph)) {
this.writeIndent();
}
}
this.sb.append('\n');
return;
}
this.sb.appendByte(0x0A);
this.pushIndent();
var idx = 0;
while (idx < item.blocks.length) {
let child = item.blocks[idx];
if (idx == 0 && hasCheckbox) {
if (let Paragraph {inlines, id} = child) {
this.writeIndent();
if (id != null) {
this.sb.append('');
} else {
this.sb.append('
');
}
this.sb.append(checkboxHtml);
this.renderInlines(inlines);
this.sb.append('
\n');
idx += 1;
continue;
}
this.writeIndent();
this.sb.append(checkboxHtml);
}
this.renderBlock(child);
idx += 1;
}
this.popIndent();
this.writeIndent();
this.sb.append('\n');
}
renderInlines(nodes: GrowableArray): void {
for (let node in nodes) {
this.renderInline(node);
}
}
renderInline(node: Inline): void {
match (node) {
case Text {literal}: {
this.escapeHtml(literal);
}
case CodeSpan {literal}: {
this.sb.append('');
this.escapeHtml(literal);
this.sb.append('');
}
case Emphasis {children}: {
this.sb.append('');
this.renderInlines(children);
this.sb.append('');
}
case Strong {children}: {
this.sb.append('');
this.renderInlines(children);
this.sb.append('');
}
case Link {destination: let written, title, children}: {
let destination = this.#resolver.resolve(written, false);
if (destination == null) {
this.renderInlines(children);
return;
};
this.sb.append('');
this.renderInlines(children);
this.sb.append('');
}
case Image {destination: let written, title, children}: {
let destination = this.#resolver.resolve(written, true);
if (destination == null) {
return;
};
this.sb.append('
');
}
case Autolink {destination: let written, label}: {
let destination = this.#resolver.resolve(written, false);
if (destination == null) {
this.escapeHtml(label);
return;
};
this.sb.append('');
this.escapeHtml(label);
this.sb.append('');
}
case HtmlInline {literal}: {
if (this.safe) {
if (this.escapeRawHtml) {
this.escapeHtml(literal);
};
} else {
this.sb.append(literal);
};
}
case SoftBreak: {
this.sb.append(this.#softBreak);
}
case HardBreak: {
this.sb.append('
\n');
}
case CustomInline as custom: {
for (let renderer in this.#renderers) {
if (renderer.renderInline(custom, this)) {
return;
}
}
}
};
}
escapeHtml(text: String): void {
let r = new StringReader(text);
var last = r.mark();
while (!r.isAtEnd) {
let pos = r.mark();
let cp = r.advance();
let entity = if (cp == 0x26) {
'&';
} else if (cp == 0x3C) {
'<';
} else if (cp == 0x3E) {
'>';
} else if (cp == 0x22) {
'"';
} else {
null;
};
if (entity != null) {
if (pos > last) {
this.sb.append(r.sliceRange(last, pos));
}
this.sb.append(entity);
last = r.mark();
}
}
if (last == 0) {
this.sb.append(text);
} else if (last < r.mark()) {
this.sb.append(r.sliceFrom(last));
}
}
escapeAttribute(text: String): void {
this.escapeHtml(text);
}
isSafeUrl(url: String, isImage: boolean): boolean {
return isSafeUrl(url, isImage);
}
}
function firstWord(s: String) {
let r = new StringReader(s);
let start = r.mark();
r.skipWhile((cp) => !isAsciiWhitespace(cp));
return r.sliceFrom(start);
}