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

Support diagrams as code with Kroki #45

Open
kazk opened this issue Oct 26, 2021 · 0 comments
Open

Support diagrams as code with Kroki #45

kazk opened this issue Oct 26, 2021 · 0 comments
Milestone

Comments

@kazk
Copy link
Member

kazk commented Oct 26, 2021

Support all the diagrams as code with https://kroki.io/. They provide a free service, but we can self-host if necessary.

This can be in a separate package @codewars/marked-kroki.

The implementation is as simple as:

import { deflate } from 'pako';

const kroki = (base = 'https://kroki.io') => {
  if (base.endsWith('/')) base = base.slice(0, -1);
  return {
    code(code, language) {
      // TODO Support options. `{name} ?foo=1&bar=2`
      // TODO Support captions and alt text
      if (diagramTypes.has(language)) {
        const u8 = deflate(code, { level: 6 });
        const data = base64(String.fromCharCode.apply(null, u8));
        return `<img src="${base}/${language}/svg/${data}">`;
      }
      return false;
    }
  };
};

// Base64 URL safe
const base64 = (s) => btoa(s).replace(/\//g, '_').replace(/\+/g, '-');
// All supported types
const diagramTypes = new Set([
  'graphviz',
  'blockdiag', 'seqdiag', 'actdiag', 'nwdiag', 'packetdiag', 'rackdiag',
  'mermaid', 'ditaa', 'svgbob', 'erd',
  'plantuml', 'c4plantuml', 'nomnoml', 'umlet',
  'vega', 'vegalite',
  'bpmn', 'wavedrom', 'bytefield',
  'excalidraw', 'pikchr', 'structurizr',
]);
import marked from 'marked';
marked.use({ renderer: kroki() });

Input:

```graphviz
digraph G {Hello->World}
```

Output:

<img src="https://kroki.io/graphviz/svg/eJxLyUwvSizIUHBXqPZIzcnJ17ULzy_KSakFAGxACMY=">


@kazk kazk added this to the v1 milestone Oct 26, 2021
@kazk kazk self-assigned this Oct 26, 2021
@kazk kazk removed their assignment Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant