vienalatina/apps/board/render.py
Claude 8e1ef8831c
The writing box shows the formatting, and raw HTML stops reaching the site
The box is now the finished text: bold is bold, a heading is a heading, a
list is a list. No preview pane — a second box beside the first is the
wrong shape on a phone — and nothing is sent anywhere while anybody
types, so the round trip the old «Vista previa» button made on every
press is gone.

Markdown is still the only thing stored, which is what keeps the file in
git, the translation pipeline, Decap and the XSS defence exactly as they
were. The two directions are deliberately not symmetrical:

  opening  — the server renders the stored markdown into a <template>
             the editor adopts, so there is no markdown parser in the
             browser and no second opinion about what text means;
  saving   — the browser walks a closed set of elements back into
             markdown. Anything else contributes its text and nothing
             more, which is why a paste brings no markup in.

Three heading levels (## ### ####), bold, italic, strikethrough, two
kinds of list, quote, link and clear formatting on the four post boxes;
bold, italic, link and clear on the short ones. A «Markdown» button
swaps to the raw text and back — the escape hatch, and the way to write
a table. With JavaScript off the plain textarea is still there and still
the field that submits; verified by driving the form with scripting
disabled.

Headings had no styles anywhere. `.article-single__body` styled `p` and
nothing else, so three levels would have rendered as three identical
bold lines. Both stylesheets now carry the ladder, along with lists,
quotes and rules.

Two renderer findings, both of which richer writing makes worse:

  - config.yaml set Goldmark `unsafe: true`, for WordPress content that
    no longer exists — no file under content/ holds a tag. A member's
    proposal containing `<img src=x onerror=…>` renders as visible
    characters in the moderation queue, which is the one screen a
    moderator judges it on, and that flag is what would have run it on
    the live site. Off now; the built output is byte-identical.
  - A line break meant two things: `breaks: True` here, Goldmark's
    hardWraps off there. Turning hardWraps on was the obvious fix and
    the wrong one — the files are hard-wrapped at 78 characters, so it
    would break every sentence in half. Instead there are two renderers,
    one per destination, and the editor writes the hard break CommonMark
    means (two spaces) so it survives either way.

Tested by running the serializer itself, in node, against the same
static/board.js the browser loads: 26 cases including the ones that go
wrong — a pasted span, a script, an empty box, a line starting with «-»,
a javascript: href — plus a round trip asserting markdown → HTML →
markdown renders identically for ten real documents.

Then by typing in a browser at 390px with touch emulation, which is what
found the one defect no fixture predicted: pressing Enter in a list
wraps it in a <div>, and reading that as a paragraph ran «uno» and «dos»
together into one word.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
2026-10-06 20:07:18 +00:00

61 lines
2.5 KiB
Python

"""Turning what members type into HTML.
`html=False` is the whole security model, and it is worth understanding rather
than copying. With raw HTML disabled, markdown-it never passes a fragment of
the input through untouched: it emits only the tags its own rules produce, and
everything else is escaped as text. A `<script>` in a post comes out as visible
characters, not as a tag.
That is why there is no sanitiser here. A sanitiser is what you need when you
have decided to allow *some* HTML and must then decide which — a judgement with
a long history of near misses. Allowing none is a smaller thing to get right.
Anyone tempted to set `html=True` later to embed a video: that single flag
turns this file into an XSS hole, and re-enabling it means adding a sanitiser
and owning its allowlist forever.
"""
from __future__ import annotations
from markdown_it import MarkdownIt
from markupsafe import Markup
def _renderer(breaks: bool) -> MarkdownIt:
return (
MarkdownIt("commonmark", {"html": False, "linkify": True, "breaks": breaks})
.enable("linkify")
.enable("table")
.enable("strikethrough")
)
# Two of them, because a line break means two different things depending on
# where the text is going.
#
# On the wall, in a private message, in a calendar entry — text this app both
# stores and renders — people write the way they write to each other, and a
# newline they typed is a newline they meant. `breaks=True`.
#
# A public post is a file in the repository rendered by Hugo, whose Goldmark
# has hardWraps off; and the files are hard-wrapped at about 78 characters,
# by the pipeline and by anything that has ever edited them. Rendering those
# with breaks on puts a line break in the middle of every sentence, so the
# editor would show something the site will never produce. `breaks=False`
# there, and a break somebody actually wants is written the way CommonMark
# writes one — two spaces at the end of the line — which both renderers honour.
_md = _renderer(True)
_md_site = _renderer(False)
def to_html(text: str, breaks: bool = True) -> Markup:
"""Markdown as HTML. `breaks=False` for anything bound for the public site."""
return Markup((_md if breaks else _md_site).render(text or ""))
def excerpt(text: str, limit: int = 220) -> str:
"""A plain-text preview for the thread list — no markup, no truncated tags."""
flat = " ".join((text or "").split())
if len(flat) <= limit:
return flat
return flat[:limit].rsplit(" ", 1)[0] + " …"