silk.markdown.html.
HTML output for Markdown trees, with escaped raw HTML and filtered destinations by default.
When to use#
Use MarkdownHtml.renderOwned for owned text, renderInto for a reusable buffer, or renderToWriter for streaming output.
Details#
Rendering walks tree links iteratively and allocates no storage per node. Default options escape raw HTML and empty destinations with javascript, vbscript, file, or data schemes.
Examples#
Render a link with a filtered destination#
import silk.markdown.document { MarkdownDocument }
import silk.markdown.html { MarkdownHtml }
import silk.text.string { String }
import silk.core.effect { Effect }
import silk.mem.allocator { Allocator, OutOfMemoryError }
pub effect fn main() -> i32
! OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let document = run MarkdownDocument.parse("[open](javascript:alert)")
|> Effect.provideMut<Allocator>(&mut allocator)
let output = run MarkdownHtml.renderOwned(&document)
|> Effect.provideMut<Allocator>(&mut allocator)
if String.view(&output) != "<p><a href=\"\">open</a></p>\n" {
return 1
}
return 0
}Import as MarkdownHtml with import silk.markdown.html { MarkdownHtml }.
Public declarations: 2.
HtmlOptions#
pub struct HtmlOptionsThe HTML rendering policy. The defaults escape raw HTML and filter dangerous destination schemes.
Field rawHtml#
pub rawHtml: boolEmits raw HTML verbatim when true. The default is false, which escapes raw HTML as text.
Field unsafeLinks#
pub unsafeLinks: boolAllows javascript, vbscript, file, and data destinations when true. The default is false. Filtering ignores ASCII case, leading control bytes, and tabs or line endings within a scheme.
Field headingIds#
pub headingIds: boolEmits unique GitHub-style heading ids when true. The default is false. Slug state starts empty for each render operation and follows rendered heading order.
Implementation Copy for HtmlOptions#
impl Copy for HtmlOptionsImplementation HtmlOptions#
impl HtmlOptionsAssociated function make#
pub fn make() -> HtmlOptionsReturns options that escape raw HTML and filter dangerous destination schemes.
MarkdownHtml#
pub struct MarkdownHtmlThe owner of Markdown HTML rendering and escaping operations.
Implementation MarkdownHtml#
impl MarkdownHtmlAssociated function renderOwned#
pub effect fn renderOwned(document: &MarkdownDocument<'_>) -> String
! OutOfMemoryError
? &mut AllocatorReturns HTML with raw HTML escaped and dangerous destination schemes filtered.
Errors#
OutOfMemoryErrorif the allocator cannot supply the output string.
Associated function renderOwnedWithOptions#
pub effect fn renderOwnedWithOptions(
document: &MarkdownDocument<'_>,
options: &HtmlOptions,
) -> String
! OutOfMemoryError
? &mut AllocatorReturns HTML using the raw-HTML and destination policy in options.
Errors#
OutOfMemoryErrorif the allocator cannot supply the output string.
Associated function renderInto#
pub effect fn renderInto(document: &MarkdownDocument<'_>, out: &mut Bytes) -> ()
! OutOfMemoryError
? &mut AllocatorReplaces out with HTML that escapes raw HTML and filters dangerous destination schemes.
Errors#
OutOfMemoryErrorif the output cannot grow. A partial result can remain inout.
Associated function renderIntoWithOptions#
pub effect fn renderIntoWithOptions(
document: &MarkdownDocument<'_>,
out: &mut Bytes,
options: &HtmlOptions,
) -> ()
! OutOfMemoryError
? &mut AllocatorReplaces out with HTML using the raw-HTML and destination policy in options.
Errors#
OutOfMemoryErrorif the output cannot grow. A partial result can remain inout.
Associated function renderNodeInto#
pub effect fn renderNodeInto(
document: &MarkdownDocument<'_>,
id: MarkdownNodeId,
out: &mut Bytes,
) -> ()
! OutOfMemoryError
? &mut AllocatorAppends the HTML of subtree id to out with raw HTML escaped and dangerous schemes filtered.
Invalid ids append nothing. List paragraphs follow their containing list's tightness.
Errors#
OutOfMemoryErrorif the output cannot grow. Bytes already appended remain inout.
Associated function renderNodeIntoWithOptions#
pub effect fn renderNodeIntoWithOptions(
document: &MarkdownDocument<'_>,
id: MarkdownNodeId,
out: &mut Bytes,
options: &HtmlOptions,
) -> ()
! OutOfMemoryError
? &mut AllocatorAppends subtree id as HTML using options. Invalid ids append nothing.
Errors#
OutOfMemoryErrorif the output cannot grow. Bytes already appended remain inout.
Associated function renderToWriter#
pub effect fn renderToWriter(document: &MarkdownDocument<'_>) -> ()
! WriterError | OutOfMemoryError
? &mut Writer | &mut AllocatorWrites HTML with raw HTML escaped and dangerous schemes filtered. Does not flush the writer. Heading ids allocate slug state only when enabled.
Errors#
WriterErrorif the writer fails. Bytes already accepted remain written.OutOfMemoryErrorif enabled heading-id storage cannot grow. Earlier output remains written.
Associated function renderToWriterWithOptions#
pub effect fn renderToWriterWithOptions(
document: &MarkdownDocument<'_>,
options: &HtmlOptions,
) -> ()
! WriterError | OutOfMemoryError
? &mut Writer | &mut AllocatorWrites HTML using options. Does not flush the writer. Heading ids allocate slug state only when enabled.
Errors#
WriterErrorif the writer fails. Bytes already accepted remain written.OutOfMemoryErrorif enabled heading-id storage cannot grow. Earlier output remains written.
Associated function escapeTextInto#
pub effect fn escapeTextInto(out: &mut Bytes, value: string) -> ()
! OutOfMemoryError
? &mut AllocatorAppends text with &, <, >, and double quotes replaced by HTML character references.
Errors#
OutOfMemoryErrorif the output cannot grow. Bytes already appended remain inout.
Associated function escapeAttributeInto#
pub effect fn escapeAttributeInto(out: &mut Bytes, value: string) -> ()
! OutOfMemoryError
? &mut AllocatorAppends text escaped for a double-quoted HTML attribute. Replaces &, <, >, and double quotes.
Errors#
OutOfMemoryErrorif the output cannot grow. Bytes already appended remain inout.
Associated function escapeTextOwned#
pub effect fn escapeTextOwned(value: string) -> String
! OutOfMemoryError
? &mut AllocatorReturns text with &, <, >, and double quotes replaced by HTML character references.
Errors#
OutOfMemoryErrorif the allocator cannot supply the output string.
Associated function escapeAttributeOwned#
pub effect fn escapeAttributeOwned(value: string) -> String
! OutOfMemoryError
? &mut AllocatorReturns text escaped for a double-quoted HTML attribute. Replaces &, <, >, and double quotes.
Errors#
OutOfMemoryErrorif the allocator cannot supply the output string.
Category: Data formats.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.