-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
enh(parser) new highlight() API #3053
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to output a deprecation message for this?
…s into new_highlight_api
I'm wondering if we could add support for continuation again? We use it for the gerrit project specifically here https://github.com/GerritCodeReview/gerrit/blob/master/polygerrit-ui/app/elements/diff/gr-syntax-layer/gr-syntax-layer.ts#L445. |
This has been removed for many different reasons.
Many of the things we'd like to investigate in v11 may require deep internal changes. We might switch from an iterative parser to a completely recursive parser. There is rethinking sublanguages (#1263), a potential new back-tracking parser (#1140), and more. Just because a line has been processed may not even mean it's processed. We might hit a rule mismatch a few lines later and then rewind and try again with an entirely different matching strategy. To allow us to iterate as freely as possible without worrying about breaking changes that means our internals need to be fully internal. As such To do line-by-line content 100% safely and correctly one should instead post-process the final output (which can be done with ~15 lines of JS or so and a small stack). See my stackoverflow answer. There are other options with the private Emitter API, but use at your own risk. Post-processing the final text output is the easy and most stable choice and what most implementors should do. |
Someone should really write a line by line plugin using Sample ~ implementation: https://github.com/hexojs/hexo-util/pull/246/files#diff-23bd09b492fb22d0b078517e838293bfa0031ece18b7bfba1adf706d5b9592d0R126 One could easily alter it to return an array or whatever, etc. |
@joshgoebel I guess it should be: result = this.hljs.highlight(
baseLine,
{
language: this.baseLanguage,
ignoreIllegals: true
}
);
result.value = result.value.split('\n').map(line => {
const prepend = tokenStack.map(token => `<span class="${token}">`).join('');
const matches = line.matchAll(/(<span class="(.*?)">|<\/span>)/g);
for (const match of matches) {
if (match[0] === '</span>') tokenStack.shift();
else tokenStack.unshift(match[2]);
}
const append = '</span>'.repeat(tokenStack.length);
return prepend + line + append;
}).join('\n');
this.baseRanges.push(this._rangesFromString(result.value, rangesCache)); ? |
You tell me...? I don't completely follow larger context of what you are doing exactly or what baseLine and revisionLine are... if you want access to the individual lines quickly you probably shouldn't be rejoining but rather just leave it as an array so you could slice/splice, etc... the code was just an example of how easy it is to "fix up" the broken tags after splitting on |
@paladox On an unrelated note: https://github.com/gerrit-review/gerrit/blob/master/polygerrit-ui/app/elements/diff/gr-syntax-layer/gr-syntax-layer.js#L349 If these are still actual issues today I'd love to see issues filed and perhaps we can fix them so you don't have to deal with them downstream. |
Resolves #2277.
Changes
highlight(code, options = {})
APIlanguage
andignoreIllegals
are options now (vs params)continuation
is for internal use only and hence not supported any longerCompatibility:
continuation
support from the old API.Checklist
CHANGES.md