-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: XML-RPC path maybe undefined sometime.
- Loading branch information
Showing
4 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="">`; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Media { | ||
mimeType: string; | ||
fileName: string; | ||
content?: ArrayBuffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters