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

Pass additional context to highlight option from fence renderer #626

Closed
KyleMit opened this issue Jan 11, 2020 · 5 comments
Closed

Pass additional context to highlight option from fence renderer #626

KyleMit opened this issue Jan 11, 2020 · 5 comments

Comments

@KyleMit
Copy link

KyleMit commented Jan 11, 2020

This is similar to #456 (Access to the render function from a custom highlight function) but much more limited in scope.

In some markdown flavors like python, you can pass additional parameters at the head of a fenced code block like this:

 ```python hl_lines="1"
 # This line is emphasized
 # This line isn't
 ```

Currently, the options.highlight callback passes two parameters:

options.highlight(token.content, langName)

Where langName is created by taking all the info available at the head of the fenced code block and splitting on the first whitespace character

var info = unescapeAll(token.info).trim()  // python hl_lines="1"
var langName = info.split(/\s+/g)[0]       // python

The only requested delta is to leave the first two parameters exactly as they are and just pass in the rest of the available context as a third so the highlight function can make use of it:

if (options.highlight) {
-    highlighted = options.highlight(token.content, langName);
+    highlighted = options.highlight(token.content, langName, info);

Possible workarounds

The same workaround to #456 is available here to override the default_rules.fence implementation, but that leaves a lot of work in the client's flow

Another available workaround is to delimit by a non-space character when shoving extra info to fenced code blocks like this python,hl_lines="1", and then splitting out the language definition inside the highlight function, but this breaks the language detection for other engines where the raw md might live, like in your editor or github.

@puzrin
Copy link
Member

puzrin commented Jan 11, 2020

Suggestion sounds reasonable, but i'm very conservative about api changes. I see no strong reasons to decline immediately, but i don't like to make this change too :).

I'd stay this open for a while.

@KyleMit
Copy link
Author

KyleMit commented Jan 12, 2020

Seems reasonable - i was able to locally override the fence handler in my own project, so that workaround handles my own workflow. Only needs to be pulled into core if it helps other folks too. Thanks

@rlidwka
Copy link
Member

rlidwka commented Sep 15, 2020

Fixed, remainder of fence params is now passed through to highlighter as 3rd option.

typescript-bot pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this issue Dec 16, 2020
…only keep `@types/highlight.js`) to resolve vulnerability GHSA-7wwv-vh3v-89cq by @UNIDY2002

* Bump dependency `highlight.js` of `types/markdown-it` from 9.7.0 to 10.4.1

* Add support for changed API of `markdown-it` (markdown-it/markdown-it#626) at 12.0.0

* Revert dependency `highlight.js` to ^9.7.0
(So that the TypeScript version can remain 2.0, and will not break the requirements of the dependents of this library.)

* Adjust test code for types/markdown-it
@danieleades
Copy link

@rlidwka will this fix only work if the additional attributes are whitespace-delimited, or will it also work if they're comma-separated?

As in-

```rust,no_run
fn foo(bar: &Bar) {
    // ...
}
```

@puzrin
Copy link
Member

puzrin commented Jul 10, 2021

This is not allowed by CM spec.

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

No branches or pull requests

4 participants