@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 CHANGED
@@ -1,158 +1,76 @@
1
1
  # markdown-imgattr-to-pcaption
2
2
 
3
- Change img alt attribute to figure caption paragraph for p7d-markdown-it-p-captions.
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
- ![キャプション](image.jpg)
16
-
17
- 段落。段落。段落。
18
-
19
-
20
- [Output]
21
- 段落。段落。段落。
22
-
23
- 図 キャプション
24
-
25
- ![](image.jpg)
26
-
27
- 段落。段落。段落。
28
-
29
-
30
- [Input]
31
- 段落。段落。段落。
32
-
33
- ![図 キャプション](image.jpg)
6
+ ## Install
34
7
 
35
- 段落。段落。段落。
36
-
37
- [Output]
38
- 段落。段落。段落。
39
-
40
- 図 キャプション
41
-
42
- ![](image.jpg)
43
-
44
- 段落。段落。段落。
45
-
46
-
47
-
48
- [Input]
49
- 段落。段落。段落。
50
-
51
- ![図1 キャプション](image.jpg)
52
-
53
- 段落。段落。段落。
54
-
55
- [Output]
56
- 段落。段落。段落。
57
-
58
- 図1 キャプション
59
-
60
- ![](image.jpg)
61
-
62
- 段落。段落。段落。
8
+ ```bash
9
+ npm i @peaceroad/markdown-imgattr-to-pcaption
63
10
  ```
64
11
 
65
- ## Option
12
+ ## Markdown transformer (`index.js`)
66
13
 
67
- ### imgTitleCaption
14
+ ### Usage
68
15
 
69
- Default: false.
70
-
71
- ```
72
- [Input]
73
- 段落。段落。段落。
74
-
75
- ![ALT](image.jpg "キャプション")
76
-
77
- 段落。段落。段落。
78
-
79
-
80
- [Output]
81
- 段落。段落。段落。
82
-
83
- 図 キャプション
84
-
85
- ![ALT](image.jpg)
86
-
87
- 段落。段落。段落。
88
- ```
89
-
90
- ### labelLang
91
-
92
- Default: 'en'.
93
-
94
- ```
95
- [Input]
96
- 段落。段落。段落。
97
-
98
- ![キャプション](image.jpg)
99
-
100
- 段落。段落。段落。
101
-
102
-
103
- [Output]
104
- 段落。段落。段落。
105
-
106
- 図 キャプション
107
-
108
- ![](image.jpg)
16
+ ```js
17
+ import setMarkdownImgAttrToPCaption from '@peaceroad/markdown-imgattr-to-pcaption'
109
18
 
110
- 段落。段落。段落。
19
+ const out = setMarkdownImgAttrToPCaption(markdown)
111
20
  ```
112
21
 
113
- ### autoLangDetection
22
+ ### Example
114
23
 
115
- Default: true. To force a specific `labelLang`, set `autoLangDetection: false`.
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
- Detect `labelLang` from the first image caption line. If the caption text contains Japanese characters, it sets `labelLang: 'ja'`. Otherwise, if the caption contains ASCII letters, it sets `labelLang: 'en'` (symbols/emoji are ignored). If the caption contains non-ASCII letters such as accents, the existing `labelLang` is left unchanged.
26
+ ```md
27
+ Paragraph.
119
28
 
120
- Example (non-ASCII letters keep the current `labelLang`):
29
+ ![A caption](image.jpg)
121
30
 
31
+ Paragraph.
122
32
  ```
123
- [Input]
124
- 段落。
125
-
126
- ![Café](image.jpg)
127
33
 
128
- 段落。
34
+ Output:
129
35
 
130
- [Output]
131
- 段落。
36
+ ```md
37
+ Paragraph.
132
38
 
133
- Figure. Café
39
+ Figure. A caption
134
40
 
135
41
  ![](image.jpg)
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
- ### labelSet
143
-
144
- Override the auto-inserted label, joint, and space for captions without labels (useful for other languages).
145
- `labelSet` accepts either a single config for the current `labelLang` or a per-language map.
146
- If a matching language entry is not found, the default (English) label config is used.
147
-
148
- ```
149
- setMarkdownImgAttrToPCaption(markdownCont, {
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
- setMarkdownImgAttrToPCaption(markdownCont, {
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
- ## Notes
82
+ ### Conversion rules
165
83
 
166
- - Only converts images that are on a single line and surrounded by blank lines. Inline images or list items are not changed.
167
- - Skips fenced code blocks (``` or ~~~).
168
- - Label detection uses `p7d-markdown-it-p-captions` label patterns (en/ja by default). `labelSet` only affects auto-inserted labels when no label is detected.
169
- - `autoLangDetection` inspects the first eligible image line and uses the caption text (title when `imgTitleCaption: true`, otherwise alt). If the caption text is empty, it falls back to alt.
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 (live preview)
90
+ ## Browser DOM helper (`script/set-img-figure-caption.js`)
172
91
 
173
- This package also provides a DOM helper to turn image alt/title into `<figure><figcaption>` on the fly.
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
- ### DOM helper options
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 `![alt](url "title")` and `![alt](url (title))`.
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
- - `imgAltCaption` (boolean|string): use `alt` text as caption (strings are treated as true)
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`