Skip to content

Commit

Permalink
[main] Updated figure number regex for Apple devices. [md_processor] …
Browse files Browse the repository at this point in the history
…Added MarkdownPostProcessorContext in attempt to render markdown in caaptions, but does not work.
  • Loading branch information
bicarlsen committed Dec 1, 2021
1 parent 7b1f02e commit 9ad5127
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-image-caption",
"name": "Image Caption",
"version": "0.0.7",
"version": "0.0.8",
"minAppVersion": "0.12.19",
"description": "Add captions to images.",
"author": "bicarlsen",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-image-caption",
"version": "0.0.7",
"version": "0.0.8",
"description": "Add captions to images in Obsidian.",
"main": "main.js",
"scripts": {
Expand All @@ -13,7 +13,7 @@
"devDependencies": {
"@types/node": "^16.11.1",
"esbuild": "0.13.8",
"obsidian": "^0.12.17",
"obsidian": "^0.12.19",
"tslib": "2.3.1",
"typescript": "4.4.4"
}
Expand Down
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const DEFAULT_SETTINGS: ImageCaptionSettings = {
delimeter: []
}


export default class ImageCaptionPlugin extends Plugin {
settings: ImageCaptionSettings;

Expand Down Expand Up @@ -85,9 +86,13 @@ export default class ImageCaptionPlugin extends Plugin {

let label = this.settings.label;
if ( label ) {
const number_pattern = /(?<!\\)#/g;
label = label.replace( number_pattern, "' attr(data-image-caption-index) '" ); // inner quotes used to kill string and insert attr. + between strings breaks it.
// replace all unescaped hashtags with image index
const number_pattern = /(^|[^\\])#/g;
label = label.replace( number_pattern, "$1' attr(data-image-caption-index) '" ); // inner quotes used to kill string and insert attr. + between strings breaks it.

// Replace escaped hashtags with hashtags
label = label.replace( '\\#', '#' );

label = `${ImageCaptionPlugin.caption_selector}::before { content: '${label} ' }`; // additional space in content intentional
}

Expand Down
13 changes: 8 additions & 5 deletions src/md_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import ImageCaptionPlugin from './main';


export function captionObserver( plugin: Plugin ) {
export function captionObserver( plugin: Plugin, ctx: MarkdownPostProcessorContext ) {

return new MutationObserver( ( mutations, observer ) => {
for ( const mutation of mutations ) {
Expand All @@ -29,7 +29,8 @@ export function captionObserver( plugin: Plugin ) {

caption_text = parseCaptionText( caption_text, plugin.settings.delimeter );
if ( caption_text !== null ) {
addCaption( mutation.target, caption_text );
const caption = addCaption( mutation.target, caption_text );
ctx.addChild( caption );
}
} // end for..of

Expand All @@ -39,6 +40,7 @@ export function captionObserver( plugin: Plugin ) {
} );
}


function parseCaptionText( text: string, delimeter: string[] ): string | null {
if ( delimeter.length === 0 ) {
return text;
Expand Down Expand Up @@ -72,16 +74,17 @@ function parseCaptionText( text: string, delimeter: string[] ): string | null {
return text.slice( start + start_offset, end );
}


function addCaption(
target: HTMLElement,
caption_text: string
): HTMLElement {
): MarkdownRenderChild {
const caption = document.createElement( ImageCaptionPlugin.caption_tag );
caption.addClass( ImageCaptionPlugin.caption_class );
caption.innerText = caption_text;
target.appendChild( caption );

return caption;
return new MarkdownRenderChild( caption );
}


Expand Down Expand Up @@ -112,7 +115,7 @@ export function processImageCaption(
( container: HTMLElement ) => {
// must listen for class changes because images
// may be loaded after this run
const observer = captionObserver( plugin );
const observer = captionObserver( plugin, ctx );
observer.observe(
container,
{ attributes: true, attributesFilter: [ 'class' ] }
Expand Down
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"0.0.8": "0.12.19",
"0.0.7": "0.12.19",
"0.0.6": "0.12.19",
"0.0.5": "0.12.19",
Expand Down

0 comments on commit 9ad5127

Please sign in to comment.