-
Notifications
You must be signed in to change notification settings - Fork 20
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
COBOL first - Add support for fenced code block markdown syntaxe highlighter #428
Comments
Very nice idea. Thanks. |
Thanks @phaumer, Regarding syntax highlighting in preview mode, according to my research it is managed by the highlight.js component. There are A new language can be registered for
To add a new language for preview in Markdown, you need to contribute to the markdown.markdownItPlugins extension point and in the Instead I overloaded the Which give:
import * as vscode from 'vscode';
import type MarkdownIt from 'markdown-it';
import hljs from 'highlight.js/lib/core';
import hljsCOBOL from 'highlightjs-enterprisecobol';
hljs.registerLanguage('cobol', hljsCOBOL);
export function activate(context: vscode.ExtensionContext) {
//console.log('Congratulations, your extension "markdown-cobol" is now active!');
return {
extendMarkdownIt(md: MarkdownIt) {
const originalHighlight = md.options.highlight || noOriginalHighlight;
md.options.highlight = (str: string, lang: string, attrs: string): string => {
if (lang && lang.match(/\bcobol\b/i) {
return hljs.highlight(str, {language: lang, ignoreIllegals: true}).value;
}
return originalHighlight(str, lang, attrs);
};
return md;
}
};
};
// Should never be called... required for compilation purpose because md.options.highlight is declared optional
function noOriginalHighlight(str: string, lang: string, attrs: string): string {
const preProcess = (str: string) =>
str.replace(/\</g, '<')
.replace(/\>/g, '>');
return `<pre>${preProcess(str)}</pre>`;
}
export function deactivate() { }
...
"contributes": {
"languages": [
{
"id": "cobol-markdown-injection"
}
],
"grammars": [
{
"language": "cobol-markdown-injection",
"scopeName": "markdown.cobol.codeblock",
"path": "./syntaxes/cobol-markdown-injection.json",
"injectTo": [
"text.html.markdown"
],
"embeddedLanguages": {
"meta.embedded.block.cobol": "cobol"
}
}
],
"markdown.markdownItPlugins": true
},
...
"devDependencies": {
"@types/markdown-it": "^14.1.1",
"highlight.js": "^11.10.0",
...
},
"dependencies": {
"highlightjs-enterprisecobol": "^1.0.5",
} The restitution of the syntactic colorization is not identical, and there are some errors (Program-Id)... Try it! |
Description of the enhancement requested
Need, use case: create documentation on COBOL programming using markdown files.
The syntax of the markdown language,
.md
files, allows you to declare fenced blocks of code, and to associate a programming language with them.In this case the block of lines is highlighted using the associated textmate grammar.
This programming language still needs to be recognized by the markdown language interpreter.
For VS Code this interpreter appears to be natively included vscode-markdown-tm-grammar extension.
Unfortunately the COBOL language is not managed there (but for example the SQL language is).
I tried to apply the solution given here: How to add custom language syntax highlighter to markdown code block in VSCode?
and here: vscode-fenced-code-block-grammar-injection-example
I was partially successful in achieving the desired result:
What I have done :
Information returned by the tool
Inspect Editor Tokens and Scopes
:Same for a SQL block code, highlighting native to VS Code:
Something is missing to render the syntax highlighting in preview mode... which remains the desired goal...
Thanks.
The text was updated successfully, but these errors were encountered: