Skip to content

Commit 90f5407

Browse files
committedDec 2, 2024
new strategies
1 parent 2f24b66 commit 90f5407

File tree

8 files changed

+258
-165
lines changed

8 files changed

+258
-165
lines changed
 

‎packages/demo/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
- create special code hook, which would produce 2 tabs:
66
- diagram
77
- code for the diagram
8+
- maybe
9+
- https://github.com/withastro/starlight/blob/main/docs/src/components/component-preview.astro
10+
- https://starlight.astro.build/components/code/

‎packages/demo/astro.config.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const cache = await getCache();
1414
const className = "not-content";
1515
const conf = {
1616
cache,
17-
strategy: "f-img-class-dark-mode",
17+
strategy: "file",
18+
darkScheme: "class",
1819
// do not use .beoe for Netlify deployments
1920
fsPath: "public/beoe",
2021
webPath: "/beoe",

‎packages/demo/src/content/docs/examples/mermaid-test.md

+14-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ title: mermaid test
33
draft: true
44
---
55

6-
## rehype-mermaid
76

8-
```mermaid
9-
graph LR
10-
accTitle: Big Decisions
11-
accDescr: Bob's Burgers process for making big decisions
12-
A[Identify Big Decision] --> B{Make Big Decision}
13-
B --> D[Be done]
14-
```
7+
## links
158

169
```mermaid strategy=inline class=not-content
10+
flowchart LR
11+
A-->B
12+
B-->C
13+
C-->D
14+
click A callback "Tooltip for a callback"
15+
click B "https://www.github.com" "This is a tooltip for a link"
16+
click C call callback() "Tooltip for a callback"
17+
click D href "https://www.github.com" "This is a tooltip for a link"
18+
```
19+
20+
## other
21+
22+
```mermaid
1723
graph LR
1824
accTitle: Big Decisions
1925
accDescr: Bob's Burgers process for making big decisions

‎packages/demo/src/content/docs/index.md

+84-58
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,21 @@
22
title: Diagram All The Things
33
---
44

5-
## Strategies
6-
7-
### inline
8-
9-
- **pros**
10-
- interactivity
11-
- links (`a href`)
12-
- <Kbd>Cmd</kbd> + <Kbd>F</kbd>
13-
- with JS, for example, if we inline JSON data
14-
- need to be careful with SVGO to not remove IDs etc.
15-
- dark theme
16-
- class based
17-
- `prefers-color-scheme`
18-
- allows to add dark theme to tools that don't support it out of the box (`graphviz`, `vizdom`, `gnuplot`)
19-
- **cons**
20-
- CSS conflicts
21-
- CSS from website can affect diagram
22-
- potential solution `.not-content`
23-
- CSS from diagram can affect website (d2 diagrams)
24-
- this can break dark theme or other diagrams on the smae page
25-
- potential solution use unique prefix to make sure there is no clash
26-
- Dark theme can be limited (black and white)
27-
- DOM footprint
28-
- easy to integrate, especially if there is no inline CSS in SVG
29-
30-
### data-url
31-
32-
- **pros**
33-
- no CSS conflicts
34-
- dark theme
35-
- class based. Two images with two classes and only one shown at a time depending on the theme
36-
- `prefers-color-scheme`: `picture` + `source media="(prefers-color-scheme: dark)` + `img`
37-
- no DOM footprint
38-
- **cons**
39-
- no interactivity
40-
- no DOM footprint, but overall weight of HTML is high as in previous strategy
41-
- easy to integrate - only need to add a bit of CSS to support dark theme
42-
43-
### file
44-
45-
- **pros**
46-
- no CSS conflicts
47-
- dark theme
48-
- class based. Two images with two classes and only one shown at a time depending on the theme
49-
- `prefers-color-scheme`: `picture` + `source media="(prefers-color-scheme: dark)` + `img`
50-
- no DOM footprint
51-
- lighter weight of HTML compared to previous strategy
52-
- **cons**
53-
- no interactivity
54-
- some tools don't support dark theme out of the box (`graphviz`, `vizdom`, `gnuplot`)
55-
- harder to integrate - need to resolve where to write file, how to do cache busting...
56-
57-
---
58-
59-
# TODO
5+
## TODO
6+
7+
- [ ] documentation about shared options
8+
- `strategy`
9+
- `darkScheme`
10+
- `cache`
11+
- `class`
12+
- `svgo`
13+
- [ ] documentation about meta
14+
- `strategy`, `class`
15+
- `alt` (not implemented yet)
16+
- [ ] documentation and examples about interactivity
17+
- [ ] tips about styles and other things
18+
- [ ] revamp site, maybe `template: splash hero: ...` for index page
19+
- [ ] publish new version
6020

6121
## Strategy
6222

@@ -72,15 +32,15 @@ title: Diagram All The Things
7232

7333
```html
7434
<figure class="beoe">
75-
<img src="data:image/svg+xml,..." width="..." height="..." alt="..." />
35+
<img src="data:image/svg+xml,..." width="..." height="..." />
7636
</figure>
7737
```
7838

7939
### `file`
8040

8141
```html
8242
<figure class="beoe">
83-
<img src="/path/to.svg" width="..." height="..." alt="..." />
43+
<img src="/path/to.svg" width="..." height="..." />
8444
</figure>
8545
```
8646

@@ -105,7 +65,7 @@ title: Diagram All The Things
10565
</figure>
10666
```
10767

108-
You would need to add CSS, something like this
68+
You would need to add CSS, something like this:
10969

11070
```CSS
11171
html[data-theme="light"] .beoe-dark {
@@ -151,3 +111,69 @@ html[data-theme="dark"] .beoe-light {
151111
- Links (`<a href="...">`)
152112
- Search text in the diagram (<Kbd>Cmd</kbd> + <Kbd>F</kbd>)
153113
- With JS, for example, if we inline JSON data
114+
115+
## Accessibility
116+
117+
````md
118+
```some alt="..."
119+
120+
```
121+
````
122+
123+
```html
124+
<img alt="..." />
125+
```
126+
127+
I can add option to [figcaption](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption) as well.
128+
129+
## Old notes
130+
131+
### inline
132+
133+
- **pros**
134+
- interactivity
135+
- links (`a href`)
136+
- <Kbd>Cmd</kbd> + <Kbd>F</kbd>
137+
- with JS, for example, if we inline JSON data
138+
- need to be careful with SVGO to not remove IDs etc.
139+
- dark theme
140+
- class based
141+
- `prefers-color-scheme`
142+
- allows to add dark theme to tools that don't support it out of the box (`graphviz`, `vizdom`, `gnuplot`)
143+
- **cons**
144+
- CSS conflicts
145+
- CSS from website can affect diagram
146+
- potential solution `.not-content`
147+
- CSS from diagram can affect website (d2 diagrams)
148+
- this can break dark theme or other diagrams on the smae page
149+
- potential solution use unique prefix to make sure there is no clash
150+
- Dark theme can be limited (black and white)
151+
- DOM footprint
152+
- easy to integrate, especially if there is no inline CSS in SVG
153+
154+
### data-url
155+
156+
- **pros**
157+
- no CSS conflicts
158+
- dark theme
159+
- class based. Two images with two classes and only one shown at a time depending on the theme
160+
- `prefers-color-scheme`: `picture` + `source media="(prefers-color-scheme: dark)` + `img`
161+
- no DOM footprint
162+
- **cons**
163+
- no interactivity
164+
- no DOM footprint, but overall weight of HTML is high as in previous strategy
165+
- easy to integrate - only need to add a bit of CSS to support dark theme
166+
167+
### file
168+
169+
- **pros**
170+
- no CSS conflicts
171+
- dark theme
172+
- class based. Two images with two classes and only one shown at a time depending on the theme
173+
- `prefers-color-scheme`: `picture` + `source media="(prefers-color-scheme: dark)` + `img`
174+
- no DOM footprint
175+
- lighter weight of HTML compared to previous strategy
176+
- **cons**
177+
- no interactivity
178+
- some tools don't support dark theme out of the box (`graphviz`, `vizdom`, `gnuplot`)
179+
- harder to integrate - need to resolve where to write file, how to do cache busting...
-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
# @beoe/rehype-code-hook-img
22

33
The same as `@beoe/rehype-code-hook`, but specifically designed for hooks that output images (e.g. diagrams).
4-
5-
## TODO
6-
7-
- [ ] Seriously. Strategies need better names

‎packages/rehype-code-hook-img/src/index.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,27 @@ export const rehypeCodeHookImg = <T extends CbInput>(
3838
defaults as any as T & BasePluginOptions,
3939
meta
4040
);
41-
const darkMode =
42-
opts.strategy === "img-class-dark-mode" ||
43-
opts.strategy === "picture-dark-mode" ||
44-
opts.strategy === "f-img-class-dark-mode" ||
45-
opts.strategy === "f-picture-dark-mode";
41+
42+
if (opts.strategy === "img") {
43+
opts.strategy = "data-url";
44+
console.warn("img strategy is deprecated, use data-url instead");
45+
}
46+
if (opts.strategy === "img-class-dark-mode") {
47+
opts.strategy = "data-url";
48+
opts.darkScheme = "class";
49+
console.warn(
50+
"img-class-dark-mode strategy is deprecated, use data-url instead + darkScheme class"
51+
);
52+
}
53+
if (opts.strategy === "picture-dark-mode") {
54+
opts.strategy = "data-url";
55+
opts.darkScheme = "media";
56+
console.warn(
57+
"img-class-dark-mode strategy is deprecated, use data-url instead + darkScheme media"
58+
);
59+
}
60+
61+
const darkMode = opts.darkScheme != undefined;
4662
const result = render(code, { ...opts, darkMode });
4763
return "then" in result
4864
? result.then((x) => svgStrategy(opts, x))

‎packages/rehype-code-hook-img/src/types.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,19 @@ export type BasePluginOptions = {
4646
svgo?: SvgoConfig | boolean;
4747
strategy?: Strategy;
4848
cache?: MapLike;
49+
darkScheme?: Scheme;
4950
/**
50-
* required for file-based strategy
51+
* required for `file` strategy
5152
*/
5253
fsPath?: string;
5354
/**
54-
* required for file-based strategy
55+
* required for `file` strategy
5556
*/
5657
webPath?: string;
5758
};
5859

60+
export type Scheme = "class" | "media";
61+
5962
/**
6063
* maybe rename img to data-uri
6164
*/
@@ -64,24 +67,29 @@ export type Strategy =
6467
* SVG directly in the HTML
6568
*/
6669
| "inline"
70+
/**
71+
* SVG in `src` as [data-url](https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data)
72+
*/
73+
| "data-url"
74+
/**
75+
* SVG stored as standalone file
76+
*/
77+
| "file"
6778
/**
6879
* SVG as data-uri in img
80+
* @deprecated
6981
*/
7082
| "img"
7183
/**
7284
* SVG as data-uri in img and source inside of a picture
85+
* @deprecated
7386
*/
7487
| "picture-dark-mode"
7588
/**
7689
* SVG as data-uri in two imgs with light and dark classes
90+
* @deprecated
7791
*/
78-
| "img-class-dark-mode"
79-
/**
80-
* Experiment
81-
*/
82-
| "f-img"
83-
| "f-picture-dark-mode"
84-
| "f-img-class-dark-mode";
92+
| "img-class-dark-mode";
8593

8694
export type CbResult = Result | Promise<Result>;
8795

0 commit comments

Comments
 (0)
Failed to load comments.