@peaceroad/markdown-imgattr-to-pcaption 0.2.1 → 0.3.0
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/README.md +86 -144
- package/index.js +174 -211
- package/package.json +2 -2
- package/script/caption-common.js +177 -0
- package/script/set-img-figure-caption.js +339 -65
package/README.md
CHANGED
|
@@ -1,158 +1,76 @@
|
|
|
1
1
|
# markdown-imgattr-to-pcaption
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Convert image alt/title text into caption paragraphs that work with
|
|
4
|
+
`p7d-markdown-it-p-captions`.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
import setMarkdownImgAttrToPCaption from '@peaceroad/markdown-imgattr-to-pcaption'
|
|
7
|
-
|
|
8
|
-
setMarkdownImgAttrToPCaption(markdownCont)
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
[Input]
|
|
13
|
-
段落。段落。段落。
|
|
14
|
-
|
|
15
|
-

|
|
16
|
-
|
|
17
|
-
段落。段落。段落。
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
[Output]
|
|
21
|
-
段落。段落。段落。
|
|
22
|
-
|
|
23
|
-
図 キャプション
|
|
24
|
-
|
|
25
|
-

|
|
26
|
-
|
|
27
|
-
段落。段落。段落。
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
[Input]
|
|
31
|
-
段落。段落。段落。
|
|
32
|
-
|
|
33
|
-

|
|
6
|
+
## Install
|
|
34
7
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
[Output]
|
|
38
|
-
段落。段落。段落。
|
|
39
|
-
|
|
40
|
-
図 キャプション
|
|
41
|
-
|
|
42
|
-

|
|
43
|
-
|
|
44
|
-
段落。段落。段落。
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
[Input]
|
|
49
|
-
段落。段落。段落。
|
|
50
|
-
|
|
51
|
-

|
|
52
|
-
|
|
53
|
-
段落。段落。段落。
|
|
54
|
-
|
|
55
|
-
[Output]
|
|
56
|
-
段落。段落。段落。
|
|
57
|
-
|
|
58
|
-
図1 キャプション
|
|
59
|
-
|
|
60
|
-

|
|
61
|
-
|
|
62
|
-
段落。段落。段落。
|
|
8
|
+
```bash
|
|
9
|
+
npm i @peaceroad/markdown-imgattr-to-pcaption
|
|
63
10
|
```
|
|
64
11
|
|
|
65
|
-
##
|
|
12
|
+
## Markdown transformer (`index.js`)
|
|
66
13
|
|
|
67
|
-
###
|
|
14
|
+
### Usage
|
|
68
15
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
```
|
|
72
|
-
[Input]
|
|
73
|
-
段落。段落。段落。
|
|
74
|
-
|
|
75
|
-

|
|
76
|
-
|
|
77
|
-
段落。段落。段落。
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
[Output]
|
|
81
|
-
段落。段落。段落。
|
|
82
|
-
|
|
83
|
-
図 キャプション
|
|
84
|
-
|
|
85
|
-

|
|
86
|
-
|
|
87
|
-
段落。段落。段落。
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### labelLang
|
|
91
|
-
|
|
92
|
-
Default: 'en'.
|
|
93
|
-
|
|
94
|
-
```
|
|
95
|
-
[Input]
|
|
96
|
-
段落。段落。段落。
|
|
97
|
-
|
|
98
|
-

|
|
99
|
-
|
|
100
|
-
段落。段落。段落。
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
[Output]
|
|
104
|
-
段落。段落。段落。
|
|
105
|
-
|
|
106
|
-
図 キャプション
|
|
107
|
-
|
|
108
|
-

|
|
16
|
+
```js
|
|
17
|
+
import setMarkdownImgAttrToPCaption from '@peaceroad/markdown-imgattr-to-pcaption'
|
|
109
18
|
|
|
110
|
-
|
|
19
|
+
const out = setMarkdownImgAttrToPCaption(markdown)
|
|
111
20
|
```
|
|
112
21
|
|
|
113
|
-
###
|
|
22
|
+
### Example
|
|
114
23
|
|
|
115
|
-
|
|
116
|
-
When `autoLangDetection` is true, it can override an explicitly set `labelLang` (it is treated as the fallback when detection cannot decide).
|
|
24
|
+
Input:
|
|
117
25
|
|
|
118
|
-
|
|
26
|
+
```md
|
|
27
|
+
Paragraph.
|
|
119
28
|
|
|
120
|
-
|
|
29
|
+

|
|
121
30
|
|
|
31
|
+
Paragraph.
|
|
122
32
|
```
|
|
123
|
-
[Input]
|
|
124
|
-
段落。
|
|
125
|
-
|
|
126
|
-

|
|
127
33
|
|
|
128
|
-
|
|
34
|
+
Output:
|
|
129
35
|
|
|
130
|
-
|
|
131
|
-
|
|
36
|
+
```md
|
|
37
|
+
Paragraph.
|
|
132
38
|
|
|
133
|
-
Figure.
|
|
39
|
+
Figure. A caption
|
|
134
40
|
|
|
135
41
|

|
|
136
42
|
|
|
137
|
-
|
|
43
|
+
Paragraph.
|
|
138
44
|
```
|
|
139
|
-
Only `ja` and `en` are auto-detected. For other languages, set `labelLang` explicitly (and use `labelSet` as needed) or leave auto-detection off.
|
|
140
|
-
Detection runs only once on the first eligible image line; subsequent images do not affect the language choice.
|
|
141
45
|
|
|
142
|
-
###
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
46
|
+
### Options
|
|
47
|
+
|
|
48
|
+
- `imgTitleCaption` (`boolean`, default: `false`)
|
|
49
|
+
- Use image `title` as caption source.
|
|
50
|
+
- When enabled, `imgAltCaption` behavior is disabled.
|
|
51
|
+
- `labelLang` (`string`, default: `en`)
|
|
52
|
+
- Default label language (`en`/`ja` out of the box).
|
|
53
|
+
- `autoLangDetection` (`boolean`, default: `true`)
|
|
54
|
+
- Detect language from the first eligible image caption.
|
|
55
|
+
- `ja` if Japanese characters exist.
|
|
56
|
+
- `en` if ASCII letters exist.
|
|
57
|
+
- Otherwise keeps the current `labelLang`.
|
|
58
|
+
- `labelSet` (`object|null`, default: `null`)
|
|
59
|
+
- Override generated `label` / `joint` / `space`.
|
|
60
|
+
- Supports single config or per-language map.
|
|
61
|
+
|
|
62
|
+
Single config example:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
setMarkdownImgAttrToPCaption(markdown, {
|
|
150
66
|
labelSet: { label: '図', joint: ':', space: ' ' }
|
|
151
67
|
})
|
|
152
68
|
```
|
|
153
69
|
|
|
154
|
-
|
|
155
|
-
|
|
70
|
+
Per-language config example:
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
setMarkdownImgAttrToPCaption(markdown, {
|
|
156
74
|
labelSet: {
|
|
157
75
|
en: { label: 'Figure', joint: '.', space: ' ' },
|
|
158
76
|
ja: { label: '図', joint: ' ', space: '' },
|
|
@@ -161,18 +79,17 @@ setMarkdownImgAttrToPCaption(markdownCont, {
|
|
|
161
79
|
})
|
|
162
80
|
```
|
|
163
81
|
|
|
164
|
-
|
|
82
|
+
### Conversion rules
|
|
165
83
|
|
|
166
|
-
-
|
|
167
|
-
- Skips fenced code blocks (```
|
|
168
|
-
-
|
|
169
|
-
- `
|
|
84
|
+
- Converts only single-line image syntax surrounded by blank lines.
|
|
85
|
+
- Skips fenced code blocks (``` and ~~~).
|
|
86
|
+
- Skips display math fence blocks using `$` markers (`$$ ... $$`, `$$$$ ... $$$$`, etc.).
|
|
87
|
+
- Uses `p7d-markdown-it-p-captions` label patterns for label detection.
|
|
88
|
+
- `autoLangDetection` runs once on the first eligible image.
|
|
170
89
|
|
|
171
|
-
## Browser DOM helper (
|
|
90
|
+
## Browser DOM helper (`script/set-img-figure-caption.js`)
|
|
172
91
|
|
|
173
|
-
|
|
174
|
-
It is useful for live preview environments that do not re-run markdown-it on each edit.
|
|
175
|
-
This helper does not insert label prefixes; it uses the raw alt/title text as the caption.
|
|
92
|
+
### Usage
|
|
176
93
|
|
|
177
94
|
```html
|
|
178
95
|
<script type="module">
|
|
@@ -181,16 +98,41 @@ import setImgFigureCaption from '@peaceroad/markdown-imgattr-to-pcaption/script/
|
|
|
181
98
|
await setImgFigureCaption({
|
|
182
99
|
imgAltCaption: true,
|
|
183
100
|
imgTitleCaption: false,
|
|
184
|
-
observe: true
|
|
101
|
+
observe: true,
|
|
185
102
|
})
|
|
186
103
|
</script>
|
|
187
104
|
```
|
|
188
105
|
|
|
189
|
-
###
|
|
106
|
+
### Behavior
|
|
107
|
+
|
|
108
|
+
- Mirrors label/caption decisions from markdown transformer.
|
|
109
|
+
- In `observe` mode, keeps one observer per document.
|
|
110
|
+
- Uses internal source cache for stable reprocessing without extra DOM attributes.
|
|
111
|
+
- Re-detects language when first-image context changes.
|
|
112
|
+
|
|
113
|
+
### Options
|
|
114
|
+
|
|
115
|
+
- `imgAltCaption` (`boolean|string`, default: `true`)
|
|
116
|
+
- `imgTitleCaption` (`boolean|string`, default: `false`)
|
|
117
|
+
- `labelLang` (`string`, default: `en`)
|
|
118
|
+
- `autoLangDetection` (`boolean`, default: `true`)
|
|
119
|
+
- `labelSet` (`object|null`, default: `null`)
|
|
120
|
+
- `figureClass` (`string`, default: `f-img`)
|
|
121
|
+
- `readMeta` (`boolean`, default: `false`)
|
|
122
|
+
- `observe` (`boolean`, default: `false`)
|
|
123
|
+
|
|
124
|
+
## Limitations
|
|
125
|
+
|
|
126
|
+
- Only single-line inline image syntax is supported.
|
|
127
|
+
- Supported forms include `` and `)`.
|
|
128
|
+
- Multi-line image link syntax is out of scope.
|
|
129
|
+
- Complex `alt` text patterns that rely on raw `](` are out of scope.
|
|
130
|
+
- Indented code blocks (4 spaces or tab) are out of scope.
|
|
131
|
+
- Use fenced code blocks if you need guaranteed skip behavior.
|
|
132
|
+
- Some non-inline image styles (for example reference-style edge cases) are out of scope.
|
|
133
|
+
|
|
134
|
+
## Related plugin
|
|
135
|
+
|
|
136
|
+
If you use markdown-it figure/caption flows, see:
|
|
190
137
|
|
|
191
|
-
- `
|
|
192
|
-
- `imgTitleCaption` (boolean|string): use `title` text as caption (strings are treated as true)
|
|
193
|
-
- `preferAlt` (boolean): when both are enabled, prefer alt (default true)
|
|
194
|
-
- `figureClass` (string): class name for created figures (default `f-img`)
|
|
195
|
-
- `readMeta` (boolean): read `<meta name="markdown-frontmatter">` and apply `imgAltCaption` / `imgTitleCaption`
|
|
196
|
-
- `observe` (boolean): re-run on DOM changes (MutationObserver)
|
|
138
|
+
- `@peaceroad/markdown-it-figure-with-p-caption`
|