This commit is contained in:
1 parent
3a543a7ca5
commit
5df6fe45e8
3 files changed
+56
-79
No files matched your search
@@ -11,6 +11,7 @@ export interface PostPayload {
|
|||||||
title: string;
|
title: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
content: string;
|
content: string;
|
||||||
|
cover?: string | null;
|
||||||
category_slug?: string;
|
category_slug?: string;
|
||||||
series_slug?: string;
|
series_slug?: string;
|
||||||
tags?: string[];
|
tags?: string[];
|
||||||
@@ -20,6 +21,7 @@ export interface PostMeta {
|
|||||||
id: number;
|
id: number;
|
||||||
slug: string;
|
slug: string;
|
||||||
title: string;
|
title: string;
|
||||||
|
cover?: string | null;
|
||||||
published_at: string;
|
published_at: string;
|
||||||
category_slug?: string;
|
category_slug?: string;
|
||||||
category_name?: string;
|
category_name?: string;
|
||||||
@@ -33,6 +35,33 @@ export interface PostDetail extends PostMeta {
|
|||||||
updated_at: string;
|
updated_at: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatPostMarkdown(detail: PostDetail): string {
|
||||||
|
const frontmatterLines = [
|
||||||
|
'---',
|
||||||
|
`title: "${detail.title.replace(/"/g, '\\"')}"`,
|
||||||
|
`slug: ${detail.slug}`,
|
||||||
|
`blogflare_published_slug: ${detail.slug}`
|
||||||
|
];
|
||||||
|
if (detail.cover) {
|
||||||
|
frontmatterLines.push(`cover: "${detail.cover.replace(/"/g, '\\"')}"`);
|
||||||
|
}
|
||||||
|
if (detail.category_slug) {
|
||||||
|
frontmatterLines.push(`category_slug: ${detail.category_slug}`);
|
||||||
|
}
|
||||||
|
if (detail.series_slug) {
|
||||||
|
frontmatterLines.push(`series_slug: ${detail.series_slug}`);
|
||||||
|
}
|
||||||
|
if (detail.tags && detail.tags.length > 0) {
|
||||||
|
frontmatterLines.push('tags:');
|
||||||
|
for (const t of detail.tags) {
|
||||||
|
frontmatterLines.push(` - ${t}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
frontmatterLines.push('---');
|
||||||
|
|
||||||
|
return frontmatterLines.join('\n') + '\n\n' + (detail.content || '');
|
||||||
|
}
|
||||||
|
|
||||||
export class BlogFlareAPI {
|
export class BlogFlareAPI {
|
||||||
private settings: BlogFlareSettings;
|
private settings: BlogFlareSettings;
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
DEFAULT_SETTINGS,
|
DEFAULT_SETTINGS,
|
||||||
BlogFlareSettingTab,
|
BlogFlareSettingTab,
|
||||||
} from './settings';
|
} from './settings';
|
||||||
import { BlogFlareAPI } from './api';
|
import { BlogFlareAPI, formatPostMarkdown, type PostPayload } from './api';
|
||||||
import { MetaSuggestModal } from './suggest';
|
import { MetaSuggestModal } from './suggest';
|
||||||
import { PullSuggestModal } from './pull_modal';
|
import { PullSuggestModal } from './pull_modal';
|
||||||
|
|
||||||
@@ -169,23 +169,7 @@ export default class BlogFlarePlugin extends Plugin {
|
|||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
try {
|
try {
|
||||||
const detail = await this.api.getPost(item.slug);
|
const detail = await this.api.getPost(item.slug);
|
||||||
|
const fileContent = formatPostMarkdown(detail);
|
||||||
// 组装 Frontmatter
|
|
||||||
const frontmatterLines = [
|
|
||||||
'---',
|
|
||||||
`title: "${detail.title.replace(/"/g, '\\"')}"`,
|
|
||||||
`slug: ${detail.slug}`,
|
|
||||||
`blogflare_published_slug: ${detail.slug}`
|
|
||||||
];
|
|
||||||
if (detail.category_slug) {
|
|
||||||
frontmatterLines.push(`category_slug: ${detail.category_slug}`);
|
|
||||||
}
|
|
||||||
if (detail.series_slug) {
|
|
||||||
frontmatterLines.push(`series_slug: ${detail.series_slug}`);
|
|
||||||
}
|
|
||||||
frontmatterLines.push('---');
|
|
||||||
|
|
||||||
const fileContent = frontmatterLines.join('\n') + '\n\n' + (detail.content || '');
|
|
||||||
|
|
||||||
const baseName = detail.title.replace(/[/\\:]/g, '-');
|
const baseName = detail.title.replace(/[/\\:]/g, '-');
|
||||||
let dirPath = '';
|
let dirPath = '';
|
||||||
@@ -329,29 +313,7 @@ export default class BlogFlarePlugin extends Plugin {
|
|||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
try {
|
try {
|
||||||
const detail = await this.api.getPost(item.slug);
|
const detail = await this.api.getPost(item.slug);
|
||||||
|
const fileContent = formatPostMarkdown(detail);
|
||||||
// 组装 Frontmatter
|
|
||||||
const frontmatterLines = [
|
|
||||||
'---',
|
|
||||||
`title: "${detail.title.replace(/"/g, '\\"')}"`,
|
|
||||||
`slug: ${detail.slug}`,
|
|
||||||
`blogflare_published_slug: ${detail.slug}`
|
|
||||||
];
|
|
||||||
if (detail.category_slug) {
|
|
||||||
frontmatterLines.push(`category_slug: ${detail.category_slug}`);
|
|
||||||
}
|
|
||||||
if (detail.series_slug) {
|
|
||||||
frontmatterLines.push(`series_slug: ${detail.series_slug}`);
|
|
||||||
}
|
|
||||||
if (detail.tags && detail.tags.length > 0) {
|
|
||||||
frontmatterLines.push(`tags:`);
|
|
||||||
for (const t of detail.tags) {
|
|
||||||
frontmatterLines.push(` - ${t}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
frontmatterLines.push('---');
|
|
||||||
|
|
||||||
const fileContent = frontmatterLines.join('\n') + '\n\n' + (detail.content || '');
|
|
||||||
|
|
||||||
const existingFile = slugMap.get(item.slug);
|
const existingFile = slugMap.get(item.slug);
|
||||||
if (existingFile) {
|
if (existingFile) {
|
||||||
@@ -418,12 +380,28 @@ export default class BlogFlarePlugin extends Plugin {
|
|||||||
const series_slug = frontmatter?.series_slug as string | undefined;
|
const series_slug = frontmatter?.series_slug as string | undefined;
|
||||||
const publishedSlug = frontmatter?.blogflare_published_slug as string | undefined;
|
const publishedSlug = frontmatter?.blogflare_published_slug as string | undefined;
|
||||||
|
|
||||||
|
const hasCoverProp = Boolean(
|
||||||
|
frontmatter &&
|
||||||
|
('cover' in frontmatter || 'featured_image' in frontmatter || 'banner' in frontmatter)
|
||||||
|
);
|
||||||
|
const coverCandidate: unknown = frontmatter?.cover ?? frontmatter?.featured_image ?? frontmatter?.banner;
|
||||||
|
let coverVal: string | null | undefined = undefined;
|
||||||
|
if (hasCoverProp) {
|
||||||
|
if (typeof coverCandidate === 'string') {
|
||||||
|
coverVal = coverCandidate.trim() || null;
|
||||||
|
} else if (typeof coverCandidate === 'number' || typeof coverCandidate === 'boolean') {
|
||||||
|
coverVal = String(coverCandidate).trim() || null;
|
||||||
|
} else if (!coverCandidate) {
|
||||||
|
coverVal = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let tags: string[] | undefined = undefined;
|
let tags: string[] | undefined = undefined;
|
||||||
if (frontmatter?.tags) {
|
if (frontmatter?.tags) {
|
||||||
tags = Array.isArray(frontmatter.tags) ? frontmatter.tags : [frontmatter.tags as string];
|
tags = Array.isArray(frontmatter.tags) ? frontmatter.tags : [frontmatter.tags as string];
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = {
|
const payload: PostPayload = {
|
||||||
title,
|
title,
|
||||||
slug,
|
slug,
|
||||||
content,
|
content,
|
||||||
@@ -434,6 +412,9 @@ export default class BlogFlarePlugin extends Plugin {
|
|||||||
|
|
||||||
if (publishedSlug) {
|
if (publishedSlug) {
|
||||||
// 已发布,进行更新
|
// 已发布,进行更新
|
||||||
|
if (coverVal !== undefined) {
|
||||||
|
payload.cover = coverVal;
|
||||||
|
}
|
||||||
const res = await this.api.updatePost(publishedSlug, payload);
|
const res = await this.api.updatePost(publishedSlug, payload);
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
new Notice('文章更新成功!');
|
new Notice('文章更新成功!');
|
||||||
@@ -442,6 +423,7 @@ export default class BlogFlarePlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 未发布,进行创建
|
// 未发布,进行创建
|
||||||
|
payload.cover = coverVal ?? null;
|
||||||
const res = await this.api.createPost(payload);
|
const res = await this.api.createPost(payload);
|
||||||
if (res.success && res.slug) {
|
if (res.success && res.slug) {
|
||||||
// 写入 blogflare_published_slug
|
// 写入 blogflare_published_slug
|
||||||
@@ -478,29 +460,7 @@ export default class BlogFlarePlugin extends Plugin {
|
|||||||
try {
|
try {
|
||||||
new Notice('开始下载文章...');
|
new Notice('开始下载文章...');
|
||||||
const detail = await this.api.getPost(publishedSlug);
|
const detail = await this.api.getPost(publishedSlug);
|
||||||
|
const fileContent = formatPostMarkdown(detail);
|
||||||
// 组装 Frontmatter
|
|
||||||
const frontmatterLines = [
|
|
||||||
'---',
|
|
||||||
`title: "${detail.title.replace(/"/g, '\\"')}"`,
|
|
||||||
`slug: ${detail.slug}`,
|
|
||||||
`blogflare_published_slug: ${detail.slug}`
|
|
||||||
];
|
|
||||||
if (detail.category_slug) {
|
|
||||||
frontmatterLines.push(`category_slug: ${detail.category_slug}`);
|
|
||||||
}
|
|
||||||
if (detail.series_slug) {
|
|
||||||
frontmatterLines.push(`series_slug: ${detail.series_slug}`);
|
|
||||||
}
|
|
||||||
if (detail.tags && detail.tags.length > 0) {
|
|
||||||
frontmatterLines.push(`tags:`);
|
|
||||||
for (const t of detail.tags) {
|
|
||||||
frontmatterLines.push(` - ${t}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
frontmatterLines.push('---');
|
|
||||||
|
|
||||||
const fileContent = frontmatterLines.join('\n') + '\n\n' + (detail.content || '');
|
|
||||||
|
|
||||||
await this.app.vault.modify(file, fileContent);
|
await this.app.vault.modify(file, fileContent);
|
||||||
new Notice('文章下载并更新成功!');
|
new Notice('文章下载并更新成功!');
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { App, FuzzySuggestModal, Notice, TFile } from 'obsidian';
|
import { App, FuzzySuggestModal, Notice, TFile } from 'obsidian';
|
||||||
import type { PostMeta, BlogFlareAPI } from './api';
|
import type { PostMeta, BlogFlareAPI } from './api';
|
||||||
|
import { formatPostMarkdown } from './api';
|
||||||
|
|
||||||
export class PullSuggestModal extends FuzzySuggestModal<PostMeta> {
|
export class PullSuggestModal extends FuzzySuggestModal<PostMeta> {
|
||||||
private items: PostMeta[];
|
private items: PostMeta[];
|
||||||
@@ -24,20 +25,7 @@ export class PullSuggestModal extends FuzzySuggestModal<PostMeta> {
|
|||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const detail = await this.api.getPost(item.slug);
|
const detail = await this.api.getPost(item.slug);
|
||||||
|
const fileContent = formatPostMarkdown(detail);
|
||||||
// 组装 Frontmatter
|
|
||||||
const frontmatterLines = [
|
|
||||||
'---',
|
|
||||||
`title: "${detail.title.replace(/"/g, '\\"')}"`,
|
|
||||||
`slug: ${detail.slug}`,
|
|
||||||
`blogflare_published_slug: ${detail.slug}`
|
|
||||||
];
|
|
||||||
if (detail.category_slug) {
|
|
||||||
frontmatterLines.push(`category_slug: ${detail.category_slug}`);
|
|
||||||
}
|
|
||||||
frontmatterLines.push('---');
|
|
||||||
|
|
||||||
const fileContent = frontmatterLines.join('\n') + '\n\n' + (detail.content || '');
|
|
||||||
|
|
||||||
// 决定文件名 (过滤掉非法字符)
|
// 决定文件名 (过滤掉非法字符)
|
||||||
const baseName = detail.title.replace(/[/\\:]/g, '-');
|
const baseName = detail.title.replace(/[/\\:]/g, '-');
|
||||||
|
|||||||
Reference in new issue
Block a user