Skip to content

Commit

Permalink
fix: XML-RPC path maybe undefined sometime.
Browse files Browse the repository at this point in the history
  • Loading branch information
devbean committed Sep 26, 2023
1 parent fa503bf commit 6699eef
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
90 changes: 90 additions & 0 deletions src/markdown-it-image-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import MarkdownIt from 'markdown-it';
import Token from 'markdown-it/lib/token';
import { trim } from 'lodash-es';


export interface MarkdownItImageActionParams {
src: string;
width?: string;
height?: string;
}

interface MarkdownItImagePluginOptions {
doWithImage: (img: MarkdownItImageActionParams) => void;
}

const pluginOptions: MarkdownItImagePluginOptions = {
doWithImage: () => {},
}

export const MarkdownItImagePluginInstance = {
plugin: pluginImpl,
doWithImage: (action: (img: MarkdownItImageActionParams) => void) => {
pluginOptions.doWithImage = action;
},
}

function pluginImpl(md: MarkdownIt): void {
md.inline.ruler.after('image', 'ob_img', (state, silent) => {
const regex = /^!\[\[([^|\]\n]+)(\|([^\]\n]+))?\]\]/;
const match = state.src.slice(state.pos).match(regex);
if (match) {
if (silent) {
return true;
}
const token = state.push('ob_img', 'img', 0);
const matched = match[0];
const src = match[1];
const size = match[3];
let width: string | undefined;
let height: string | undefined;
if (size) {
const sepIndex = size.indexOf('x'); // width x height
if (sepIndex > 0) {
width = trim(size.substring(0, sepIndex));
height = trim(size.substring(sepIndex + 1));
token.attrs = [
[ 'src', src ],
[ 'width', width ],
[ 'height', height ],
];
} else {
width = trim(size);
token.attrs = [
[ 'src', src ],
[ 'width', width ],
];
}
} else {
token.attrs = [
[ 'src', src ],
];
}
if (pluginOptions.doWithImage) {
pluginOptions.doWithImage({
src: token.attrs?.[0]?.[1],
width: token.attrs?.[1]?.[1],
height: token.attrs?.[2]?.[1],
});
}
state.pos += matched.length;
return true;
} else {
return false;
}
});
md.renderer.rules.ob_img = (tokens: Token[], idx: number) => {
const token = tokens[idx];
const src = token.attrs?.[0]?.[1];
const width = token.attrs?.[1]?.[1];
const height = token.attrs?.[2]?.[1];
if (width) {
if (height) {
return `<img src="${src}" width="${width}" height="${height}" alt="">`;
}
return `<img src="${src}" width="${width}" alt="">`;
} else {
return `<img src="${src}" alt="">`;
}
};
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Media {
mimeType: string;
fileName: string;
content?: ArrayBuffer;
}
2 changes: 1 addition & 1 deletion src/wp-profile-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class WpProfileModal extends Modal {
.setDesc(t('settings_xmlRpcPathDesc'))
.addText(text => text
.setPlaceholder('/xmlrpc.php')
.setValue(this.profileData.xmlRpcPath)
.setValue(this.profileData.xmlRpcPath ?? '')
.onChange((value) => {
this.profileData.xmlRpcPath = value;
}));
Expand Down
2 changes: 1 addition & 1 deletion src/wp-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface WpProfile {
/**
* XML-RPC path.
*/
xmlRpcPath: string;
xmlRpcPath?: string;

/**
* WordPress username.
Expand Down

0 comments on commit 6699eef

Please sign in to comment.