Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:#42 支持Wordpress #49

Merged
merged 1 commit into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ CONF_API_URL=
CONF_USERNAME=
CONF_PASSWORD=

# wordpress
WORDPRESS_API_URL=
WORDPRESS_USERNAME=
WORDPRESS_PASSWORD=

# plantuml server
PLANT_UML_SERVR=
4 changes: 4 additions & 0 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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("未找到接口适配器,请检查参数")
}
Expand Down
7 changes: 6 additions & 1 deletion lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const API_TYPE_CONF = "conf"
* Cnblogs
*/
const API_TYPE_CNBLOGS = "cnblogs"
/**
* Wordpress
*/
const API_TYPE_WORDPRESS = "wordpress"

/**
* API类型常量定义
Expand All @@ -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
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/htmlUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions lib/metaweblog/WordpressApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
5 changes: 4 additions & 1 deletion lib/metaweblog/metaWeblogApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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适配器
Expand Down Expand Up @@ -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
Expand Down