diff --git a/.env.local.example b/.env.local.example index f6843246..53e14f0d 100644 --- a/.env.local.example +++ b/.env.local.example @@ -24,5 +24,10 @@ CONF_API_URL= CONF_USERNAME= CONF_PASSWORD= +# wordpress +WORDPRESS_API_URL= +WORDPRESS_USERNAME= +WORDPRESS_PASSWORD= + # plantuml server PLANT_UML_SERVR= \ No newline at end of file diff --git a/lib/api.ts b/lib/api.ts index 84abf2ea..706e75f5 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -5,6 +5,7 @@ import {ConfApiAdaptor} from "./metaweblog/confApiAdaptor"; import {CnblogsApiAdaptor} from "./metaweblog/cnblogsApiAdaptor"; import {Post} from "./common/post"; import {UserBlog} from "./common/userBlog"; +import {WordpressApiAdaptor} from "./metaweblog/WordpressApiAdaptor"; export interface IApi { /** @@ -47,6 +48,9 @@ export class API implements IApi { case API_TYPE_CONSTANTS.API_TYPE_CNBLOGS: this.apiAdaptor = new CnblogsApiAdaptor() break; + case API_TYPE_CONSTANTS.API_TYPE_WORDPRESS: + this.apiAdaptor = new WordpressApiAdaptor() + break; default: throw new Error("未找到接口适配器,请检查参数") } diff --git a/lib/constants.ts b/lib/constants.ts index 254ff3fb..54117072 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -14,6 +14,10 @@ const API_TYPE_CONF = "conf" * Cnblogs */ const API_TYPE_CNBLOGS = "cnblogs" +/** + * Wordpress + */ +const API_TYPE_WORDPRESS = "wordpress" /** * API类型常量定义 @@ -22,7 +26,8 @@ export const API_TYPE_CONSTANTS = { API_TYPE_SIYUAN, API_TYPE_JVUE, API_TYPE_CONF, - API_TYPE_CNBLOGS + API_TYPE_CNBLOGS, + API_TYPE_WORDPRESS } /** diff --git a/lib/htmlUtil.ts b/lib/htmlUtil.ts index 7805c0b2..f8405998 100644 --- a/lib/htmlUtil.ts +++ b/lib/htmlUtil.ts @@ -97,7 +97,7 @@ function filterHtml(str: string) { const excludeWords = ['\\d*/\\d/\\d*', '[、|\\\\]', '[,|,]', '\\d', '/', '-'] for (let i = 0; i < excludeWords.length; i++) { const regex = new RegExp(excludeWords[i], "g"); - str = str.replaceAll(regex, "") + str = str.replace(regex, "") } str = str.toLowerCase(); diff --git a/lib/metaweblog/WordpressApiAdaptor.ts b/lib/metaweblog/WordpressApiAdaptor.ts new file mode 100644 index 00000000..47a8cd66 --- /dev/null +++ b/lib/metaweblog/WordpressApiAdaptor.ts @@ -0,0 +1,20 @@ +import {MetaWeblogApiAdaptor} from "./metaWeblogApiAdaptor"; +import {IApi} from "../api"; +import {API_TYPE_CONSTANTS} from "../constants"; +import {MetaWeblogApi} from "./metaWeblogApi"; + +/** + * Wordpress的API适配器 + */ +export class WordpressApiAdaptor extends MetaWeblogApiAdaptor implements IApi{ + constructor() { + super(); + + this.apiUrl = process.env.WORDPRESS_API_URL || "" + this.username = process.env.WORDPRESS_USERNAME || "" + this.password = process.env.WORDPRESS_PASSWORD || "" + this.appkey = API_TYPE_CONSTANTS.API_TYPE_WORDPRESS + + this.metaWeblog = new MetaWeblogApi(this.appkey, this.apiUrl, this.username, this.password); + } +} \ No newline at end of file diff --git a/lib/metaweblog/metaWeblogApiAdaptor.ts b/lib/metaweblog/metaWeblogApiAdaptor.ts index 2414e27f..69503c65 100644 --- a/lib/metaweblog/metaWeblogApiAdaptor.ts +++ b/lib/metaweblog/metaWeblogApiAdaptor.ts @@ -4,6 +4,7 @@ import {UserBlog} from "../common/userBlog"; import {mdToHtml, mdToPlainText, parseHtml, removeTitleNumber} from "../htmlUtil"; import {CONSTANTS} from "../constants"; import logUtil from "../logUtil"; +import {isEmptyString} from "../util"; /** * 博客园的API适配器 @@ -63,13 +64,15 @@ export class MetaWeblogApiAdaptor implements IApi { const shortDesc = parseHtml(plainText, CONSTANTS.MAX_PREVIEW_LENGTH, true) // logUtil.logInfo("shortDesc=>", shortDesc) + let permalink = blogPost.link || blogPost.permalink || "" + // 适配公共属性 let commonPost = new Post() commonPost.postid = blogPost.postid commonPost.title = blogPost.title commonPost.shortDesc = shortDesc || "" commonPost.mt_keywords = blogPost.mt_keywords - commonPost.permalink = blogPost.permalink + commonPost.permalink = permalink commonPost.description = blogPost.description commonPost.wp_slug = blogPost.wp_slug commonPost.dateCreated = blogPost.dateCreated