@md-plugins/md-plugin-image 0.1.0-alpha.1 → 0.1.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2024-present, Jeff Galbraith
3
+ Copyright (c) 2024-present, MD-PLUGINS
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -26,21 +26,21 @@ pnpm add @md-plugins/md-plugin-image
26
26
  ### Basic Setup
27
27
 
28
28
  ```js
29
- import MarkdownIt from 'markdown-it';
30
- import { imagePlugin } from '@md-plugins/md-plugin-image';
29
+ import MarkdownIt from 'markdown-it'
30
+ import { imagePlugin } from '@md-plugins/md-plugin-image'
31
31
 
32
- const md = new MarkdownIt();
32
+ const md = new MarkdownIt()
33
33
  md.use(imagePlugin, {
34
34
  imageClass: 'custom-image-class',
35
- });
35
+ })
36
36
 
37
37
  const markdownContent = `
38
38
  ![Alt text](example.jpg)
39
- `;
39
+ `
40
40
 
41
- const renderedOutput = md.render(markdownContent);
41
+ const renderedOutput = md.render(markdownContent)
42
42
 
43
- console.log('Rendered Output:', renderedOutput);
43
+ console.log('Rendered Output:', renderedOutput)
44
44
  ```
45
45
 
46
46
  ### Example Output
@@ -67,6 +67,10 @@ Run the tests to ensure the plugin behaves as expected:
67
67
  pnpm test
68
68
  ```
69
69
 
70
+ ## Documentation
71
+
72
+ In case this README falls out of date, please refer to the [documentation](https://md-plugins.netlify.app/md-plugins/image/overview) for the latest information.
73
+
70
74
  ## License
71
75
 
72
76
  This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
package/dist/index.mjs CHANGED
@@ -5,20 +5,39 @@ const imagePlugin = (md, { imageClass = "markdown-image" } = {}) => {
5
5
  if (!token) {
6
6
  return self.renderToken(tokens, idx, options);
7
7
  }
8
- const existingClass = token.attrGet("class") || "";
9
- const combinedClass = [existingClass, imageClass].filter(Boolean).join(" ");
10
- token.attrSet("class", combinedClass);
11
- if (token.content) {
8
+ let content = token.content;
9
+ const widthMatch = content.match(/width="(\d+)"/);
10
+ const heightMatch = content.match(/height="(\d+)"/);
11
+ if (widthMatch) {
12
+ token.attrSet("width", widthMatch[1]);
13
+ content = content.replace(widthMatch[0], "").trim();
14
+ }
15
+ if (heightMatch) {
16
+ token.attrSet("height", heightMatch[1]);
17
+ content = content.replace(heightMatch[0], "").trim();
18
+ }
19
+ if (content) {
12
20
  const altIndex = token.attrIndex("alt");
13
21
  if (altIndex >= 0 && token.attrs) {
14
22
  const altValue = token.attrs[altIndex][1];
15
23
  if (!altValue) {
16
- token.attrs[altIndex][1] = token.content;
24
+ token.attrs[altIndex][1] = content;
17
25
  }
18
26
  } else {
19
- token.attrPush(["alt", token.content]);
27
+ token.attrPush(["alt", content]);
20
28
  }
21
29
  }
30
+ const existingClass = token.attrGet("class") || "";
31
+ const combinedClass = [existingClass, imageClass].filter(Boolean).join(" ");
32
+ token.attrSet("class", combinedClass);
33
+ token.content = content;
34
+ if (token.children) {
35
+ token.children.forEach((child) => {
36
+ if (child.type === "text") {
37
+ child.content = content;
38
+ }
39
+ });
40
+ }
22
41
  return originalImageRender ? originalImageRender(tokens, idx, options, env, self) : self.renderToken(tokens, idx, options);
23
42
  };
24
43
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@md-plugins/md-plugin-image",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0-alpha.10",
4
4
  "description": "A markdown-it plugin for handling images.",
5
5
  "keywords": [
6
6
  "markdown-it",
@@ -33,16 +33,21 @@
33
33
  "./dist"
34
34
  ],
35
35
  "dependencies": {
36
- "@types/markdown-it": "^14.1.2",
37
36
  "markdown-it": "^14.1.0",
38
- "@md-plugins/shared": "0.1.0-alpha.1"
37
+ "@md-plugins/shared": "0.1.0-alpha.10"
38
+ },
39
+ "devDependencies": {
40
+ "@types/markdown-it": "^14.1.2"
41
+ },
42
+ "peerDependencies": {
43
+ "markdown-it": "^14.1.0"
39
44
  },
40
45
  "publishConfig": {
41
46
  "access": "public"
42
47
  },
43
48
  "scripts": {
44
49
  "build": "unbuild",
45
- "clean": "rm -rf dist",
50
+ "clean": "rm -rf dist/ node_modules/",
46
51
  "test": "vitest"
47
52
  }
48
53
  }