1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-07-13 01:10:14 +02:00

Make linkeable lines a link to themselves

Currently its already possible to make line lumbers linkeable. Getting
such a link however requires the end user to look at the pages source
and then to manually edit the URL, which is not a great UX.

This PR changes that to make the line numbers a link to themselves, so
clicking on them gives a link to that line that can then be passed
around, similiar to e.G. GitHub.
This commit is contained in:
Alvaro Aleman
2020-11-01 13:47:03 -05:00
committed by Alec Thomas
parent 9eb358bc3f
commit ab61726cdb
3 changed files with 42 additions and 4 deletions

View File

@ -108,6 +108,32 @@ func TestTableLineNumberNewlines(t *testing.T) {
</span>`)
}
func TestLinkeableLineNumbers(t *testing.T) {
f := New(WithClasses(true), WithLineNumbers(true), LinkableLineNumbers(true, "line"))
it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(\"hello world\")\n}\n")
assert.NoError(t, err)
var buf bytes.Buffer
err = f.Format(&buf, styles.Fallback, it)
assert.NoError(t, err)
assert.Contains(t, buf.String(), `id="line1"><a style="outline: none; text-decoration:none; color:inherit" href="#line1">1</a>`)
assert.Contains(t, buf.String(), `id="line5"><a style="outline: none; text-decoration:none; color:inherit" href="#line5">5</a>`)
}
func TestTableLinkeableLineNumbers(t *testing.T) {
f := New(WithClasses(true), WithLineNumbers(true), LineNumbersInTable(true), LinkableLineNumbers(true, "line"))
it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
assert.NoError(t, err)
var buf bytes.Buffer
err = f.Format(&buf, styles.Fallback, it)
assert.NoError(t, err)
assert.Contains(t, buf.String(), `id="line1"><a style="outline: none; text-decoration:none; color:inherit" href="#line1">1</a>`)
assert.Contains(t, buf.String(), `id="line5"><a style="outline: none; text-decoration:none; color:inherit" href="#line5">5</a>`)
}
func TestTableLineNumberSpacing(t *testing.T) {
testCases := []struct {
baseLineNumber int