Skip to content
This repository was archived by the owner on Feb 8, 2025. It is now read-only.

Commit bc67628

Browse files
authored
feat(contented): add image processing (#634)
#### What this PR does / why we need it: Add images support for contented. Image commonly used in Markdown is supported but with some caveats. Contented being a prose-bundler is designed to bundle prose such that it is portable and can be deployed to different sites or domains. This makes it difficult to support images that are not bundled together with the prose. To get around this, all local images are embedded into the Markdown file as base64 encoded strings. Remote images are left as is.
1 parent 886d709 commit bc67628

File tree

6 files changed

+178
-10
lines changed

6 files changed

+178
-10
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Contented
22

3-
[Contented](https://contented.dev) is a Markdown-based bundler for your documentation with pipeline driven
4-
authoring-oriented workflow to encourage developer authoring within its contextual Git repository.
3+
[Contented](https://contented.dev) is a prose bundler for your documentation with pipeline driven
4+
authoring-oriented workflow to encourage developers authoring within its contextual Git repository.
55

66
With a headless design of 1 config file `contented.config.mjs`, developers can start writing
77
their [markdown content](./04-markdown.md) and preview it on their localhost `contented generate --watch`.
@@ -15,7 +15,7 @@ checks it is compilable and presentable.
1515
By encouraging authoring next to the source (in the same git repo), developers can contextually document changes as they
1616
develop. All domain-specific changes will go into the `main` branch with one Git Pull Request.
1717

18-
With `contented build`, you can compile your markdown into sources `index.js` and `*.json`. That output
18+
With `contented build`, you can compile your prose into sources `index.js` and `*.json`. That output
1919
into `./dist` to `npm publish` them into any registry of your choice, for you can
2020
easily `npm i @your-scope/your-npm-package` and use the processed content on any of your downstream sites. Easily
2121
pulling up-to-date content and prose from individual domain-specific repositories and re-presented. Think microservices,
@@ -30,7 +30,7 @@ naturally satisfies.
3030

3131
### Just Another SSG?
3232

33-
**This is not a static site generator.** This is a markdown processor workflow with a built-in static site generator.
33+
**This is not a static site generator.** This is a prose processor workflow with a built-in static site generator.
3434
The outcome we're trying to achieve is
3535
this [@contentedjs/contented-example/dist](https://www.jsdelivr.com/package/npm/@contentedjs/contented-example)
3636

@@ -51,12 +51,12 @@ Your docs can be anywhere as long as contented is configured to pick them up.
5151
repo/
5252
├─ packages/**
5353
├─ docs/
54-
│ ├─ 01:Title 1/*.md
55-
│ ├─ 02:Title 2/*.md
56-
│ ├─ 03:Title 3/
57-
│ │ ├─ 01:Subtitle 1/*.md
58-
│ │ ├─ 02:overview.md
59-
│ │ └─ 03:faq.md
54+
│ ├─ 01-Title 1/*.md
55+
│ ├─ 02-Title 2/*.md
56+
│ ├─ 03-Title 3/
57+
│ │ ├─ 01-Subtitle 1/*.md
58+
│ │ ├─ 02-overview.md
59+
│ │ └─ 03-faq.md
6060
│ └─ package.json
6161
├─ contented.config.mjs
6262
├─ package.json

package-lock.json

+133
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contented-example/docs/04-markdown.md

+31
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,34 @@ const b = 2;
330330
331331
::::
332332
````
333+
334+
## Images
335+
336+
Image commonly used in Markdown is supported but with some caveats.
337+
Contented being a prose-bundler is designed to bundle prose such that it is portable and can be deployed to different
338+
sites or domains.
339+
This makes it difficult to support images that are not bundled together with the prose.
340+
To get around this, all local images are embedded into the Markdown file as base64 encoded strings.
341+
Remote images are left as is.
342+
343+
> You can inspect this HTML page to see how the images are embedded.
344+
345+
:::div{.admonitions.yellow}
346+
347+
Always be careful with user input. For example, it’s possible to hide JavaScript inside images (such as GIFs, WebPs, and
348+
SVGs). User provided images open you up to a cross-site scripting (XSS) attack.
349+
350+
If you’re using Contented to render user-provided Markdown, you should disable images by default and only enable them
351+
when you trust the source. Contented designed to be used for developer authoring where the source is trusted and XSS
352+
being the least of your worries since the developer (having control of source code) can already inject arbitrary
353+
JavaScript into the page without needing to go through this lengthy process.
354+
355+
:::
356+
357+
![local-embedded-image.png](local-embedded-image.png)
358+
![placehold.co](https://placehold.co/1500x300.png?text=Remote%20Loaded%20Image)
359+
360+
```markdown
361+
![local-embedded-image.png](local-embedded-image.png)
362+
![placehold.co](https://placehold.co/1500x300.png?text=Remote%20Loaded%20Image)
363+
```
Loading

packages/contented-pipeline-md/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"rehype-slug": "^5.1.0",
5757
"rehype-stringify": "^9.0.4",
5858
"remark-directive": "^2.0.1",
59+
"remark-embed-images": "^4.0.0",
5960
"remark-frontmatter": "^4.0.1",
6061
"remark-gfm": "^3.0.1",
6162
"remark-parse": "^10.0.2",

packages/contented-pipeline-md/src/UnifiedProcessor.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import rehypeExternalLinks from 'rehype-external-links';
44
import rehypeSlug from 'rehype-slug';
55
import rehypeStringify from 'rehype-stringify';
66
import remarkDirective from 'remark-directive';
7+
import remarkEmbedImages from 'remark-embed-images';
78
import remarkFrontmatter from 'remark-frontmatter';
89
import remarkGfm from 'remark-gfm';
910
import remarkParse from 'remark-parse';
@@ -41,6 +42,7 @@ export function initProcessor(processor: Processor, options?: UnifiedOptions): P
4142
.use(remarkGfm)
4243
.use(remarkFrontmatter)
4344
.use(remarkParse)
45+
.use(remarkEmbedImages)
4446
.use(remarkLink)
4547
.use(remarkDirective)
4648
.use(remarkDirectiveRehypeCodeblockHeader)
@@ -90,6 +92,7 @@ export {
9092
remarkDirectiveRehype,
9193
remarkDirectiveRehypeCodeblockGroup,
9294
remarkDirectiveRehypeCodeblockHeader,
95+
remarkEmbedImages,
9396
remarkFrontmatter,
9497
remarkFrontmatterCollect,
9598
remarkFrontmatterResolve,

0 commit comments

Comments
 (0)