Pure model layer for x-code — a code-display component with an in-house, regex-based syntax tokenizer.
No DOM, no side effects. Every function here is a plain data transform and is exercised directly by model_test.cljs with sparse inputs.
The tokenizer is deliberately lightweight. It covers js / json / css / html only and degrades any other language to plain text. It is a highlighter, not a parser: it does not track nested languages, and a handful of context-sensitive cases are knowingly imperfect (see docs/x-code.md).
Pure model layer for x-code — a code-display component with an in-house, regex-based syntax tokenizer. No DOM, no side effects. Every function here is a plain data transform and is exercised directly by model_test.cljs with sparse inputs. The tokenizer is deliberately lightweight. It covers js / json / css / html only and degrades any other language to plain text. It is a highlighter, not a parser: it does not track nested languages, and a handful of context-sensitive cases are knowingly imperfect (see docs/x-code.md).
(collapsible? max-lines line-count)True when max-lines is positive and the code has more lines than that.
True when `max-lines` is positive and the code has more lines than that.
(dedent s)Remove the longest run of leading whitespace shared by every non-blank line. Lets authors indent slotted code to match surrounding markup without that indentation showing up in the rendered block.
Remove the longest run of leading whitespace shared by every non-blank line. Lets authors indent slotted code to match surrounding markup without that indentation showing up in the rendered block.
(language-label language)Short uppercase badge for a language, or "" for plain text.
Short uppercase badge for a language, or "" for plain text.
(normalize {:keys [code-raw language-raw filename-raw show-copy? line-numbers?
wrap? max-lines-raw expanded? lines]})Normalise raw inputs into the render-ready view-model.
Input keys: :code-raw string | nil (already resolved from property/attr/text) :language-raw string | nil :filename-raw string | nil :show-copy? boolean :line-numbers? boolean :wrap? boolean :max-lines-raw string | nil :expanded? boolean :lines optional precomputed per-line tokens. When supplied (a caller holding a tokenization for the same code + language) the tokenizer is skipped; otherwise :lines is computed from code + language.
Output keys: :code prepared code string :language canonical language id :language-label uppercase badge | "" :filename trimmed filename | "" :show-copy? boolean :line-numbers? boolean :wrap? boolean :max-lines int >= 0 :expanded? boolean :line-count int :lines [[[type text] ...] ...] per-line tokens :collapsible? boolean :header? boolean
Normalise raw inputs into the render-ready view-model.
Input keys:
:code-raw string | nil (already resolved from property/attr/text)
:language-raw string | nil
:filename-raw string | nil
:show-copy? boolean
:line-numbers? boolean
:wrap? boolean
:max-lines-raw string | nil
:expanded? boolean
:lines optional precomputed per-line tokens. When supplied
(a caller holding a tokenization for the same code +
language) the tokenizer is skipped; otherwise :lines is
computed from code + language.
Output keys:
:code prepared code string
:language canonical language id
:language-label uppercase badge | ""
:filename trimmed filename | ""
:show-copy? boolean
:line-numbers? boolean
:wrap? boolean
:max-lines int >= 0
:expanded? boolean
:line-count int
:lines [[[type text] ...] ...] per-line tokens
:collapsible? boolean
:header? boolean(parse-language s)Canonicalise a language attribute. Aliases map onto js/json/css/html;
anything unknown (including nil) becomes text.
Canonicalise a `language` attribute. Aliases map onto js/json/css/html; anything unknown (including nil) becomes `text`.
(parse-max-lines s)Parse the max-lines attribute into a non-negative integer. 0 (the
default for nil / non-numeric / non-positive input) means no collapsing.
Parse the `max-lines` attribute into a non-negative integer. 0 (the default for nil / non-numeric / non-positive input) means no collapsing.
(prepare-code s)Normalise raw code for display: convert CRLF / CR line endings to LF, drop leading/trailing blank lines, then strip common indentation. Non-string input becomes the empty string.
Normalise raw code for display: convert CRLF / CR line endings to LF, drop leading/trailing blank lines, then strip common indentation. Non-string input becomes the empty string.
(tokenize language code)Tokenize code for language into a flat [[type text] ...] vector.
The text language and any unknown language degrade to a single :plain
token spanning the whole input.
Tokenize `code` for `language` into a flat [[type text] ...] vector. The `text` language and any unknown language degrade to a single :plain token spanning the whole input.
(tokens->lines tokens)Split a flat token vector into per-line token vectors. A token whose text spans newlines is split so each line carries only its own slice.
Split a flat token vector into per-line token vectors. A token whose text spans newlines is split so each line carries only its own slice.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |