mirror of
https://github.com/yeban8090/mp-preview.git
synced 2026-07-22 05:46:27 +00:00
模板的相关优化
This commit is contained in:
parent
26198e1116
commit
70a13986fd
13 changed files with 286 additions and 71 deletions
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"templateId": "elegant",
|
||||
"templateId": "academic",
|
||||
"fontFamily": "Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, \"PingFang SC\", Cambria, Cochin, Georgia, Times, \"Times New Roman\", serif",
|
||||
"fontSize": 15
|
||||
}
|
||||
291
main.js
291
main.js
File diff suppressed because one or more lines are too long
|
|
@ -140,10 +140,7 @@ export class MPConverter {
|
|||
try {
|
||||
// 获取文件的元数据
|
||||
const linktext = src.split('|')[0]; // 处理可能带有别名的链接
|
||||
console.log(src);
|
||||
console.log(linktext);
|
||||
const file = this.app.metadataCache.getFirstLinkpathDest(linktext, '');
|
||||
console.log(file);
|
||||
if (file) {
|
||||
const absolutePath = this.app.vault.adapter.getResourcePath(file.path);
|
||||
const newImg = document.createElement('img');
|
||||
|
|
|
|||
|
|
@ -58,7 +58,6 @@ export class CopyManager {
|
|||
container.style.left = '-9999px';
|
||||
container.innerHTML = element.innerHTML;
|
||||
document.body.appendChild(container);
|
||||
console.log(element.innerHTML);
|
||||
// 处理图片转换为 base64
|
||||
await this.processImages(container);
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { MPView, VIEW_TYPE_MP } from './view';
|
|||
import { TemplateManager } from './templateManager';
|
||||
import { SettingsManager } from './settings';
|
||||
import { MPConverter } from './converter';
|
||||
|
||||
import { DonateManager } from './donateManager';
|
||||
export default class MPPlugin extends Plugin {
|
||||
private settingsManager: SettingsManager;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { App } from 'obsidian';
|
||||
import { templates } from './templates';
|
||||
|
||||
interface Template {
|
||||
id: string;
|
||||
|
|
@ -56,40 +57,13 @@ export class TemplateManager {
|
|||
|
||||
public async loadTemplates() {
|
||||
try {
|
||||
const configDir = this.app.vault.configDir;
|
||||
const templatesPath = `${configDir}/plugins/obsidian-to-mp/templates`;
|
||||
|
||||
// 加载默认模板
|
||||
const defaultTemplate = JSON.parse(
|
||||
await this.app.vault.adapter.read(`${templatesPath}/default.json`)
|
||||
);
|
||||
this.templates.set(defaultTemplate.id, defaultTemplate);
|
||||
this.currentTemplate = defaultTemplate;
|
||||
|
||||
// 加载极简主题
|
||||
const minimalTemplate = JSON.parse(
|
||||
await this.app.vault.adapter.read(`${templatesPath}/minimal.json`)
|
||||
);
|
||||
this.templates.set(minimalTemplate.id, minimalTemplate);
|
||||
|
||||
// 加载优雅主题
|
||||
const elegantTemplate = JSON.parse(
|
||||
await this.app.vault.adapter.read(`${templatesPath}/elegant.json`)
|
||||
);
|
||||
this.templates.set(elegantTemplate.id, elegantTemplate);
|
||||
|
||||
// 加载深色主题
|
||||
const darkTemplate = JSON.parse(
|
||||
await this.app.vault.adapter.read(`${templatesPath}/dark.json`)
|
||||
);
|
||||
this.templates.set(darkTemplate.id, darkTemplate);
|
||||
|
||||
// 加载学术主题
|
||||
const academicTemplate = JSON.parse(
|
||||
await this.app.vault.adapter.read(`${templatesPath}/academic.json`)
|
||||
);
|
||||
this.templates.set(academicTemplate.id, academicTemplate);
|
||||
|
||||
// 直接从内置模板加载
|
||||
Object.values(templates).forEach(template => {
|
||||
this.templates.set(template.id, template);
|
||||
if (template.id === 'default') {
|
||||
this.currentTemplate = template;
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('加载模板失败:', error);
|
||||
throw new Error('无法加载模板文件');
|
||||
|
|
|
|||
14
src/templates/index.ts
Normal file
14
src/templates/index.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// 使用 require 导入 JSON 文件以避免 TypeScript 的 JSON 模块解析问题
|
||||
const defaultTemplate = require('./default.json');
|
||||
const minimalTemplate = require('./minimal.json');
|
||||
const elegantTemplate = require('./elegant.json');
|
||||
const darkTemplate = require('./dark.json');
|
||||
const academicTemplate = require('./academic.json');
|
||||
|
||||
export const templates = {
|
||||
default: defaultTemplate,
|
||||
minimal: minimalTemplate,
|
||||
elegant: elegantTemplate,
|
||||
dark: darkTemplate,
|
||||
academic: academicTemplate
|
||||
};
|
||||
|
|
@ -20,5 +20,5 @@
|
|||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
]
|
||||
, "templates/index.ts" ]
|
||||
}
|
||||
Loading…
Reference in a new issue