10 Commits

Author SHA1 Message Date
ac3c25150e 0.1.11 2023-05-09 21:48:08 -07:00
ac4d6786d3 fix regexes 2023-05-09 21:47:56 -07:00
b3c9ac0c5c 0.1.10 2023-05-09 21:38:38 -07:00
9d233a8dc3 fix regexes 2023-05-09 21:38:23 -07:00
126803c489 0.1.9 2023-05-09 21:32:55 -07:00
d9c3371245 first pass at color name => theme color processing for pikchr 2023-05-09 21:32:48 -07:00
3553ced719 0.1.8 2023-05-07 17:20:08 -07:00
715451f02f fix frontmatter parse flag 2023-05-07 17:19:55 -07:00
b2de8df538 0.1.7 2023-05-06 15:21:35 -07:00
25ae8b0bcb remove console.log 2023-05-06 15:21:31 -07:00
6 changed files with 174 additions and 42 deletions

39
package-lock.json generated
View File

@@ -1,10 +1,12 @@
{
"name": "markdown-to-html",
"name": "@doc-utils/markdown2html",
"version": "0.1.11",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "markdown-to-html",
"name": "@doc-utils/markdown2html",
"version": "0.1.11",
"dependencies": {
"bytefield-svg": "^1.6.1",
"dompurify": "^2.3.6",
@@ -18,6 +20,9 @@
"vega": "^5.22.1",
"yaml": "^2.2.2"
},
"bin": {
"markdown2html": "bin/markdown2html"
},
"devDependencies": {
"@types/dompurify": "^2.3.3",
"@types/jsdom": "^20.0.0",
@@ -28,8 +33,7 @@
"@types/prismjs": "^1.26.0",
"@types/qrcode": "^1.5.0",
"typescript": "^5.0.4"
},
"version": "0.1.6"
}
},
"node_modules/@tootallnate/once": {
"version": "2.0.0",
@@ -82,15 +86,15 @@
"dev": true
},
"node_modules/@types/marked": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.0.8.tgz",
"integrity": "sha512-HVNzMT5QlWCOdeuBsgXP8EZzKUf0+AXzN+sLmjvaB3ZlLqO+e4u0uXrdw9ub69wBKFs+c6/pA4r9sy6cCDvImw==",
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.3.0.tgz",
"integrity": "sha512-zK4gSFMjgslsv5Lyvr3O1yCjgmnE4pr8jbG8qVn4QglMwtpvPCf4YT2Wma7Nk95OxUUJI8Z+kzdXohbM7mVpGw==",
"dev": true
},
"node_modules/@types/node": {
"version": "18.16.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz",
"integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==",
"version": "18.16.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.5.tgz",
"integrity": "sha512-seOA34WMo9KB+UA78qaJoCO20RJzZGVXQ5Sh6FWu0g/hfT44nKXnej3/tCQl7FL97idFpBhisLYCTB50S0EirA==",
"dev": true
},
"node_modules/@types/prismjs": {
@@ -2011,15 +2015,15 @@
"dev": true
},
"@types/marked": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.0.8.tgz",
"integrity": "sha512-HVNzMT5QlWCOdeuBsgXP8EZzKUf0+AXzN+sLmjvaB3ZlLqO+e4u0uXrdw9ub69wBKFs+c6/pA4r9sy6cCDvImw==",
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.3.0.tgz",
"integrity": "sha512-zK4gSFMjgslsv5Lyvr3O1yCjgmnE4pr8jbG8qVn4QglMwtpvPCf4YT2Wma7Nk95OxUUJI8Z+kzdXohbM7mVpGw==",
"dev": true
},
"@types/node": {
"version": "18.16.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz",
"integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==",
"version": "18.16.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.5.tgz",
"integrity": "sha512-seOA34WMo9KB+UA78qaJoCO20RJzZGVXQ5Sh6FWu0g/hfT44nKXnej3/tCQl7FL97idFpBhisLYCTB50S0EirA==",
"dev": true
},
"@types/prismjs": {
@@ -3482,6 +3486,5 @@
"decamelize": "^1.2.0"
}
}
},
"version": "0.1.6"
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@doc-utils/markdown2html",
"version": "0.1.6",
"version": "0.1.11",
"publishConfig": {
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
},

View File

@@ -88,7 +88,5 @@ export function parse_attributes(text: string, fallback_id?: Lazy<string>) {
}
}
console.log(attrs);
return attrs;
}

View File

@@ -20,7 +20,7 @@ export function process_frontmatter(raw_document: string, parse = true) {
const formatter_yaml = raw_document.slice(3, end_index);
const document = raw_document.slice(end_index + 4);
const frontmatter = parse ? formatter_yaml : yaml.parse(formatter_yaml);
const frontmatter = parse ? yaml.parse(formatter_yaml) : formatter_yaml;
return { frontmatter, document };
}

150
src/pikchr.ts Normal file
View File

@@ -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, (_, $1) => `<text${$1}fill="${text_css_vars[color]}"`);
}
// shape fill
for (const [ color, regex ] of Object.entries(shape_fill_regex)) {
svg = svg.replace(regex, (_, $1) => `<path${$1}style="fill:${fill_css_vars[color]}`);
}
// arrow heads
for (const [ color, regex ] of Object.entries(arrow_head_regex)) {
svg = svg.replace(regex, `fill:${line_css_vars[color]}`);
}
// lines / boxes
for (const [ color, regex ] of Object.entries(line_regex)) {
svg = svg.replace(regex, `stroke:${line_css_vars[color]}`);
}
return svg;
}
type CSSVarColor
= 'black'
| 'red'
| 'orange'
| 'yellow'
| 'green'
| 'teal'
| 'pink'
| 'purple'
| 'blue'
| 'indigo'
| 'magenta'
| 'brown'
;
const text_regex: Record<CSSVarColor, RegExp> = {
black: /<text( | [^>]+ )fill="rgb\(0,0,0\)"/gi,
red: /<text( | [^>]+ )fill="rgb\(255,0,0\)"/gi,
orange: /<text( | [^>]+ )fill="rgb\(255,165,0\)"/gi,
yellow: /<text( | [^>]+ )fill="rgb\(255,255,0\)"/gi,
green: /<text( | [^>]+ )fill="rgb\(0,128,0\)"/gi,
teal: /<text( | [^>]+ )fill="rgb\(0,128,128\)"/gi,
pink: /<text( | [^>]+ )fill="rgb\(255,192,203\)"/gi,
purple: /<text( | [^>]+ )fill="rgb\(128,0,128\)"/gi,
blue: /<text( | [^>]+ )fill="rgb\(0,0,255\)"/gi,
indigo: /<text( | [^>]+ )fill="rgb\(75,0,130\)"/gi,
magenta: /<text( | [^>]+ )fill="rgb\(255,0,255\)"/gi,
brown: /<text( | [^>]+ )fill="rgb\(165,42,42\)"/gi,
};
const line_regex: Record<CSSVarColor, RegExp> = {
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: /<path( | [^>]+ )style="fill:rgb\(0,0,0\)/gi,
red: /<path( | [^>]+ )style="fill:rgb\(255,0,0\)/gi,
orange: /<path( | [^>]+ )style="fill:rgb\(255,165,0\)/gi,
yellow: /<path( | [^>]+ )style="fill:rgb\(255,255,0\)/gi,
green: /<path( | [^>]+ )style="fill:rgb\(0,128,0\)/gi,
teal: /<path( | [^>]+ )style="fill:rgb\(0,128,128\)/gi,
pink: /<path( | [^>]+ )style="fill:rgb\(255,192,203\)/gi,
purple: /<path( | [^>]+ )style="fill:rgb\(128,0,128\)/gi,
blue: /<path( | [^>]+ )style="fill:rgb\(0,0,255\)/gi,
indigo: /<path( | [^>]+ )style="fill:rgb\(75,0,130\)/gi,
magenta: /<path( | [^>]+ )style="fill:rgb\(255,0,255\)/gi,
brown: /<path( | [^>]+ )style="fill:rgb\(165,42,42\)/gi,
}
const arrow_head_regex: Record<CSSVarColor, RegExp> = {
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<CSSVarColor, string> = {
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<CSSVarColor, string> = {
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<CSSVarColor, string> = {
black: 'var(--theme-bg-heavy, 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)',
};

View File

@@ -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<text fill="rgb\(0,0,0\)"/gi;
const svg_fill_rgb_000 = /fill:rgb\(0,0,0\)/gi;
const svg_stroke_rgb_000 = /stroke:rgb\(0,0,0\)/gi;
// todo: css variables
function post_process_pikchr_svg(svg: string, size?: string) {
svg = strip_svg(svg);
// text
svg = svg.replace(svg_text_fill_rgb_000, '<text fill="var(--theme-text-body, currentcolor)"');
// arrow heads
svg = svg.replace(svg_fill_rgb_000, 'fill:var(--theme-line, currentcolor)');
// lines / boxes
svg = svg.replace(svg_stroke_rgb_000, 'stroke:var(--theme-line, currentcolor)');
return svg;
}