2025-08-02 12:09:34 +08:00
/ *
THIS IS A GENERATED / BUNDLED FILE BY ESBUILD
if you want to view the source , please visit the github repository of this plugin
* /
var _ _defProp = Object . defineProperty ;
var _ _getOwnPropDesc = Object . getOwnPropertyDescriptor ;
var _ _getOwnPropNames = Object . getOwnPropertyNames ;
var _ _hasOwnProp = Object . prototype . hasOwnProperty ;
var _ _export = ( target , all ) => {
for ( var name in all )
_ _defProp ( target , name , { get : all [ name ] , enumerable : true } ) ;
} ;
var _ _copyProps = ( to , from , except , desc ) => {
if ( from && typeof from === "object" || typeof from === "function" ) {
for ( let key of _ _getOwnPropNames ( from ) )
if ( ! _ _hasOwnProp . call ( to , key ) && key !== except )
_ _defProp ( to , key , { get : ( ) => from [ key ] , enumerable : ! ( desc = _ _getOwnPropDesc ( from , key ) ) || desc . enumerable } ) ;
}
return to ;
} ;
var _ _toCommonJS = ( mod ) => _ _copyProps ( _ _defProp ( { } , "__esModule" , { value : true } ) , mod ) ;
// src/main.ts
var main _exports = { } ;
_ _export ( main _exports , {
default : ( ) => ImageCaptions ,
renderMarkdown : ( ) => renderMarkdown
} ) ;
module . exports = _ _toCommonJS ( main _exports ) ;
var import _obsidian2 = require ( "obsidian" ) ;
// src/settings.ts
var import _obsidian = require ( "obsidian" ) ;
var DEFAULT _SETTINGS = {
captionRegex : ""
} ;
var CaptionSettingTab = class extends import _obsidian . PluginSettingTab {
constructor ( app , plugin ) {
super ( app , plugin ) ;
this . plugin = plugin ;
}
display ( ) {
const { containerEl } = this ;
containerEl . empty ( ) ;
new import _obsidian . Setting ( containerEl ) . setName ( "Advanced settings" ) . setHeading ( ) ;
new import _obsidian . Setting ( containerEl ) . setName ( "Caption regex" ) . setDesc ( "For advanced caption parsing, you can add a regex here. The first capturing group will be used as the image caption. This is useful in situations where you might have another plugin or theme adding text to the caption area which you want to strip out. The placeholder example would be used to exclude everything following a pipe character (if one exists)." ) . addText ( ( text ) => text . setPlaceholder ( "^([^|]+)" ) . setValue ( this . plugin . settings . captionRegex ) . onChange ( async ( value ) => {
this . plugin . settings . captionRegex = value ;
await this . plugin . saveSettings ( ) ;
} ) ) ;
}
} ;
// src/main.ts
var filenamePlaceholder = "%" ;
var filenameExtensionPlaceholder = "%.%" ;
var ImageCaptions = class extends import _obsidian2 . Plugin {
async onload ( ) {
this . registerMarkdownPostProcessor ( this . externalImageProcessor ( ) ) ;
await this . loadSettings ( ) ;
this . addSettingTab ( new CaptionSettingTab ( this . app , this ) ) ;
this . observer = new MutationObserver ( ( mutations ) => {
mutations . forEach ( ( rec ) => {
if ( rec . type === "childList" ) {
rec . target . querySelectorAll ( ".image-embed, .video-embed" ) . forEach ( async ( imageEmbedContainer ) => {
var _a , _b ;
const img = imageEmbedContainer . querySelector ( "img, video" ) ;
const width = imageEmbedContainer . getAttribute ( "width" ) || "" ;
const captionText = this . getCaptionText ( imageEmbedContainer ) ;
if ( ! img )
return ;
const figure = imageEmbedContainer . querySelector ( "figure" ) ;
const figCaption = imageEmbedContainer . querySelector ( "figcaption" ) ;
if ( figure || ( ( _a = img . parentElement ) == null ? void 0 : _a . nodeName ) === "FIGURE" ) {
if ( figCaption && captionText ) {
const children = ( _b = await renderMarkdown ( captionText , "" , this ) ) != null ? _b : [ captionText ] ;
figCaption . replaceChildren ( ... children ) ;
} else if ( ! captionText ) {
imageEmbedContainer . appendChild ( img ) ;
figure == null ? void 0 : figure . remove ( ) ;
}
} else {
if ( captionText && captionText !== imageEmbedContainer . getAttribute ( "src" ) ) {
await this . insertFigureWithCaption ( img , imageEmbedContainer , captionText , "" ) ;
}
}
if ( width ) {
img . setAttribute ( "width" , width ) ;
} else {
img . removeAttribute ( "width" ) ;
}
} ) ;
}
} ) ;
} ) ;
this . observer . observe ( document . body , {
subtree : true ,
childList : true
} ) ;
}
getCaptionText ( img ) {
let captionText = img . getAttribute ( "alt" ) || "" ;
const src = img . getAttribute ( "src" ) || "" ;
const edge = captionText . replace ( / > / , "#" ) ;
if ( captionText === src || edge === src ) {
return "" ;
}
if ( this . settings . captionRegex ) {
try {
const match = captionText . match ( new RegExp ( this . settings . captionRegex ) ) ;
2026-05-06 17:32:44 +08:00
if ( match && match [ 1 ] ) {
2025-08-02 12:09:34 +08:00
captionText = match [ 1 ] ;
2026-05-06 17:32:44 +08:00
} else {
captionText = "" ;
}
2025-08-02 12:09:34 +08:00
} catch ( e ) {
}
}
if ( captionText === filenamePlaceholder ) {
const match = src . match ( /[^\\/]+(?=\.\w+$)|[^\\/]+$/ ) ;
if ( match == null ? void 0 : match [ 0 ] ) {
captionText = match [ 0 ] ;
}
} else if ( captionText === filenameExtensionPlaceholder ) {
const match = src . match ( /[^\\/]+$/ ) ;
if ( match == null ? void 0 : match [ 0 ] ) {
captionText = match [ 0 ] ;
}
} else if ( captionText === "\\" + filenamePlaceholder ) {
captionText = filenamePlaceholder ;
}
captionText = captionText . replace ( /<<(.*?)>>/g , ( _ , linktext ) => {
return "[[" + linktext + "]]" ;
} ) ;
return captionText ;
}
externalImageProcessor ( ) {
return ( el , ctx ) => {
el . findAll ( "img:not(.emoji), video" ) . forEach ( async ( img ) => {
const captionText = this . getCaptionText ( img ) ;
const parent = img . parentElement ;
if ( parent && ( parent == null ? void 0 : parent . nodeName ) !== "FIGURE" && captionText && captionText !== img . getAttribute ( "src" ) ) {
await this . insertFigureWithCaption ( img , parent , captionText , ctx . sourcePath ) ;
}
} ) ;
} ;
}
async insertFigureWithCaption ( imageEl , outerEl , captionText , sourcePath ) {
var _a ;
const figure = outerEl . createEl ( "figure" ) ;
figure . addClass ( "image-captions-figure" ) ;
figure . appendChild ( imageEl ) ;
const children = ( _a = await renderMarkdown ( captionText , sourcePath , this ) ) != null ? _a : [ captionText ] ;
figure . createEl ( "figcaption" , {
cls : "image-captions-caption"
} ) . replaceChildren ( ... children ) ;
}
async loadSettings ( ) {
this . settings = Object . assign ( { } , DEFAULT _SETTINGS , await this . loadData ( ) ) ;
}
async saveSettings ( ) {
await this . saveData ( this . settings ) ;
}
onunload ( ) {
this . observer . disconnect ( ) ;
}
} ;
async function renderMarkdown ( markdown , sourcePath , component ) {
const el = createDiv ( ) ;
await import _obsidian2 . MarkdownRenderer . renderMarkdown ( markdown , el , sourcePath , component ) ;
for ( const child of el . children ) {
if ( child . tagName . toLowerCase ( ) === "p" ) {
return child . childNodes ;
}
}
}
/* nosourcemap */