diff --git a/src/pikchr.ts b/src/pikchr.ts new file mode 100644 index 0000000..a3dd64e --- /dev/null +++ b/src/pikchr.ts @@ -0,0 +1,150 @@ + +import { strip_svg } from './svg'; + +export function post_process_pikchr_svg(svg: string, size?: string) { + svg = strip_svg(svg); + + // text + for (const [ color, regex ] of Object.entries(text_regex)) { + svg = svg.replace(regex, ` = { + black: /stroke:rgb\(0,0,0\)/gi, + red: /stroke:rgb\(255,0,0\)/gi, + orange: /stroke:rgb\(255,165,0\)/gi, + yellow: /stroke:rgb\(255,255,0\)/gi, + green: /stroke:rgb\(0,128,0\)/gi, + teal: /stroke:rgb\(0,128,128\)/gi, + pink: /stroke:rgb\(255,192,203\)/gi, + purple: /stroke:rgb\(128,0,128\)/gi, + blue: /stroke:rgb\(0,0,255\)/gi, + indigo: /stroke:rgb\(75,0,130\)/gi, + magenta: /stroke:rgb\(255,0,255\)/gi, + brown: /stroke:rgb\(165,42,42\)/gi, +}; + +const shape_fill_regex = { + black: /\b = { + black: /fill:rgb\(0,0,0\)/gi, + red: /fill:rgb\(255,0,0\)/gi, + orange: /fill:rgb:\(255,165,0\)/gi, + yellow: /fill:rgb:\(255,255,0\)/gi, + green: /fill:rgb:\(0,128,0\)/gi, + teal: /fill:rgb:\(0,128,128\)/gi, + pink: /fill:rgb:\(255,192,203\)/gi, + purple: /fill:rgb:\(128,0,128\)/gi, + blue: /fill:rgb:\(0,0,255\)/gi, + indigo: /fill:rgb:\(75,0,130\)/gi, + magenta: /fill:rgb:\(255,0,255\)/gi, + brown: /fill:rgb:\(165,42,42\)/gi, +}; + +const line_css_vars: Record = { + black: 'var(--theme-line, currentcolor)', + red: 'var(--theme-chart-shape-red-line, currentcolor)', + orange: 'var(--theme-chart-shape-orange-line, currentcolor)', + yellow: 'var(--theme-chart-shape-yellow-line, currentcolor)', + green: 'var(--theme-chart-shape-green-line, currentcolor)', + teal: 'var(--theme-chart-shape-teal-line, currentcolor)', + pink: 'var(--theme-chart-shape-pink-line, currentcolor)', + purple: 'var(--theme-chart-shape-purple-line, currentcolor)', + blue: 'var(--theme-chart-shape-blue-line, currentcolor)', + indigo: 'var(--theme-chart-shape-indigo-line, currentcolor)', + magenta: 'var(--theme-chart-shape-magenta-line, currentcolor)', + brown: 'var(--theme-chart-shape-brown-line, currentcolor)', +}; + +const text_css_vars: Record = { + black: 'var(--theme-text-body, currentcolor)', + red: 'var(--theme-chart-shape-red-line, currentcolor)', + orange: 'var(--theme-chart-shape-orange-line, currentcolor)', + yellow: 'var(--theme-chart-shape-yellow-line, currentcolor)', + green: 'var(--theme-chart-shape-green-line, currentcolor)', + teal: 'var(--theme-chart-shape-teal-line, currentcolor)', + pink: 'var(--theme-chart-shape-pink-line, currentcolor)', + purple: 'var(--theme-chart-shape-purple-line, currentcolor)', + blue: 'var(--theme-chart-shape-blue-line, currentcolor)', + indigo: 'var(--theme-chart-shape-indigo-line, currentcolor)', + magenta: 'var(--theme-chart-shape-magenta-line, currentcolor)', + brown: 'var(--theme-chart-shape-brown-line, currentcolor)', +}; + +const fill_css_vars: Record = { + black: 'var(--theme-chart-shape-black-fill, transparent)', + red: 'var(--theme-chart-shape-red-fill, transparent)', + orange: 'var(--theme-chart-shape-orange-fill, transparent)', + yellow: 'var(--theme-chart-shape-yellow-fill, transparent)', + green: 'var(--theme-chart-shape-green-fill, transparent)', + teal: 'var(--theme-chart-shape-teal-fill, transparent)', + pink: 'var(--theme-chart-shape-pink-fill, transparent)', + purple: 'var(--theme-chart-shape-purple-fill, transparent)', + blue: 'var(--theme-chart-shape-blue-fill, transparent)', + indigo: 'var(--theme-chart-shape-indigo-fill, transparent)', + magenta: 'var(--theme-chart-shape-magenta-fill, transparent)', + brown: 'var(--theme-chart-shape-brown-fill, transparent)', +}; diff --git a/src/renderer.ts b/src/renderer.ts index 7e66c69..e26e614 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -14,6 +14,7 @@ import { bind_data_async } from './async-steps'; import { render_vega_spec } from './vega'; import { parse_attributes } from './attrs'; import { MarkdownOptions } from './render'; +import { post_process_pikchr_svg } from './pikchr'; export function create_renderer(opts: MarkdownOptions) { const renderer = new marked.Renderer(); @@ -275,23 +276,3 @@ function post_process_nomnoml_svg(svg: string, size?: string) { return svg; } - -const svg_text_fill_rgb_000 = /\b