Skip to content

feat: Parse inline markup - #14

Merged
seflue merged 17 commits into
nextfrom
feat/markup
Nov 9, 2026
Merged

seflue merged 17 commits into
nextfrom
feat/markup

Conversation

@seflue

@seflue seflue commented Nov 9, 2026

Copy link
Copy Markdown

Merges feat/markup into next, to be released as 2.1.0.

  • Not breaking: compared with 2.0.4, the grammar only adds node types (bold, italic, underline, strikethrough, code, verbatim, inline_latex, command, latex_argument, latex_optional_argument), so orgmode's queries still load.
  • Tests: all 235 corpus tests pass.
  • Unchanged from feat/markup: the markup nodes and the Emacs conformance fixes from Align markup with Emacs org-element #11.

New in this PR:

  • Merge conflict: feat/markup names the footnote reference node footnote_reference, next names it fnref. The merge keeps fnref, which orgmode uses since 2.0.4 and which pairs with fndef.
  • Footnote fix: a continuation line of a footnote definition that started with markup or a LaTeX command lost its markup. Such a line may now start with them.

After tagging 2.1.0, I'll open a PR in orgmode to use it.

kristijanhusak and others added 17 commits May 3, 2026 20:17
Reference for feat/markup against Emacs org-element behaviour:
- markup_delimiter_rules.tst: pre and post char, body border, headline
  disambiguation
- markup_span_extent.tst: newline handling, paragraph break terminates
- markup_composition.tst: markup with latex, math and literal spans
Two verbatim spans on one line each contain an unpaired $. The
verbatim contents are restricted to expr, so the $ characters must
not be paired into an inline_math_block crossing span boundaries.
Emacs org-element--parse-generic-emphasis requires the char before an
opening emphasis/verbatim/code delimiter to be whitespace, one of
- ( ' " {, or beginning-of-line. The scanner accepted any non-space
pre-char, so mid-word delimiters (foo*bar*) wrongly opened markup.

Only whitespace/BOL is checked, not the - ( ' " { subset: the external
scanner has no lookbehind into characters consumed by the ordinary
expr/str lexing, so those specific punctuation pre-chars can't be
distinguished from an ordinary word character. Whitespace/BOL is
derived from the indent-skip loop already in scan(), which does see
those characters directly.

Fixes DelimiterRules.1 in the markup conformance corpus.
org-element--parse-generic-emphasis accepts [ after a closing delimiter
and forbids only whitespace at the border. The two tests asserted the
correct behavior under titles calling it a divergence.
Emacs org-emphasis-alist and org-element define the equals sign as
verbatim and the tilde as code. The scanner mapped them the other way
round, so the equals span parsed as a code node and the tilde span as
a verbatim node. Node names are public API for highlight and fold
queries.

The fix swaps the two delimiters in the scanner's delimiter to markup
mapping and its inverse. The grammar rules, external token order and
scanner enums keep their names and order, so the diff stays limited to
the two mapping functions. Renaming every enum member and grammar rule
would produce the same parse tree with a larger diff.

Corpus expectations, test titles and sample text follow the swap.
Emacs org-emphasis-regexp-components lists the post set as
- \t.,:!?;'")}\[ or end of line. tree-sitter-org enumerated the
same punctuation but only space, newline and carriage return as
whitespace, so a closing delimiter followed by a tab was rejected.

The post predicate now tests whitespace with iswspace and the Emacs
punctuation with strchr against a string constant. Delimiter chaining
stays a separate term: it is not in the Emacs post set and remains a
deliberate deviation.
Emacs org-emphasis-regexp-components splits emphasis into the fields
pre, border, body, post and newlines. The scanner implements the same
rules but named its predicates after content and marker, which hides
the correspondence.

The border and post predicates now carry the Emacs field names and a
comment block maps all five fields to their places in the scanner. The
border predicate drops the explicit newline and carriage return tests,
both are covered by iswspace. No behaviour change.
The markup delimiter scan decided close, open and plain text in one
function whose precondition (delimiter already consumed) was implicit,
and it derived the delimiter char back from the markup kind although
every caller already knows the char.

Split it into a close step, an open guard and an open walk behind one
entry point that states the precondition. The delimiter char is passed
down instead of derived, which leaves the reverse lookup table unused,
so it is removed. The line end test and the closer test are named
predicates. No behaviour change.
Emacs restricts inline emphasis to a paragraph, the markup stack in
this scanner is cleared at the beginning of every line instead. The
comment on that clear now states why the stack goes stale at all: a
closer swallowed by a link or math token is never requested as a
close, so its entry would otherwise stay forever.

The column was read three times per scan: once for the markup stack
clear, once for the headline check and once inside the bracket branch,
where a second variable shadowed the first. The column is now read
once at the top and reused. The two later reads happened after the
indent skip, so they are replaced by the combination of the initial
read and a zero indent length, which is equivalent at both sites.
A [ can open a link, a footnote reference or a footnote definition.
The scanner handled all three in one nested branch: a link attempt
that found no closing ]] left the lexer at the end of the line and
fell through into the footnote check, which then failed on the line
end. Harmless, since tree-sitter discards the advance calls of a scan
that returns false, but the control flow hid that the fall-through
can never match.

The branch is now a dispatcher that consumes the [ and hands over to
one helper per token kind. The link helper returns false after its
walk, so the footnote helper is only entered when the character after
the [ is an f. The footnote label loop drops the explicit newline
checks, iswspace already covers them. The column-zero test for the
definition is passed in from the caller, which reads the column once.
Deserialize pushes one sentinel entry onto the indent, bullet and
section stacks before it reads the buffer. Serialize therefore writes
those counts as length minus one, while the markup stack has no
sentinel and is written at full length. The asymmetry was unexplained
in the code.

Add a comment stating the rule. Change the loop counter in serialize
to size_t so it matches deserialize and compares unsigned against the
size_t counts instead of mixing signedness.
The external scanner decides whether a markup delimiter opens a span,
closes one or is plain text. It only gets that chance while the
delimiter is still unconsumed: once the internal lexer has emitted the
character as a sym token, tree-sitter never asks the scanner about it.
The expr helper therefore drops the delimiters from both its single
character tokens and the sym class, and a separate low precedence
token in the expr rule collects a lone delimiter the scanner rejected.

Both places now carry a comment stating that rule. No grammar change,
the generated parser is identical.
A continuation line of a footnote definition that started with bold,
italic, underline, strikethrough, code, verbatim or a LaTeX command
lost its markup: the whole line became plain text.

Each continuation line of the description has to start with one of a
fixed set of nodes, so that a line starting with [fn: opens a new
definition instead. That set mirrors the markup choice with plain text
replaced by text that cannot start with [. The markup spans and inline
LaTeX were missing from it, so they could only appear after the first
word of a line. Add them to the set.
A continuation line of a footnote definition may start with any text
except [, which would open a new definition. The character class for
that rule is built from an exclusion list, and [ must be escaped in it.
No corpus case had a continuation line starting with a word or a
symbol, so a broken class went unnoticed until the grammar failed to
generate.

The expected tree matches the released grammar.
@seflue seflue changed the title Merge markup into next feat: Parse inline markup Nov 9, 2026
@seflue
seflue requested a review from kristijanhusak November 9, 2026 18:19
@seflue
seflue merged commit 500fef8 into next Nov 9, 2026
@seflue

seflue commented Nov 19, 2026

Copy link
Copy Markdown
Author

@kristijanhusak Could you please also approve the companion PR #1207 in nvim-orgmode/orgmode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants