Skip to content

feat: add data-lexer-name attribute to file-info-entry - #37791

Closed
lavamini wants to merge 1 commit into
go-gitea:mainfrom
lavamini:add-data-lexer-name
Closed

feat: add data-lexer-name attribute to file-info-entry#37791
lavamini wants to merge 1 commit into
go-gitea:mainfrom
lavamini:add-data-lexer-name

Conversation

@lavamini

Copy link
Copy Markdown
Contributor

This makes it easy to customize syntax highlighting.
e.g., in templates/custom/header.tmpl.

/* Lexer name: Go */
:root:has(.file-info-entry[data-lexer-name="Go"]) {
  /* Add custom syntax highlighting */
}

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 20, 2026
{{end}}
{{if .LexerName}}
<div class="file-info-entry">
<div class="file-info-entry" data-lexer-name="{{.LexerName}}">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the new attribute is not used in the current code base, it needs a clear comment to describe why it is added, otherwise it would be removed in the future again when some people find that it is not used.

@wxiaoguang

Copy link
Copy Markdown
Contributor

By the way, if your customized highlighting is a general requirement, it's better to add it in Gitea or upstream Chroma https://github.com/alecthomas/chroma

@lavamini

Copy link
Copy Markdown
Contributor Author

At its core, Chroma is a lexer ported from Python's Pygments rather than being based on an Abstract Syntax Tree (AST). Because of this, its inability to recognize certain syntax is a fundamental flaw baked into its design. To work around this inherent limitation, I am planning to use frontend CSS to manually supplement and patch up the syntax that Chroma fails to recognize.

@wxiaoguang

Copy link
Copy Markdown
Contributor

I am planning to use frontend CSS to manually supplement and patch up the syntax that Chroma fails to recognize.

That's a huge work. Not only frontend CSS, you also need frontend syntax parser, a lot of details then.

By the way, could you show some bad cases which need to be patched?

@lavamini

Copy link
Copy Markdown
Contributor Author
  1. Currently, syntax highlighting applies the exact same color scheme across all languages, and several valid keywords are left unhighlighted. Giving users the option to customize syntax highlighting for individual languages would be a massive improvement.

  2. By allowing users to load custom styles from a local custom directory, this configuration remains completely isolated and won't affect anyone else.

  3. Implementing this is straightforward: we just need to inject a data-lexer-name attribute (e.g., data-lexer-name="go") into the container.

  4. This not only makes things incredibly convenient for users looking to patch specific syntax limitations, but also adds a unique, powerful feature to the project: fully customizable syntax highlighting.

@lavamini

Copy link
Copy Markdown
Contributor Author

This is some bad cases.

some-bad-cases

@wxiaoguang

Copy link
Copy Markdown
Contributor

This is some bad cases.

TBH, IMO it's arguable whethere they are really "bad cases"

If you'd really like to patch them, what's your plan? how would the CSS selector be used?

@lavamini

Copy link
Copy Markdown
Contributor Author
custom-syntax-example

@wxiaoguang

Copy link
Copy Markdown
Contributor

I see your description, but how to?

What will you write to replace the /* Add custom syntax highlighting */? I don't see a feasible approach at the moment.

@lavamini

Copy link
Copy Markdown
Contributor Author

e.g., Port should be tokenized as nv, not nx.

bad-case-Port

@wxiaoguang

Copy link
Copy Markdown
Contributor

e.g., Port should be tokenized as nv, not nx.

So you are fighting with Chroma's highlighting result, such case should be fixed by upstream Chroma.

@wxiaoguang
wxiaoguang marked this pull request as draft May 20, 2026 07:51
@wxiaoguang wxiaoguang added issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail type/upstream This is an issue in one of Gitea's dependencies and should be reported there labels May 20, 2026
@lavamini

Copy link
Copy Markdown
Contributor Author

I do not need to fight with Chroma's highlighting result.
I just need to correct the token color like 'Port'.

@lavamini

Copy link
Copy Markdown
Contributor Author

e.g., Port should be tokenized as nv, not nx.

So you are fighting with Chroma's highlighting result, such case should be fixed by upstream Chroma.

Chroma can't resolve this token issue like 'Port'.
it is an inherent limitation of Chroma.

@lavamini

Copy link
Copy Markdown
Contributor Author

If I want to provide different colors for keywords in different languages, what's a better way?

@wxiaoguang

wxiaoguang commented May 20, 2026

Copy link
Copy Markdown
Contributor

I do not need to fight with Chroma's highlighting result. I just need to correct the token color like 'Port'.

"nx" means "NameOther", you will highlight all "nx" symbols appearing in Golang source code in the end, it's impossible to only highlight the fields like "Port" in the struct.


I don't think it is a right approach, while I won't block this change as long as it doesn't cause new problems and it is maintainable ( #37791 (review) )

@lavamini

Copy link
Copy Markdown
Contributor Author

/* ok, this may be a joke, but I'm working on it */

github-light-theme

@wxiaoguang

wxiaoguang commented May 20, 2026

Copy link
Copy Markdown
Contributor

/* ok, this may be a joke, but I'm working on it */

@wxiaoguang wxiaoguang closed this Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. type/upstream This is an issue in one of Gitea's dependencies and should be reported there

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants