mirror of
https://github.com/litruv/lit.ruv.wtf.git
synced 2026-07-24 02:36:02 +10:00
chore: update code structure for improved readability and maintainability
This commit is contained in:
BIN
tools/blogeditor/blog-editor-0.1.12.vsix
Normal file
BIN
tools/blogeditor/blog-editor-0.1.12.vsix
Normal file
Binary file not shown.
@@ -424,6 +424,18 @@
|
||||
/** uploadId -> resolve(relativePath) */
|
||||
const _uploadResolvers = new Map();
|
||||
|
||||
/**
|
||||
* Splits markdown image target into src and optional title segment.
|
||||
* @param {string} target
|
||||
* @returns {{ src: string, suffix: string }}
|
||||
*/
|
||||
function splitImageTarget(target) {
|
||||
const trimmed = target.trim();
|
||||
const m = trimmed.match(/^(\S+)(\s+["'][\s\S]*["'])?$/);
|
||||
if (!m) return { src: trimmed, suffix: '' };
|
||||
return { src: m[1], suffix: m[2] ?? '' };
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads a blob URL to the extension to save to media/.
|
||||
* Returns a Promise that resolves once mediaSaved is received.
|
||||
@@ -552,9 +564,10 @@
|
||||
* @returns {string}
|
||||
*/
|
||||
function cleanImagePaths(markdown) {
|
||||
return markdown.replace(/(\!\[[^\]]*\]\()([^)]+)(\))/g, (_m, open, src, close) => {
|
||||
const rel = blobToRelative.get(src) ?? webviewToRelative.get(src);
|
||||
return rel ? `${open}${rel}${close}` : `${open}${src}${close}`;
|
||||
return markdown.replace(/(\!\[[^\]]*\]\()([^)]+)(\))/g, (_m, open, target, close) => {
|
||||
const { src, suffix } = splitImageTarget(target);
|
||||
const rel = blobToRelative.get(src) ?? webviewToRelative.get(src);
|
||||
return rel ? `${open}${rel}${suffix}${close}` : `${open}${src}${suffix}${close}`;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -343,6 +343,21 @@ function buildHtml(webview) {
|
||||
.replace('{{THEME_CSS}}', themeCss.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits markdown image target into URL/path and optional trailing title segment.
|
||||
* Examples:
|
||||
* data/blog/media/a.png "Caption" -> { src: data/blog/media/a.png, suffix: "Caption" }
|
||||
* https://x/y.png -> { src: https://x/y.png, suffix: '' }
|
||||
* @param {string} target
|
||||
* @returns {{ src: string, suffix: string }}
|
||||
*/
|
||||
function splitImageTarget(target) {
|
||||
const trimmed = target.trim();
|
||||
const m = trimmed.match(/^(\S+)(\s+["'][\s\S]*["'])?$/);
|
||||
if (!m) return { src: trimmed, suffix: '' };
|
||||
return { src: m[1], suffix: m[2] ?? '' };
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces relative image paths in markdown with webview-safe URIs.
|
||||
* @param {string} content
|
||||
@@ -350,9 +365,10 @@ function buildHtml(webview) {
|
||||
* @returns {string}
|
||||
*/
|
||||
function injectWebviewImageUris(content, websiteBaseUri) {
|
||||
return content.replace(/(\!\[[^\]]*\]\()([^)]+)(\))/g, (match, open, src, close) => {
|
||||
return content.replace(/(\!\[[^\]]*\]\()([^)]+)(\))/g, (match, open, target, close) => {
|
||||
const { src, suffix } = splitImageTarget(target);
|
||||
if (/^https?:|^data:|^blob:/.test(src)) return match;
|
||||
return `${open}${websiteBaseUri}/${src.replace(/^\//, '')}${close}`;
|
||||
return `${open}${websiteBaseUri}/${src.replace(/^\//, '')}${suffix}${close}`;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -366,7 +382,10 @@ function revertWebviewImageUris(content, websiteBaseUri) {
|
||||
const escaped = websiteBaseUri.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
return content.replace(
|
||||
new RegExp(`(\\!\\[[^\\]]*\\]\\()${escaped}/([^)]+)(\\))`, 'g'),
|
||||
(_m, open, rel, close) => `${open}${rel}${close}`
|
||||
(_m, open, target, close) => {
|
||||
const { src, suffix } = splitImageTarget(target);
|
||||
return `${open}${src}${suffix}${close}`;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -399,7 +418,7 @@ function handleMessage(msg, blogDir, websiteDir) {
|
||||
const imgPattern = /!\[[^\]]*\]\(([^)]+)\)/g;
|
||||
let imgMatch;
|
||||
while ((imgMatch = imgPattern.exec(raw)) !== null) {
|
||||
const src = imgMatch[1];
|
||||
const { src } = splitImageTarget(imgMatch[1]);
|
||||
if (/^https?:|^data:|^blob:/.test(src)) continue;
|
||||
const absPath = path.join(websiteDir, src.replace(/^\//, ''));
|
||||
if (fs.existsSync(absPath)) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "blog-editor",
|
||||
"displayName": "Blog Editor",
|
||||
"description": "WYSIWYG Milkdown blog post editor for this workspace",
|
||||
"version": "0.1.11",
|
||||
"version": "0.1.12",
|
||||
"publisher": "local",
|
||||
"engines": { "vscode": "^1.80.0" },
|
||||
"categories": ["Other"],
|
||||
|
||||
Reference in New Issue
Block a user