@peaceroad/markdown-imgattr-to-pcaption
v0.6.0
Published
Change img alt attribute to figure caption paragraph for p7d-markdown-it-p-captions.
Readme
markdown-imgattr-to-pcaption
Convert image alt / title text into caption paragraphs that work with
p7d-markdown-it-p-captions.
Install
npm i @peaceroad/markdown-imgattr-to-pcaptionMarkdown transformer (index.js)
Usage
import setMarkdownImgAttrToPCaption from '@peaceroad/markdown-imgattr-to-pcaption'
const out = setMarkdownImgAttrToPCaption(markdown)markdownmust be a string.- Non-string input throws
TypeError.
- Non-string input throws
Example
Input:
Paragraph.

Paragraph.Output:
Paragraph.
Figure. A caption

Paragraph.Options
imgAltCaption(boolean, default:true)- Use image
altas caption source.
- Use image
imgTitleCaption(boolean, default:false)- Use image
titleas caption source. - When enabled,
imgAltCaptionis disabled.
- Use image
labelLang(string, default:en)- Default label language (
en/jaout of the box).
- Default label language (
autoLangDetection(boolean, default:true)- Detect language from the first eligible image caption.
jaif Japanese characters exist.enif ASCII letters exist andlabelLangis still the defaulten/jafamily.- Preserves explicit non-
en/jalabelLangon ASCII-only text. - Otherwise falls back to the current upstream label-default priority.
labelSet(object|null, default:null)- Override generated
label/joint/space. - Supports single config or per-language map.
- Override generated
imgAltCaption/imgTitleCaption/autoLangDetectionaccept boolean values only.- Non-boolean values are ignored.
Single config example:
setMarkdownImgAttrToPCaption(markdown, {
labelSet: { label: '図', joint: ':', space: ' ' }
})Per-language config example:
setMarkdownImgAttrToPCaption(markdown, {
labelSet: {
en: { label: 'Figure', joint: '.', space: ' ' },
ja: { label: '図', joint: ' ', space: '' },
fr: { label: 'Fig', joint: '.', space: ' ' },
}
})Conversion rules
- Converts only single-line image syntax surrounded by blank lines.
- Skips fenced code blocks (backtick fences and tilde fences).
- Skips display math fence blocks using
$markers ($$ ... $$,$$$$ ... $$$$, etc.). - Uses
p7d-markdown-it-p-captionslabel patterns for label detection. autoLangDetectionruns once on the first eligible image.
Browser DOM helper (script/set-img-figure-caption.js)
Usage
<script type="module">
import setImgFigureCaption from '@peaceroad/markdown-imgattr-to-pcaption/script/set-img-figure-caption.js'
await setImgFigureCaption({
imgAltCaption: true,
imgTitleCaption: false,
observe: true,
})
</script>Behavior
- Mirrors label/caption decisions from markdown transformer.
- Processes all
imgelements by default (scope: 'all'). - In
observemode, keeps one observer per document. - Uses internal source cache for stable reprocessing without extra DOM attributes.
- Re-detects language when first-image context changes.
observesupports tuning by attribute/meta/child-list granularity and optional quiet-period debounce.
Options
Caption and label:
imgAltCaption(boolean, default:true)imgTitleCaption(boolean, default:false)labelLang(string, default:en)autoLangDetection(boolean, default:true)labelSet(object|null, default:null)figureClass(string, default:f-img)
Meta + observe:
readMeta(boolean, default:false)observe(boolean, default:false)observeAttributes(string[], default:['alt', 'title'])- Select image attributes to watch in observe mode.
- Supported values:
alt,title.
observeMetaContent(boolean, default:true)- When
readMeta: true, watchmeta[name="markdown-frontmatter"]contentchanges.
- When
observeChildList(boolean, default:true)- Watch DOM tree changes (
childList) in observe mode.
- Watch DOM tree changes (
observeDebounceMs(number, default:0)- Quiet-period debounce for observe reprocessing (
0keeps immediate scheduling behavior).
- Quiet-period debounce for observe reprocessing (
Scope:
scope('all'|'standalone'|'figure-only', default:'all')all: process everyimg.standalone: process only standalone images (no significant siblings) and images already insidefigure.figure-only: process only images already insidefigure.
Boolean-only option rule:
imgAltCaption,imgTitleCaption,autoLangDetection,readMeta,observe,observeMetaContent, andobserveChildListaccept boolean values only.- Non-boolean values (including frontmatter values) are ignored.
Limitations
- Only single-line inline image syntax is supported.
- Supported forms include
and).
- Supported forms include
- Multi-line image link syntax is out of scope.
- Complex
alttext patterns that rely on raw](are out of scope. - Indented code blocks (4 spaces or tab) are out of scope.
- Use fenced code blocks if you need guaranteed skip behavior.
- Some non-inline image styles (for example reference-style edge cases) are out of scope.
Related plugin
If you use markdown-it figure/caption flows, see:
@peaceroad/markdown-it-figure-with-p-caption
