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

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
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.5",
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.5"
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
  }