Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code-lang attribute to code element #10580

Closed
extua opened this issue Aug 27, 2024 · 1 comment
Closed

Add code-lang attribute to code element #10580

extua opened this issue Aug 27, 2024 · 1 comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest

Comments

@extua
Copy link

extua commented Aug 27, 2024

What problem are you trying to solve?

In @edent's recent blog post about the code element he mentions that there could be a gap in the HTML spec for defining the programming language of code in a code block.

For example, I would like to mark up the following code snippet to show that it is Rust code:

fn main() {
    println!("Hello World!");
}

The existing lang global attribute is not suitable for this, as it's supposed to be used for human languages, not programming.

What solutions exist today?

According to the current HTML spec

there is no formal way to indicate the language of computer code being marked up.

But,

authors who wish to mark code elements with the language used, e.g. so that syntax highlighting scripts can use the right rules, can use the class attribute, e.g. by adding a class prefixed with "language-" to the element.

The problem with this is that naming classes with a "language-" prefix is not used by browsers, and as the spec mentions, it's mainly for the benefit of third party styling libraries.

Schema.org has a microdata type for SoftwareSourceCode, but this is overly verbose and again, not used by browsers (or styling libraries).

How would you solve it?

I would propose adding a 'code-lang' attribute to the code element, with this example syntax along similar lines suggested by @edent:

<code code-lang="rust;1.80.1">
    fn main() {
        println!("Hello World!");
    }
</code>

The language codes could be standardised on the pygments language short code list, or another standard coding language identifier? Language version would be specified with a semi colon.

Anything else?

Ideally this (might?) allow browsers to read programming languages in code blocks and add syntax highlighting to the default user agent stylesheet, but I recognise that's a very ambitious outcome for this proposal.

@extua extua added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Aug 27, 2024
@domenic
Copy link
Member

domenic commented Aug 27, 2024

Dupe of #7869.

@domenic domenic closed this as completed Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest
Development

No branches or pull requests

2 participants