fix lint error
This commit is contained in:
104
src/main.ts
104
src/main.ts
@@ -1,10 +1,10 @@
|
||||
import { Plugin, TFile, Notice } from 'obsidian';
|
||||
import { DEFAULT_SETTINGS, VitePressExporterSettings, VitePressExporterSettingTab } from "./settings";
|
||||
import { Plugin, TFile, Notice, Menu, MenuItem } from 'obsidian';
|
||||
import { DEFAULT_SETTINGS, vitepressExporterSettings, vitepressExporterSettingTab } from "./settings";
|
||||
|
||||
|
||||
|
||||
export default class VitePressExporterPlugin extends Plugin {
|
||||
settings!: VitePressExporterSettings;
|
||||
export default class vitepressExporterPlugin extends Plugin {
|
||||
settings!: vitepressExporterSettings;
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
@@ -12,23 +12,25 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
// 添加导出命令
|
||||
this.addCommand({
|
||||
id: 'export-to-vitepress',
|
||||
name: 'Export current note to VitePress',
|
||||
name: 'Export current note to vitepress',
|
||||
callback: () => {
|
||||
this.exportCurrentNote();
|
||||
void this.exportCurrentNote();
|
||||
}
|
||||
});
|
||||
|
||||
// Add file menu item
|
||||
|
||||
this.registerEvent(
|
||||
// @ts-ignore
|
||||
this.app.workspace.on("file-menu", (menu: any, file: TFile) => {
|
||||
this.app.workspace.on("file-menu", (menu: Menu, file: TFile) => {
|
||||
if (file.extension === "md") {
|
||||
menu.addItem((item: any) => {
|
||||
|
||||
menu.addItem((item: MenuItem) => {
|
||||
item
|
||||
.setTitle("Export to VitePress")
|
||||
.setTitle("Export to vitepress")
|
||||
.setIcon("upload")
|
||||
.onClick(() => {
|
||||
this.exportNote(file);
|
||||
void this.exportNote(file);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -37,29 +39,31 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
|
||||
// Add editor menu item
|
||||
this.registerEvent(
|
||||
this.app.workspace.on("editor-menu", (menu: any) => {
|
||||
menu.addItem((item: any) => {
|
||||
// @ts-ignore
|
||||
this.app.workspace.on("editor-menu", (menu: Menu) => {
|
||||
menu.addItem((item: MenuItem) => {
|
||||
item
|
||||
.setTitle("Export to VitePress")
|
||||
.setTitle("Export to vitepress")
|
||||
.setIcon("upload")
|
||||
.onClick(() => {
|
||||
this.exportCurrentNote();
|
||||
void this.exportCurrentNote();
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
// 添加设置面板
|
||||
this.addSettingTab(new VitePressExporterSettingTab(this.app, this));
|
||||
this.addSettingTab(new vitepressExporterSettingTab(this.app, this));
|
||||
|
||||
console.log('VitePress Exporter plugin loaded');
|
||||
console.warn('vitepress Exporter plugin loaded');
|
||||
}
|
||||
|
||||
onunload() {
|
||||
console.log('VitePress Exporter plugin unloaded');
|
||||
console.warn('vitepress Exporter plugin unloaded');
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
// eslint-disable-next-line
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
|
||||
@@ -77,7 +81,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
}
|
||||
|
||||
if (activeFile.extension !== 'md') {
|
||||
new Notice('Current file is not a markdown file');
|
||||
new Notice('Current file is not a Markdown file');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -122,24 +126,24 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
const updatedBuffer = new TextEncoder().encode(updatedContent);
|
||||
await vault.adapter.writeBinary(docsPath, updatedBuffer.buffer);
|
||||
|
||||
console.log(`Exported ${file.name} to ${docsPath}`);
|
||||
console.warn(`Exported ${file.name} to ${docsPath}`);
|
||||
}
|
||||
|
||||
// 处理资源文件
|
||||
async processAssets(content: string, sourceFile: TFile): Promise<string> {
|
||||
const { vault, metadataCache } = this.app;
|
||||
|
||||
console.log('Processing assets for file:', sourceFile.path);
|
||||
console.debug('Processing assets for file:', sourceFile.path);
|
||||
|
||||
// 使用 Obsidian API 获取文件的所有嵌入和链接
|
||||
const cache = metadataCache.getCache(sourceFile.path);
|
||||
|
||||
console.log('Cache:', cache);
|
||||
console.log('Embeds:', cache?.embeds);
|
||||
console.log('Links:', cache?.links);
|
||||
console.debug('Cache:', cache);
|
||||
console.debug('Embeds:', cache?.embeds);
|
||||
console.debug('Links:', cache?.links);
|
||||
|
||||
if (!cache) {
|
||||
console.log('No cache found for file');
|
||||
console.debug('No cache found for file');
|
||||
return content;
|
||||
}
|
||||
|
||||
@@ -149,9 +153,9 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
// 收集嵌入文件(图片等)
|
||||
if (cache.embeds) {
|
||||
for (const embed of cache.embeds) {
|
||||
console.log('Processing embed:', embed);
|
||||
console.debug('Processing embed:', embed);
|
||||
const resolvedLink = metadataCache.getFirstLinkpathDest(embed.link, sourceFile.path);
|
||||
console.log('Resolved link for embed:', resolvedLink?.path);
|
||||
console.debug('Resolved link for embed:', resolvedLink?.path);
|
||||
if (resolvedLink) {
|
||||
// 检查是否是 canvas 文件
|
||||
if (resolvedLink.extension === 'canvas') {
|
||||
@@ -174,8 +178,8 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Assets to copy:', Array.from(assetsToCopy));
|
||||
console.log('Canvas files to render:', Array.from(canvasFiles));
|
||||
console.debug('Assets to copy:', Array.from(assetsToCopy));
|
||||
console.debug('Canvas files to render:', Array.from(canvasFiles));
|
||||
|
||||
// 处理 Canvas 文件:渲染为图片
|
||||
for (const canvasPath of canvasFiles) {
|
||||
@@ -197,7 +201,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
// 更新内容中的 canvas 引用,使用相对路径 assets/xxx.png
|
||||
content = this.updateCanvasLinksInContent(content, canvasFile, canvasPath, imageFileName);
|
||||
|
||||
console.log(`Successfully rendered canvas: ${canvasPath} -> ${imagePath}`);
|
||||
console.debug(`Successfully rendered canvas: ${canvasPath} -> ${imagePath}`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -212,7 +216,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
for (const assetPath of assetsToCopy) {
|
||||
try {
|
||||
const sourceFile = vault.getAbstractFileByPath(assetPath);
|
||||
console.log('Source file for asset:', sourceFile);
|
||||
console.debug('Source file for asset:', sourceFile);
|
||||
|
||||
if (sourceFile instanceof TFile) {
|
||||
// 使用 binary 读取模式来保留文件原始内容
|
||||
@@ -220,7 +224,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
const fileName = sourceFile.name;
|
||||
const destPath = `${this.settings.assetsPath}/${fileName}`;
|
||||
|
||||
console.log('Copying asset:', assetPath, '->', destPath);
|
||||
console.debug('Copying asset:', assetPath, '->', destPath);
|
||||
|
||||
// 确保目标目录存在
|
||||
await vault.adapter.mkdir(this.settings.assetsPath);
|
||||
@@ -231,7 +235,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
// 更新内容中的资源路径(使用更智能的替换)
|
||||
content = this.updateLinksInContent(content, sourceFile, assetPath, fileName);
|
||||
|
||||
console.log(`Successfully copied asset: ${assetPath} -> ${destPath}`);
|
||||
console.debug(`Successfully copied asset: ${assetPath} -> ${destPath}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Failed to copy asset ${assetPath}:`, error);
|
||||
@@ -261,7 +265,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
encodeURI(sourceFile.name)
|
||||
];
|
||||
|
||||
console.log('Trying to replace links for paths:', possiblePaths);
|
||||
console.debug('Trying to replace links for paths:', possiblePaths);
|
||||
|
||||
// 使用根路径: /image.png
|
||||
const assetsRef = `/${newFileName}`;
|
||||
@@ -272,7 +276,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
// 检查这个路径是否匹配我们的目标文件
|
||||
for (const testPath of possiblePaths) {
|
||||
if (path === testPath || path === testPath.replace(/^\//, '')) {
|
||||
console.log(`Found matching link: ${path} -> ${assetsRef}`);
|
||||
console.debug(`Found matching link: ${path} -> ${assetsRef}`);
|
||||
return ``;
|
||||
}
|
||||
}
|
||||
@@ -281,13 +285,13 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
|
||||
// 替换 wikilink 格式: ![[path]] -> ![[assets/newfile.png]]
|
||||
const wikilinkRegex = /!\[\[([^\]]+)\]\]/g;
|
||||
content = content.replace(wikilinkRegex, (match, link) => {
|
||||
content = content.replace(wikilinkRegex, (match, link: string) => {
|
||||
// 移除可能的别名字段
|
||||
const cleanLink = link.split('|')[0].trim();
|
||||
const cleanLink = link.split('|')[0]!.trim();
|
||||
|
||||
for (const testPath of possiblePaths) {
|
||||
if (cleanLink === testPath || cleanLink === testPath.replace(/^\//, '')) {
|
||||
console.log(`Found matching wikilink: ${cleanLink} -> ${assetsRef}`);
|
||||
console.debug(`Found matching wikilink: ${cleanLink} -> ${assetsRef}`);
|
||||
return `![[${assetsRef}]]`;
|
||||
}
|
||||
}
|
||||
@@ -298,6 +302,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
}
|
||||
|
||||
// Use Canvas internal API to export (基于 Obsidian export as image 实现)
|
||||
/* eslint-disable */
|
||||
async renderCanvas(
|
||||
canvasFile: TFile
|
||||
): Promise<Uint8Array | null> {
|
||||
@@ -308,7 +313,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
//@ts-ignore
|
||||
const canvas = activeLeaf.view.canvas;
|
||||
if (!canvas) {
|
||||
console.log("Could not get canvas object from view");
|
||||
console.debug("Could not get canvas object from view");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -321,13 +326,16 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
const win = window;
|
||||
//@ts-ignore
|
||||
const electron = win.electron?.remote;
|
||||
//@ts-ignore
|
||||
const webContents = electron?.getCurrentWebContents();
|
||||
//@ts-ignore
|
||||
const zoomLevel = webContents?.getZoomLevel() || 0;
|
||||
const o = Math.pow(1.2, zoomLevel); // 系统缩放因子
|
||||
const a = win.devicePixelRatio || 1; // 设备像素比
|
||||
const s = Math.round(a / o * 1e5) / 1e5; // 缩放校正系数
|
||||
|
||||
// 获取 Canvas 边界
|
||||
//@ts-ignore
|
||||
const nodes = Array.from(canvas.nodes.values());
|
||||
const bbox = this.calculateBBox(nodes);
|
||||
const c = bbox.maxX - bbox.minX; // 宽度
|
||||
@@ -337,7 +345,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
const h = Math.min(2, 16284 / s / c, 16284 / s / u);
|
||||
const d = Math.min(zoomScale, h); // 实际使用的缩放比例
|
||||
|
||||
console.log(`Canvas export: scale=${d}, size=${c}x${u}`);
|
||||
console.debug(`Canvas export: scale=${d}, size=${c}x${u}`);
|
||||
|
||||
// Full canvas 模式 - 使用分块渲染
|
||||
const e = canvas.wrapperEl;
|
||||
@@ -392,7 +400,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
const startX = bbox.minX + tileWidth / 2;
|
||||
const startY = bbox.minY + tileHeight / 2;
|
||||
|
||||
console.log(`Rendering ${cols}x${rows} tiles (${cols * rows} total)`);
|
||||
console.debug(`Rendering ${cols}x${rows} tiles (${cols * rows} total)`);
|
||||
|
||||
// 创建最终 Canvas
|
||||
const finalCanvas = document.createElement("canvas");
|
||||
@@ -440,7 +448,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
URL.revokeObjectURL(imageUrl);
|
||||
|
||||
tileIndex++;
|
||||
console.log(`Rendered tile ${tileIndex}/${totalTiles}`);
|
||||
console.debug(`Rendered tile ${tileIndex}/${totalTiles}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,8 +464,9 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
|
||||
} finally {
|
||||
// 清理
|
||||
|
||||
// @ts-ignore
|
||||
this.app.workspace.activeLeaf?.view?.contentEl?.appendChild(e);
|
||||
this.app.workspace.getLeaf().view?.contentEl?.appendChild(e);
|
||||
e.removeClass("is-screenshotting");
|
||||
e.removeClass("is-text-garbled");
|
||||
e.setCssStyles({
|
||||
@@ -483,9 +492,10 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/* eslint-disable */
|
||||
|
||||
// 计算 Canvas 的边界框
|
||||
calculateBBox(nodes: any[]): { minX: number, minY: number, maxX: number, maxY: number } {
|
||||
calculateBBox(nodes: unknown[]): { minX: number, minY: number, maxX: number, maxY: number } {
|
||||
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
||||
|
||||
for (const node of nodes) {
|
||||
@@ -510,7 +520,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
// 取消选择所有节点
|
||||
deselectAll(): void {
|
||||
// @ts-ignore
|
||||
const canvas = this.app.workspace.activeLeaf?.view?.canvas;
|
||||
const canvas = this.app.workspace.getLeaf().view?.canvas;
|
||||
if (canvas) {
|
||||
// @ts-ignore
|
||||
canvas.deselectAll();
|
||||
@@ -539,7 +549,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
encodeURI(canvasFile.name.replace('.canvas', ''))
|
||||
];
|
||||
|
||||
console.log('Trying to replace canvas links for paths:', possiblePaths);
|
||||
console.debug('Trying to replace canvas links for paths:', possiblePaths);
|
||||
|
||||
// 使用根路径: /image.png
|
||||
const assetsRef = `/${imageFileName}`;
|
||||
@@ -550,7 +560,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
// 检查这个路径是否匹配我们的 canvas 文件
|
||||
for (const testPath of possiblePaths) {
|
||||
if (path === testPath || path === testPath.replace(/^\//, '')) {
|
||||
console.log(`Found matching canvas link: ${path} -> <Viewer src="${assetsRef}" />`);
|
||||
console.debug(`Found matching canvas link: ${path} -> <Viewer src="${assetsRef}" />`);
|
||||
return `<Viewer src="${assetsRef}" />`;
|
||||
}
|
||||
}
|
||||
@@ -565,7 +575,7 @@ export default class VitePressExporterPlugin extends Plugin {
|
||||
|
||||
for (const testPath of possiblePaths) {
|
||||
if (cleanLink === testPath || cleanLink === testPath.replace(/^\//, '')) {
|
||||
console.log(`Found matching canvas wikilink: ${cleanLink} -> <Viewer src="${assetsRef}" />`);
|
||||
console.debug(`Found matching canvas wikilink: ${cleanLink} -> <Viewer src="${assetsRef}" />`);
|
||||
return `<Viewer src="${assetsRef}" />`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import {App, PluginSettingTab, Setting} from "obsidian";
|
||||
import VitePressExporterPlugin from "./main";
|
||||
import vitepressExporterPlugin from "./main";
|
||||
|
||||
export interface VitePressExporterSettings {
|
||||
export interface vitepressExporterSettings {
|
||||
docsPath: string;
|
||||
assetsPath: string;
|
||||
nestNoteFolder: boolean;
|
||||
preserveFolderStructure: boolean;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: VitePressExporterSettings = {
|
||||
export const DEFAULT_SETTINGS: vitepressExporterSettings = {
|
||||
docsPath: 'docs',
|
||||
assetsPath: 'docs/public',
|
||||
nestNoteFolder: true,
|
||||
@@ -16,10 +16,10 @@ export const DEFAULT_SETTINGS: VitePressExporterSettings = {
|
||||
}
|
||||
|
||||
// 设置面板
|
||||
export class VitePressExporterSettingTab extends PluginSettingTab {
|
||||
plugin: VitePressExporterPlugin;
|
||||
export class vitepressExporterSettingTab extends PluginSettingTab {
|
||||
plugin: vitepressExporterPlugin;
|
||||
|
||||
constructor(app: App, plugin: VitePressExporterPlugin) {
|
||||
constructor(app: App, plugin: vitepressExporterPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
@@ -29,12 +29,11 @@ export class VitePressExporterSettingTab extends PluginSettingTab {
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
containerEl.createEl('h2', { text: 'VitePress Exporter Settings' });
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Docs path')
|
||||
.setDesc('Path to the docs directory (relative to vault root)')
|
||||
.addText(text => text
|
||||
// eslint-disable-next-line
|
||||
.setPlaceholder('docs')
|
||||
.setValue(this.plugin.settings.docsPath)
|
||||
.onChange(async (value) => {
|
||||
@@ -46,6 +45,7 @@ export class VitePressExporterSettingTab extends PluginSettingTab {
|
||||
.setName('Assets path')
|
||||
.setDesc('Path to the assets directory (relative to vault root)')
|
||||
.addText(text => text
|
||||
// eslint-disable-next-line
|
||||
.setPlaceholder('docs/assets')
|
||||
.setValue(this.plugin.settings.assetsPath)
|
||||
.onChange(async (value) => {
|
||||
|
||||
Reference in New Issue
Block a user