feat: add source line number toggle by masalha-alaa · Pull Request #5111 · marktext/marktext · GitHub
Skip to content

feat: add source line number toggle - #5111

Merged
Jocs merged 9 commits into
marktext:developfrom
masalha-alaa:feat/toggle-line-numbers
Sep 15, 2026
Merged

Jocs merged 9 commits into
marktext:developfrom
masalha-alaa:feat/toggle-line-numbers

Conversation

@masalha-alaa

@masalha-alaa masalha-alaa commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #4033

Summary

Adds a View → Toggle Line Numbers option directly below Toggle Table of Contents.

The option shows or hides line numbers in Source Code Mode. When enabled, every line is numbered using CodeMirror’s existing line-number font and color. The preference is persisted and defaults to enabled to preserve the existing behavior.

Locale entries and automated tests are included.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added
  • Manually tested on: Electron could not launch in the sandbox because Chromium process sockets are restricted.

Automated validation completed:

  • 738 unit tests passed
  • Lint passed
  • TypeScript checks passed
  • Production Electron build passed

Notes for reviewers [optional]

The focused E2E test covers menu placement, checked state, live hiding and showing, and sequential line numbering. It is included but could not be executed because of the sandbox limitation described above.


By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](LICENSE).

@Jocs

Jocs commented Sep 2, 2026

Copy link
Copy Markdown
Member

@Jocs Jocs added the 🛑 blocked This issue or PR is blocked by other issue label Sep 2, 2026
@masalha-alaa

Copy link
Copy Markdown
Contributor Author

Done

@Jocs

Jocs commented Sep 3, 2026

Copy link
Copy Markdown
Member

Since toggling whether to show line numbers isn’t something users do frequently, I’d prefer to add this option to the Settings, perhaps under the Editor tab?

@Jocs

Jocs commented Sep 3, 2026

Copy link
Copy Markdown
Member

The test CI failed. We need to investigate what went wrong?

@masalha-alaa

Copy link
Copy Markdown
Contributor Author

I grouped it with the other table of contents toggle button only because it falls under the same category (view & toggle), but we can move it to settings and maybe assign a keyboard shortcut for it.

CI should pass now.

@Jocs Jocs removed the 🛑 blocked This issue or PR is blocked by other issue label Sep 3, 2026
@Jocs

Jocs commented Sep 3, 2026

Copy link
Copy Markdown
Member

There were no keyboard shortcuts for the other settings either, so we don't need to add one here.

Jocs and others added 6 commits September 15, 2026 10:22
Line numbers are a rarely toggled display setting, so they belong in
Preferences rather than the View menu. The menu toggle also stored its
state per window and never persisted it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Show or hide the CodeMirror gutter in Source Code mode from
Preferences > Editor > Miscellaneous. The value is persisted and
broadcast to every window, and defaults to on.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
formatSourceLineNumber returned its input unchanged, which is exactly
CodeMirror 5's default lineNumberFormatter.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Checks that every line is numbered by default, that the Preferences
switch hides the whole gutter live and for a newly mounted editor, and
that turning it back on numbers every line again.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Jocs

Jocs commented Sep 15, 2026

Copy link
Copy Markdown
Member

@Jocs
Jocs merged commit a28beb5 into marktext:develop Sep 15, 2026
10 checks passed
Jocs added a commit that referenced this pull request Sep 21, 2026
…5483) (#5484)

* feat(desktop): follow the math preferences in source mode (#5483)

The `markdown-math` mode has never read a preference. `codeMirror/index.ts`
registers it at module load and `sourceCode.vue` asked for it by name, so the
source view delegated `$…$` to stex whether or not `texMathDollars` was on.

That went unnoticed because the preference ships enabled, but #5446 added
three more math syntaxes and they ship disabled — the same shortcut would be
wrong in the opposite direction for them. So the mode now takes its regions
from the mode spec, which is where the delimiters that follow can join.

Visible change, and the intended one: with `texMathDollars` off, the source
view stops treating `$` as a delimiter, matching what the editor has done
since #5450. That is what #2002 and #5243 asked for — the latter reported
patching app.asar to get it.

The factory's defaults reproduce the old behaviour, so a caller still passing
the bare string `'markdown-math'` is unaffected. A fresh CodeMirror instance
reads the preferences at mount (`index.vue` builds the source view behind a
`v-if`, so every entry is a new one); the watch covers a preference changed
while the view is already open, mirroring the one #5111 added for
`sourceCodeLineNumbers`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(desktop): apply pandoc's dollar-math constraints to the source view guard (#5483)

The inline `$…$` open guard said it mirrored Muya's `inline_math` rule, and
pointed at `src/muya/lib/parser/rules.js` — a path that belongs to the engine
this editor no longer uses. It drifted further when #5449 gave the editor
pandoc's three `tex_math_dollars` constraints, which the guard never gained.

So the source view still opened a math span on `Revenue rose from $13B to
$24B.` while the editor read it as prose — the currency case #2002 and #5243
reported, on the very input #5446 used to describe the problem.

The guard now carries the same three constraints: a non-space right after the
opener, a non-space right before the closer, and no digit right after it. It
still requires a closer on the line, which is this view's own rule rather than
pandoc's: CodeMirror tokenizes per line and the multiplexer's state survives
across them, so an unclosed opener would render the rest of the file as LaTeX.

The guard only decides whether to enter stex. The region's closer is the plain
string `$` and leaves on the next one without re-checking; a closer regexp
would need the text before it, which `StringStream.match` does not expose.

The existing currency case passed throughout: the old guard opened its span at
`$5` and closed it before `only`, which is what the assertion looked for. It
now checks the span contents instead.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(desktop): highlight the backslash TeX math delimiters in source mode (#5483)

`\(…\)`, `\[…\]`, `\\(…\\)` and `\\[…\\]` render as formulas in the WYSIWYG
view since #5481 but were plain prose in the source view — and worse than
plain: `markdown-math` exists because the markdown mode reads the underscores
in `\(\text{F}_\text{A}\)` as emphasis (#4121), which it was still doing for
these.

Each pair now gets a stex region, riding the preference that governs it.

Notes:

* The double-backslash regions are registered first. The multiplexer takes the
  first spec matching at the cursor, and the literal `\(` matches at the second
  character of `\\(`.
* The inline forms need their own open guards, not `INLINE_MATH_OPEN`: part of
  that one requires the character before the closer not to be a backslash, and
  for `\)` it is one. The content rules come from Muya's own regexes, including
  the two escape rules that differ between the extensions, so both views draw
  the same span. Display math legitimately spans lines, so like `$$` it carries
  no guard.
* Known limit, commented at the guards: `\\(x\)` — a double-backslash opener
  closed with a single-backslash closer — is read as math when only the single
  extension is on. The multiplexer searches a regexp against the rest of the
  line, so a guard cannot see the backslash in front of the opener. The shape
  is invalid in both dialects, and the well-formed `\\(x\\)` is unaffected.
* `- \[TODO\] item` becomes a formula once the extension is on. That is pandoc's
  documented drawback — the extension "precludes escaping `(` and `[`" — the
  editor agrees, and it is why both preferences ship disabled.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor(desktop): trim the markdown-math mode to the comments that earn it

The file had grown to 42% comments, several of them restating the code or
recording archaeology (why the outer mode is gfm, what `setMode` used to
resolve to) rather than anything a reader needs.

The three delimiter pairs also carried three copies of the same two region
literals. Folding them into one helper removes the duplication and, with it,
the comments that existed to explain it — including the note about display
regions preceding inline ones, which was never load-bearing.

What stays is what the code cannot say: why this mode exists at all, the
constraints each open guard encodes and where they come from, why the
backslash guards cannot reuse the dollar one, the ordering the multiplexer
demands, and the one shape it reads differently from the editor.

156 lines to 111; no behaviour change.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(desktop): stop a backslash math region claiming the other one's block (#5483)

With only `texMathSingleBackslash` on, a `\\[…\\]` block highlighted as math —
the very syntax that preference does not cover. The editor reads `\\` there as
an escaped backslash and finds no formula, so the two views disagreed exactly
where this change set out to make them agree.

The display openers span lines, so unlike the inline ones they carry no closer
lookahead. `\[` was a plain string, and `\\[` contains it one character in: the
multiplexer found it at that offset and opened a region that ran to the
matching `\]`, swallowing the block whichever way the double-backslash
preference was set.

Every opener now refuses a preceding backslash. That needs the openers to be
regexps, and regexps could not see what precedes them: `indexOf` in our
vendored `multiplexingMode` searched `string.slice(from)`, hiding the text
before the cursor. It now searches the whole line from an offset, which keeps
lookbehind working — at the cost of `^` in an opener anchoring to the start of
the line rather than to the cursor, noted there. No opener in this codebase is
anchored.

The same guard retires the limitation the previous commit documented: `\\(x\)`
is no longer read as single-backslash math either.

The regression test sets its own preferences rather than leaning on the test
before it — with the extension off the assertion is vacuous, which is how a
first version of it passed against the unfixed code.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(desktop): set the math delimiters back from the formula in source mode (#5483)

`$`, `\(`, `\[` and their closers carried the multiplexer's `delimStyle`
classes but no rule ever painted them, so a delimiter was as loud as the
formula it wrapped — unlike the WYSIWYG view, which greys its own markers.

They now keep the surrounding colour at reduced opacity. Fading what is
inherited rather than naming a hue is what makes this work across the three
CodeMirror themes the app ships and the thirty-odd MarkText palettes behind
them; no single colour could sit on both `#ffffff` and `#161616`.

0.65 is the lowest value that clears 3:1 — the threshold this sort of
de-emphasised punctuation is held to — in every shipped theme. Measured:
light 7.0, graphite 6.85, ulysses 6.72, tokyo-night-light 6.0, oxocarbon-dark
7.28, synthwave-84 6.57, material-dark 5.43, one-dark 3.67. one-dark sets the
floor because its body text only reaches 6.57:1, so anything fainter
disappears there long before it does anywhere else.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(desktop): give GitHub inline math its own source-view region (#5483)

`` $`e=mc^2`$ `` opens on `$`, so the dollar rule claimed the span and handed
the backticks to stex as part of the formula — the same mis-reading #5457 fixed
in the editor, still on show in the source view, and unaffected by the
`texMathGfm` preference either way.

It now has its own region, offered ahead of the dollar ones because its opener
is the longer of the two. With the extension off the dollar rule keeps the span
exactly as before, which is what `tex_math_dollars` alone prescribes and what
the editor does.

The three delimiter pairs were building their regions through one helper that
took a display pair and an inline pair together; gfm has no display half, so
the helper splits into `displayRegion` and `inlineRegion` and each extension
pushes what it actually has.

Out of scope, and unchanged: ```` ```math ```` is a fenced code block rather
than a delimiter pair, so it reaches stex through a different route than a
multiplexer region.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs: trim the source-mode math comments to what the code cannot say

144 comment lines across the four files this PR touches, down to 102. What
went was restatement and detail a reader does not need to act on: how
`StringStream.match` hides context, why display openers need the guard more
than inline ones, that inner modes are imported eagerly.

Two comments had drifted from the code they describe — later edits inserted
the gfm opener between the backslash comment and its constants, and the
delimiter-contrast note between the currency comment and its test. Both are
back above what they explain, and one had picked up doubled backslashes on the
way through a script.

Comments only; no behaviour change.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Wangqkoe pushed a commit to Wangqkoe/marktext that referenced this pull request Oct 21, 2026
…reference (marktext#4033) (marktext#5111)

Source Code mode now numbers every line instead of only line 1 and every
tenth line. CodeMirror sizes the gutter from the last line's label, which
the old formatter usually left empty, so three-digit numbers also no
longer run into the text.

A new `sourceCodeLineNumbers` preference (Preferences > Editor >
Miscellaneous, on by default) shows or hides the whole gutter. It is
persisted and broadcast to every window, and the source editor applies it
both on mount and live.

Closes marktext#4033
Closes marktext#2929

Co-authored-by: Jocs <luoran1988@126.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
(cherry picked from commit a28beb5)
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.

Add line numbering for every line in Source Code Mode instead of every 5 lines

2 participants