mirror of
https://github.com/woodchen-ink/obsidian-publish-to-discourse.git
synced 2025-07-17 13:23:33 +08:00
Refactor plugin for publishing to Discourse: renamed plugin, updated metadata, localized UI elements to Chinese, removed unused CSS, and upgraded GitHub Actions for Node.js environment.
This commit is contained in:
parent
5337ccd33c
commit
70d7803527
6
.github/workflows/release.yml
vendored
6
.github/workflows/release.yml
vendored
@ -10,12 +10,12 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Use Node.js
|
||||
uses: actions/setup-node@v3
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "18.x"
|
||||
node-version: "20.x"
|
||||
|
||||
- name: Build plugin
|
||||
run: |
|
||||
|
@ -1,11 +1,11 @@
|
||||
{
|
||||
"id": "discourse-sync",
|
||||
"name": "Sync to Discourse",
|
||||
"version": "1.0.4",
|
||||
"id": "publish-to-discourse",
|
||||
"name": "发布到 Discourse",
|
||||
"version": "1.0.0",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "Sync notes to a Discourse server.",
|
||||
"author": "Tim Finley",
|
||||
"authorUrl": "https://finley-group.com",
|
||||
"fundingUrl": "https://finley-group.com",
|
||||
"description": "发布笔记到 Discourse 论坛。",
|
||||
"author": "WoodChen",
|
||||
"authorUrl": "https://woodchen.ink",
|
||||
"fundingUrl": "https://woodchen.ink",
|
||||
"isDesktopOnly": true
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "obsidian-sample-plugin",
|
||||
"name": "obsidian-publish-to-discourse",
|
||||
"version": "1.0.0",
|
||||
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
|
||||
"description": "发布笔记到 Discourse 论坛。",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
|
@ -24,11 +24,11 @@ export class DiscourseSyncSettingsTab extends PluginSettingTab {
|
||||
display(): void {
|
||||
const { containerEl } = this;
|
||||
containerEl.empty();
|
||||
containerEl.createEl("h2", { text: "Discourse Sync" });
|
||||
containerEl.createEl("h2", { text: "发布到 Discourse" });
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Base URL")
|
||||
.setDesc("base url of discourse server")
|
||||
.setName("论坛地址")
|
||||
.setDesc("Discourse 论坛的网址")
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder("https://forum.example.com")
|
||||
@ -40,8 +40,8 @@ export class DiscourseSyncSettingsTab extends PluginSettingTab {
|
||||
);
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("API Key")
|
||||
.setDesc("user created API Key")
|
||||
.setName("API 密钥")
|
||||
.setDesc("用户创建的 API 密钥")
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder("api_key")
|
||||
@ -53,8 +53,8 @@ export class DiscourseSyncSettingsTab extends PluginSettingTab {
|
||||
);
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Username")
|
||||
.setDesc("Discourse Username")
|
||||
.setName("用户名")
|
||||
.setDesc("Discourse 用户名")
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder("username")
|
||||
|
112
src/main.ts
112
src/main.ts
@ -15,7 +15,7 @@ export default class DiscourseSyncPlugin extends Plugin {
|
||||
|
||||
this.addCommand({
|
||||
id: "category-modal",
|
||||
name: "Category Modal",
|
||||
name: "发布到 Discourse",
|
||||
callback: () => {
|
||||
this.openCategoryModal();
|
||||
},
|
||||
@ -118,6 +118,10 @@ export default class DiscourseSyncPlugin extends Plugin {
|
||||
"Api-Username": this.settings.disUser,
|
||||
}
|
||||
let content = this.activeFile.content;
|
||||
|
||||
// 过滤掉笔记属性部分
|
||||
content = content.replace(/^---[\s\S]*?---\n/, '');
|
||||
|
||||
const imageReferences = this.extractImageReferences(content);
|
||||
const imageUrls = await this.uploadImages(imageReferences);
|
||||
|
||||
@ -193,7 +197,7 @@ export default class DiscourseSyncPlugin extends Plugin {
|
||||
|
||||
registerDirMenu(menu: Menu, file: TFile) {
|
||||
const syncDiscourse = (item: MenuItem) => {
|
||||
item.setTitle("Sync to Discourse");
|
||||
item.setTitle("发布到 Discourse");
|
||||
item.onClick(async () => {
|
||||
this.activeFile = {
|
||||
name: file.basename,
|
||||
@ -273,28 +277,124 @@ export class SelectCategoryModal extends Modal {
|
||||
|
||||
onOpen() {
|
||||
const { contentEl } = this;
|
||||
contentEl.createEl("h1", { text: 'Select a category for syncing' });
|
||||
const selectEl = contentEl.createEl('select');
|
||||
|
||||
// 添加样式
|
||||
contentEl.addClass('discourse-sync-modal');
|
||||
const styleEl = document.head.createEl('style');
|
||||
styleEl.textContent = `
|
||||
.discourse-sync-modal {
|
||||
padding: 20px;
|
||||
}
|
||||
.discourse-sync-modal h1 {
|
||||
margin-bottom: 20px;
|
||||
color: var(--text-normal);
|
||||
}
|
||||
.discourse-sync-modal .select-container {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
.discourse-sync-modal select {
|
||||
width: 100%;
|
||||
padding: 8px 12px;
|
||||
height: 42px;
|
||||
line-height: 1.5;
|
||||
border: 1px solid var(--background-modifier-border);
|
||||
border-radius: 4px;
|
||||
background-color: var(--background-primary);
|
||||
color: var(--text-normal);
|
||||
}
|
||||
.discourse-sync-modal .submit-button {
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
background-color: var(--interactive-accent);
|
||||
color: var(--text-on-accent);
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-weight: 500;
|
||||
}
|
||||
.discourse-sync-modal .submit-button:hover {
|
||||
background-color: var(--interactive-accent-hover);
|
||||
}
|
||||
.discourse-sync-modal .notice {
|
||||
margin-top: 16px;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
text-align: center;
|
||||
}
|
||||
.discourse-sync-modal .notice.success {
|
||||
background-color: var(--background-modifier-success);
|
||||
color: var(--text-success);
|
||||
}
|
||||
.discourse-sync-modal .notice.error {
|
||||
background-color: var(--background-modifier-error);
|
||||
color: var(--text-error);
|
||||
}
|
||||
`;
|
||||
|
||||
contentEl.createEl("h1", { text: '选择发布分类' });
|
||||
|
||||
// 创建选择器容器
|
||||
const selectContainer = contentEl.createEl('div', { cls: 'select-container' });
|
||||
const selectLabel = selectContainer.createEl('label', { text: '分类' });
|
||||
selectLabel.style.display = 'block';
|
||||
selectLabel.style.marginBottom = '8px';
|
||||
|
||||
const selectEl = selectContainer.createEl('select');
|
||||
|
||||
this.categories.forEach(category => {
|
||||
const option = selectEl.createEl('option', { text: category.name });
|
||||
option.value = category.id.toString();
|
||||
});
|
||||
|
||||
const submitButton = contentEl.createEl('button', { text: 'Submit' });
|
||||
const submitButton = contentEl.createEl('button', {
|
||||
text: '发布',
|
||||
cls: 'submit-button'
|
||||
});
|
||||
|
||||
// 创建提示信息容器
|
||||
const noticeContainer = contentEl.createEl('div');
|
||||
|
||||
|
||||
submitButton.onclick = async () => {
|
||||
const selectedCategoryId = selectEl.value;
|
||||
this.plugin.settings.category = parseInt(selectedCategoryId);
|
||||
await this.plugin.saveSettings();
|
||||
|
||||
// 禁用提交按钮,显示加载状态
|
||||
submitButton.disabled = true;
|
||||
submitButton.textContent = '发布中...';
|
||||
|
||||
const reply = await this.plugin.postTopic();
|
||||
console.log(`postTopic message: ${reply.message}`);
|
||||
console.log(`ID: ${selectedCategoryId}`);
|
||||
this.close();
|
||||
|
||||
// 显示提示信息
|
||||
noticeContainer.empty();
|
||||
noticeContainer.createEl('div', {
|
||||
cls: `notice ${reply.message === 'Success' ? 'success' : 'error'}`,
|
||||
text: reply.message === 'Success' ? '发布成功!' : '发布失败,请重试。'
|
||||
});
|
||||
|
||||
if (reply.message === 'Success') {
|
||||
// 成功后延迟关闭
|
||||
setTimeout(() => {
|
||||
this.close();
|
||||
}, 1500);
|
||||
} else {
|
||||
// 失败时重置按钮状态
|
||||
submitButton.disabled = false;
|
||||
submitButton.textContent = '发布';
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
onClose() {
|
||||
const { contentEl } = this;
|
||||
contentEl.empty();
|
||||
// 清理添加的样式
|
||||
const styleEl = document.head.querySelector('style:last-child');
|
||||
if (styleEl) {
|
||||
styleEl.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
/*
|
||||
|
||||
This CSS file will be included with your plugin, and
|
||||
available in the app when your plugin is enabled.
|
||||
|
||||
If your plugin does not need CSS, delete this file.
|
||||
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user