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

Feature request: Syntax highlighting for code blocks #2

Closed
littledivy opened this issue Jan 1, 2021 · 3 comments
Closed

Feature request: Syntax highlighting for code blocks #2

littledivy opened this issue Jan 1, 2021 · 3 comments

Comments

@littledivy
Copy link

Awesome module! I've been working on a lightweight syntax highlighter, Hue. Would love to see it being used here :D

@littletof
Copy link
Owner

@littledivy Thanks!

Cool, I will look into it. I think it should work as an extension, so its optional.
To be honest I havent looked into the extension part of it yet, but this could be a good first candidate for it.

@littletof
Copy link
Owner

@littledivy
Extensions got implemented with #6, so it should now be possible to create an extension, that renders codeblocks with hue.

@littletof
Copy link
Owner

littletof commented May 5, 2021

@littledivy
For a crude example, this code below shows an extension, that uses hue to format codeblocks.

import { DefaultTheme } from "https://deno.land/x/hue@0.0.0-alpha.0/themes/mod.ts";
import Typescript from "https://deno.land/x/hue@0.0.0-alpha.0/languages/typescript/typescript.ts";

const CodeBlockExt = {
    generateNode(genFn: any, node: Node, parent: Node, options: Options) {
          if(node.type === 'code') {
            let code: string = node.value;

            switch(node.lang) {
              case 'ts':
                code = new Typescript(node.value, DefaultTheme, { output: "console"}).highlight();
                break;
            }

            // just some formatting
            const lineStart = colors.bgWhite(colors.white(' '));
            let result = 
              colors.bgWhite(colors.black(` codeblock [${node.lang}]`) + colors.white(''.padEnd(45, '.')))  +'\r\n'
              + lineStart+'\r\n'
              + code.split(/\r?\n/).map(hl => lineStart + '\t' + hl).join('\r\n') + '\r\n'
              + lineStart+'\r\n'
              + colors.bgWhite(colors.white(''.padEnd(60, '.'))) + '\r\n';

            return result;
          }
      }
}

let md = '';// your markdown text with codeblocks;
console.log(renderMarkdown(md, {extensions: [CodeBlockExt]}))

results in:

image

Closing this as extensions provide the possibility to do what you requested as seen above, and currently I dont plan to provide official extensions.
Feel free to reopen this issue, if you have more to add to this.

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

2 participants