obsidian社区插件
				
				QuickAdd 脚本:为图片添加题注
				
				
				
				
				
				
				
					插件ID:quickadd%E8%84%9A%E6%9C%AC-%E4%B8%BA%E5%9B%BE%E7%89%87%E6%B7%BB%E5%8A%A0%E9%A2%98%E6%B3%A8
					quickadd%E8%84%9A%E6%9C%AC-%E4%B8%BA%E5%9B%BE%E7%89%87%E6%B7%BB%E5%8A%A0%E9%A2%98%E6%B3%A8
					quickadd%E8%84%9A%E6%9C%AC %E4%B8%BA%E5%9B%BE%E7%89%87%E6%B7%BB%E5%8A%A0%E9%A2%98%E6%B3%A8:使用QuickAdd脚本为图片添加题注
				
			QuickAdd 脚本:为图片添加题注
需求
由于目前我着手的库中有许多笔记经由同一个模板,且其中的图片都需要以固定格式进行题注命名,并通过图片的 metadata 控制图片大小保持一致,因此我撰写了这个脚本。
功能
- 将图片的文件名填入到图片题注中,例如 在经过脚本转换后变为
- 这是一个可选功能,在执行脚本后为图片添加上固定的 metadata 参数,如 
注意
本脚本仅限非 Wiki 链接形式的图片使用。
效果展示:

脚本
// autoImageCaption.js by @ProudBenzene
let editor;
let activeLeaf = this.app.workspace.activeLeaf;
if (activeLeaf.view.getViewType() === 'markdown') {
	editor = activeLeaf.view.sourceMode.cmEditor;
}
// 现在你可以在这个作用域内使用editor对象了
const { exec } = require('child_process');
const path = require("path");
let QuickAdd;
module.exports = async function autoImageCaptions(params) {
	QuickAdd = params;
	new Notice(`题注自动生成完毕`, 5000);
};
// 获取笔记的基本路径
const filePath = app.workspace.getActiveFile().path;
const fileFullPath = app.vault.adapter.getFullPath(filePath)
// 获取选中的文本
const editors = app.workspace.activeLeaf.view.sourceMode.cmEditor;
const selection = editors.getSelection();
console.log(selection);
// 分情况讨论(选中的是链接本身还是链接中的文件名)
let selectionEmbed;
if (selection.includes("[[")) { // 如果选中的是Wiki链接本身
    selectionEmbed = matchSelectionEmbed(selection);
} else { // 如果选中的是Wiki链接中的文件名
    selectionEmbed = selection;
}
console.log(selectionEmbed)
// 分情况讨论(wiki链接或是标准markdown链接)
if (!selection.includes("(")) {
    // Wiki: 获取库所有文件列表
	const regex = /\/([^\/]*?)\./g;
	let decodedSelection = decodeURIComponent(selectionEmbed);
	let matches;
	let wikiName;
	while ((matches = regex.exec(decodedSelection)) !== null) {
		wikiName = matches[1];
	}
	console.log(wikiName)
	// 用wikiName替换[]中的内容
	if (!selection.includes("]]")) {
		const newSelection = `${selectionEmbed}|${wikiName}`;
		console.log(newSelection);
		editor.replaceSelection(newSelection);
	} else {
		const newSelection = selectionEmbed.replace(/\]\]/, `|${wikiName}]]`);
		console.log(newSelection);
		editor.replaceSelection(newSelection);
	}
} else {
    // 通过正则依次获取图片路径、图片名称并将其解码为文本
    const regex1 = /\((.*?)\)/;
    const matches1 = regex1.exec(selection);
    const selectionPath = matches1[1]; //去掉嵌入语法后的图片路径
    console.log(selectionPath);
	const regex = /.*\/(.*?)\./;
	const decodedSelection = decodeURIComponent(selectionPath);
	const matches2 = regex.exec(decodedSelection);
	let imageName = '';
	if (matches2 && matches2[1]) {
		imageName = matches2[1]/* + "|inlL|300"*/; /*修改此处可以为图片增加固定metadata控制大小*/
	}
	console.log(imageName);
	// 用imageName替换[]中的内容
	const newSelection = selection.replace(/\[.*?\]/, `[${imageName}]`);
	editor.replaceSelection(newSelection);
}
// 获取Wiki路径
function getFilePath(files, link) {
    let files2 = files.filter(f => path.basename(f.path).endsWith(link.replace(/\[\[/, "").replace(/\|.*]]/, "").replace(/\]\]/, "")));
    let filePath = files2.map((f) => f.path)[0];
    console.log(filePath);
    return filePath;
}
function matchSelectionEmbed(text) {
    const regex = /(\[\[.*\]\])/;
    const matches = text.match(regex);
    return matches ? matches[1] : "";
}
讨论
若阁下有独到的见解或新颖的想法,诚邀您在文章下方留言,与大家共同探讨。
反馈交流
其他渠道
版权声明
版权声明:所有 PKMer 文章如果需要转载,请附上原文出处链接。
 
										 
						
										