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#

silk
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#

silk
pub struct HtmlOptions

The HTML rendering policy. The defaults escape raw HTML and filter dangerous destination schemes.

Field rawHtml#

silk
pub rawHtml: bool

Emits raw HTML verbatim when true. The default is false, which escapes raw HTML as text.

silk
pub unsafeLinks: bool

Allows 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#

silk
pub headingIds: bool

Emits 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#

silk
impl Copy for HtmlOptions

Implementation HtmlOptions#

silk
impl HtmlOptions

Associated function make#

silk
pub fn make() -> HtmlOptions

Returns options that escape raw HTML and filter dangerous destination schemes.

MarkdownHtml#

silk
pub struct MarkdownHtml

The owner of Markdown HTML rendering and escaping operations.

Implementation MarkdownHtml#

silk
impl MarkdownHtml

Associated function renderOwned#

silk
pub effect fn renderOwned(document: &MarkdownDocument<'_>) -> String
    ! OutOfMemoryError
    ? &mut Allocator

Returns HTML with raw HTML escaped and dangerous destination schemes filtered.

Errors#

  • OutOfMemoryError if the allocator cannot supply the output string.

Associated function renderOwnedWithOptions#

silk
pub effect fn renderOwnedWithOptions(
    document: &MarkdownDocument<'_>,
    options: &HtmlOptions,
  ) -> String
    ! OutOfMemoryError
    ? &mut Allocator

Returns HTML using the raw-HTML and destination policy in options.

Errors#

  • OutOfMemoryError if the allocator cannot supply the output string.

Associated function renderInto#

silk
pub effect fn renderInto(document: &MarkdownDocument<'_>, out: &mut Bytes) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Replaces out with HTML that escapes raw HTML and filters dangerous destination schemes.

Errors#

  • OutOfMemoryError if the output cannot grow. A partial result can remain in out.

Associated function renderIntoWithOptions#

silk
pub effect fn renderIntoWithOptions(
    document: &MarkdownDocument<'_>,
    out: &mut Bytes,
    options: &HtmlOptions,
  ) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Replaces out with HTML using the raw-HTML and destination policy in options.

Errors#

  • OutOfMemoryError if the output cannot grow. A partial result can remain in out.

Associated function renderNodeInto#

silk
pub effect fn renderNodeInto(
    document: &MarkdownDocument<'_>,
    id: MarkdownNodeId,
    out: &mut Bytes,
  ) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Appends 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#

  • OutOfMemoryError if the output cannot grow. Bytes already appended remain in out.

Associated function renderNodeIntoWithOptions#

silk
pub effect fn renderNodeIntoWithOptions(
    document: &MarkdownDocument<'_>,
    id: MarkdownNodeId,
    out: &mut Bytes,
    options: &HtmlOptions,
  ) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Appends subtree id as HTML using options. Invalid ids append nothing.

Errors#

  • OutOfMemoryError if the output cannot grow. Bytes already appended remain in out.

Associated function renderToWriter#

silk
pub effect fn renderToWriter(document: &MarkdownDocument<'_>) -> ()
    ! WriterError | OutOfMemoryError
    ? &mut Writer | &mut Allocator

Writes HTML with raw HTML escaped and dangerous schemes filtered. Does not flush the writer. Heading ids allocate slug state only when enabled.

Errors#

  • WriterError if the writer fails. Bytes already accepted remain written.

  • OutOfMemoryError if enabled heading-id storage cannot grow. Earlier output remains written.

Associated function renderToWriterWithOptions#

silk
pub effect fn renderToWriterWithOptions(
    document: &MarkdownDocument<'_>,
    options: &HtmlOptions,
  ) -> ()
    ! WriterError | OutOfMemoryError
    ? &mut Writer | &mut Allocator

Writes HTML using options. Does not flush the writer. Heading ids allocate slug state only when enabled.

Errors#

  • WriterError if the writer fails. Bytes already accepted remain written.

  • OutOfMemoryError if enabled heading-id storage cannot grow. Earlier output remains written.

Associated function escapeTextInto#

silk
pub effect fn escapeTextInto(out: &mut Bytes, value: string) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Appends text with &, <, >, and double quotes replaced by HTML character references.

Errors#

  • OutOfMemoryError if the output cannot grow. Bytes already appended remain in out.

Associated function escapeAttributeInto#

silk
pub effect fn escapeAttributeInto(out: &mut Bytes, value: string) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Appends text escaped for a double-quoted HTML attribute. Replaces &, <, >, and double quotes.

Errors#

  • OutOfMemoryError if the output cannot grow. Bytes already appended remain in out.

Associated function escapeTextOwned#

silk
pub effect fn escapeTextOwned(value: string) -> String
    ! OutOfMemoryError
    ? &mut Allocator

Returns text with &, <, >, and double quotes replaced by HTML character references.

Errors#

  • OutOfMemoryError if the allocator cannot supply the output string.

Associated function escapeAttributeOwned#

silk
pub effect fn escapeAttributeOwned(value: string) -> String
    ! OutOfMemoryError
    ? &mut Allocator

Returns text escaped for a double-quoted HTML attribute. Replaces &, <, >, and double quotes.

Errors#

  • OutOfMemoryError if 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.

View the source of this page