block

codeListing

Wire value: "codeListing"

Appears on: Documentation Topic, Tutorial (Project), Tutorial Article

1. How swift-docc-render consumes this

codeListing is one of the block-content kinds dispatched by the shared render() function inside components/ContentNode.vue's BlockType map (codeListing: 'codeListing', line 39). The switch (node.type) case for it is:

case BlockType.codeListing: {
  if (node.metadata && node.metadata.anchor) {
    return renderFigure(node);
  }

  const props = {
    syntax: node.syntax,
    fileType: node.fileType,
    content: node.code,
    showLineNumbers: node.showLineNumbers,
    copyToClipboard: node.copyToClipboard ?? false,
    wrap: node.wrap ?? 0,
    lineAnnotations: node.lineAnnotations ?? [],
  };
  return createElement(CodeListing, { props });
}

(src/components/ContentNode.vue#L267-L281). Two things stand out mechanically:

The actual leaf component, components/ContentNode/CodeListing.vue, renders a <div class="code-listing"> containing an optional filename header, a copy-to-clipboard button, and a <pre><code>…</code></pre> block built line-by-line from the content prop (bound from node.code). Each source line becomes its own <span class="code-line-container">, with an optional <span class="code-number"> (shown when showLineNumbers is true) and a <span class="code-line" v-html="line"> holding the syntax-highlighted markup for that line (CodeListing.vue#L40-L59). Syntax highlighting itself happens client-side and asynchronously: a content watcher (CodeListing.vue#L177-L215) calls registerHighlightLanguage(this.syntaxNameNormalized) then highlightContent(...), falling back to plain HTML-escaped text if the language isn't supported — so syntax (normalized via a small occ→objective-c legacy-name map, line 144-148) drives which highlighter grammar loads, not which HTML is emitted.

Per-line decoration (highlight/strikeout) comes from the lineAnnotations prop, converted into per-style Sets of line numbers in the styleLineSets computed property (CodeListing.vue#L152-L175) and applied as the highlighted/strikethrough classes on each line's container span. copyToClipboard gates a small copy button that calls navigator.clipboard.writeText (CodeListing.vue#L216-L230), and wrap (a positive integer) sets a --wrap-ch CSS custom property used by the scoped SCSS to cap line width at N monospace characters instead of allowing horizontal scroll.

Note that CollapsibleCodeListing.vue and MobileCodeListing.vue (also under components/ContentNode/) exist in this repo but are not in this dispatch path — grepping their usages shows they are consumed by EndpointExample.vue and DictionaryExample.vue (and, for the mobile variant, the tutorial-only MobileCodePreview.vue), not by the plain block-level codeListing case above. A bare codeListing node always renders through the single, non-collapsible CodeListing.vue.

2. Example JSON and rendered mockup

Real fixture: Tests/DocCArchiveTests/Fixtures/ExampleDocs.doccarchive/data/documentation/exampledocs/examplearticle.json in this repository, found inside primaryContentSections[0].content[2].

{
  "type": "codeListing",
  "syntax": "swift",
  "code": [
    "func main() {",
    "    print(\"hello world\")",
    "}"
  ]
}

This minimal fixture omits every optional field (metadata, copyToClipboard, showLineNumbers, wrap, lineAnnotations). To also illustrate those fields (line numbers, a highlighted line, and a caption via metadata.anchor), here is a hand-extended variant built strictly from the fields documented in section 3:

{
  "type": "codeListing",
  "syntax": "swift",
  "code": [
    "func main() {",
    "    print(\"hello world\")",
    "}"
  ],
  "showLineNumbers": true,
  "copyToClipboard": true,
  "lineAnnotations": [
    { "style": "highlight", "range": [ { "line": 2 }, { "line": 2 } ] }
  ],
  "metadata": {
    "anchor": "main-function",
    "abstract": [ { "type": "text", "text": "The program's entry point." } ]
  }
}
The program's entry point.
1
2
3
func main() {
    print("hello world") }

3. Data sheet

Schema object: #/components/schemas/CodeListing in Sources/DocCArchive/Vendored/RenderNode.spec.json (line 816), one of the variants under the RenderBlockContent discriminated union (its type enum member is listed at line 2462 of the same file). Generated Swift: Components.Schemas.CodeListing, a plain Codable, Hashable, Sendable struct at Sources/DocCArchive/generated/Types.swift:4267-4328, reachable as the .CodeListing case of the generated RenderBlockContent enum (Types.swift:3617).

FieldTypeRequired?Enum values / defaultNote
typestringyes"codeListing"Discriminator; generated as _TypePayload with the single case .codeListing.
code[string]yes—The listing's source lines, one array element per line (no embedded newlines). Maps to the content prop on CodeListing.vue.
syntaxstring, nullableyes (schema-required; may be null)—Highlighter language id, e.g. "swift". Normalized client-side (occ → objective-c) before being handed to the highlighter.
metadataRenderContentMetadatano—Optional anchor/title/abstract/deviceFrame object (spec line 484-503). Presence of anchor is what triggers the renderFigure path in section 1.
copyToClipboardbooleannodefaults to false at the render call siteShows/hides the copy button.
showLineNumbersbooleannodefaults to false (component prop default) / undefined-falsy at the call siteToggles the .code-number gutter spans.
wrapintegernodefaults to 0 at the render call site (0 = no wrap/scroll instead)Max line width in characters; feeds the --wrap-ch custom property.
lineAnnotations[LineAnnotation]nodefaults to [] at the render call siteEach entry has required style (enum "highlight" | "strikeout", spec line 786-789) and required range (array of two Position objects, each with required line and optional character, spec line 802-815).

Note the schema does not declare a fileType property on CodeListing, even though ContentNode.vue's render case reads node.fileType and forwards it as a prop (section 1) — the generated Swift struct therefore has no fileType member either, and this field, if ever emitted, would simply be dropped by strict decoding against this schema.

4. HTML/CSS mapping in DocC-Render output

Root element: <div class="code-listing" data-syntax="…">, with modifier classes single-line (exactly one highlighted line of code) and is-wrapped (wrap > 0), and an inline style="--wrap-ch: N" when wrapping is active (CodeListing.vue#L12-L17). Inside it:

All of the above is scoped SCSS inside components/ContentNode/CodeListing.vue's own <style scoped lang="scss"> block (imports docc-render/styles/_core.scss). Notable SCSS variables consumed, all defined in src/styles/core/_vars.scss: $code-listing-with-numbers-padding (14px 0), $code-number-padding / $code-number-padding-left (8px), and $highlighted-border-width (4px). CSS custom properties consumed at paint time: --color-code-line-highlight and --color-code-line-highlight-border (theme colors, defined per light/dark palette in src/styles/core/colors/_light.scss / _dark.scss), --line-highlight (an optional per-instance override), --color-fill-gray-tertiary/--color-fill-gray (copy-button background), --code-border-radius, --button-border-radius, and --code-indentationWidth (drives the CSS tab-size of the <pre>). When wrapped in a figure (anchor case), the outer markup instead comes from components/ContentNode/Figure.vue — a bare <figure :id="anchor"><slot /></figure> — plus an optional <figcaption> from components/ContentNode/Caption.vue; the CodeListing markup described above still appears unchanged inside that <figure>.

5. Modern CSS-only alternative

Partial

Current behavior, precisely: two independent pieces of client-side JS state exist on CodeListing.vue. (1) copyState (component data, one of idle/success/failure) is set by a click handler on .copy-button that calls navigator.clipboard.writeText and, on success, flips copyState to success for one second via setTimeout before reverting to idle (CodeListing.vue#L216-L230). (2) syntaxHighlightedLines (also component data) is populated asynchronously by an await-ing watcher that loads a highlighter grammar and re-renders each line's <span class="code-line" v-html="line"> once highlighting resolves.

Neither of these is a good match for a purely declarative, state-in-the-DOM native mechanism, for two different reasons, so the verdict is split: