Skip to content

Commit 86e0c0e

Browse files
authored
Merge pull request #3 from 0gust1/feat/add_more_config_options
Feat/add more config options
2 parents 237d73a + da9c70a commit 86e0c0e

File tree

9 files changed

+1872
-158
lines changed

9 files changed

+1872
-158
lines changed

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# mdsvex-enhanced-images
22
[![NPM Downloads](https://img.shields.io/npm/dt/%40lzinga%2Fmdsvex-enhanced-images)](https://www.npmjs.com/package/@lzinga/mdsvex-enhanced-images)
33

4-
Allows you to use relative urls to images from the markdown file while also using the enhanced:img library from @sveltejs/enhanced-img.
4+
Allows you to:
5+
- use relative urls to images from the markdown file while also using the enhanced:img library from @sveltejs/enhanced-img.
6+
- add css classes and extra attributes to images in markdown
7+
- add imagetools directives to images in markdown
58

6-
Thanks to https://github.com/mattjennings/mdsvex-relative-images as I used it as a base for this. It could probably be a lot more dynamic and allow custom configurations, but for now. This is all I needed. If you have a recommended change please do a pull request.
9+
Thanks to https://github.com/mattjennings/mdsvex-relative-images for the inspiration.
710

11+
## Usage
812

9-
# Usage
1013
Install the package
1114
```
12-
npm i @lzinga/mdsvex-enhanced-images
15+
npm install --save-dev mdsvex-enhanced-images
1316
```
1417

1518
Configure the package in your mdsvex config.
@@ -22,7 +25,24 @@ const config = {
2225
preprocess: [
2326
mdsvex({
2427
extensions: ['.md'],
25-
remarkPlugins: [enhancedImage]
28+
remarkPlugins: [[
29+
enhancedImage,
30+
{
31+
// Optional: attributes to add to **all** `img` tags
32+
attributes: {
33+
fetchpriority: "auto", // browser's default
34+
loading: "eager", // browser's default
35+
decoding: "auto", // browser's default
36+
class: "test-decoration test-shadow" // add classes to all images
37+
},
38+
// Optional: imagetools directives to add to **all** `img` tags
39+
// see https://github.com/JonasKruckenberg/imagetools/blob/main/docs/directives.md#format
40+
imagetoolsDirectives:{
41+
tint: "rgba(10,33,127)",
42+
blur: 10,
43+
}
44+
}
45+
]]
2646
})
2747
],
2848
kit: {
@@ -44,7 +64,22 @@ Now you can add images like
4464
![Image no space, lib folder](../lib/images/img.png)
4565
```
4666

67+
You can also individually add css classes and extra attribute to images:
68+
69+
```markdown
70+
### Image with css classes:
71+
![Image no space, lib folder](../lib/images/img.png?class=my-class1;my-class2)
72+
73+
### Image with more attributes (here, loading=lazy):
74+
![Image no space, lib folder](../lib/images/img.png?loading=lazy)
75+
76+
### Image with imagetools directives
77+
78+
![Image no space, lib folder](../lib/images/img.png?rotate=90)
79+
```
80+
4781
and in the page the images get replaced with the component like so.
82+
4883
```html
4984
<enhanced:img src={importedImage} alt="Image With Space Local Folder"></enhanced:img>
5085
```

index.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1-
import { visit } from 'unist-util-visit';
1+
import { visit } from "unist-util-visit";
2+
import { processAttributesAndConfig } from "./utils.js";
23

34
const RE_SCRIPT_START =
45
/<script(?:\s+?[a-zA-z]+(=(?:["']){0,1}[a-zA-Z0-9]+(?:["']){0,1}){0,1})*\s*?>/;
56
const RE_SRC = /src\s*=\s*"(.+?)"/;
67

7-
export default function enhancedImage() {
8+
export default function enhancedImage(options = {}) {
89
return function transformer(tree) {
910
let scripts = "";
10-
visit(tree, 'image', (node) => {
11-
if(node.url.startsWith("."))
12-
{
11+
visit(tree, "image", (node) => {
12+
if (node.url.startsWith(".")) {
13+
// Process the attributes and config
14+
const {
15+
combinedClassesAttrStr,
16+
combinedAttributesStr,
17+
combinedDirectivesUrlParams,
18+
} = processAttributesAndConfig(node.url, options);
19+
20+
// Now, clean possible search params from the node.url
21+
// They've been processed and are now in the result of processAttributesAndConfig call
22+
node.url = node.url.split("?")[0];
23+
1324
// Generate a unique identifier for the import
1425
const importName = `_img${Math.random().toString(36).substr(2, 9)}`;
1526

1627
// Create the import statement
17-
const importStatement = `import ${importName} from '${decodeURIComponent(node.url)}?enhanced';\n`;
18-
scripts += `${importStatement}`
28+
const importStatement = `import ${importName} from '${decodeURIComponent(
29+
node.url
30+
)}?enhanced${combinedDirectivesUrlParams}';\n`;
31+
scripts += `${importStatement}`;
1932

2033
// Create the image component
21-
const imageComponent = `<enhanced:img src={${importName}} alt="${node.alt || ''}"></enhanced:img>`;
22-
34+
const imageComponent = `<enhanced:img src={${importName}} alt="${
35+
node.alt ?? ""
36+
}" ${combinedClassesAttrStr} ${combinedAttributesStr}></enhanced:img>`;
37+
2338
// Replace the node with the import and component
24-
node.type = 'html';
39+
node.type = "html";
2540
node.value = imageComponent;
26-
2741
}
2842
});
2943

@@ -42,9 +56,6 @@ export default function enhancedImage() {
4256
type: "html",
4357
value: `<script>\n${scripts}</script>`,
4458
});
45-
}
46-
47-
48-
49-
}
50-
}
59+
}
60+
};
61+
}

0 commit comments

Comments
 (0)