Skip to content

Commit

Permalink
feat:#27 适配jvue、cnblogs文章展示
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 23, 2022
1 parent 375cba5 commit 4cfe257
Show file tree
Hide file tree
Showing 26 changed files with 732 additions and 128 deletions.
2 changes: 1 addition & 1 deletion components/themes/default/css/post.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
}

.postPublish{

margin-bottom: 10px;
}

.postPublish a{
Expand Down
4 changes: 2 additions & 2 deletions components/themes/default/defaultHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SiteConfig from "../../../lib/common/siteconfig";
import Image from "next/image";
import headerStyles from "./css/header.module.css"

export default function DefaultHeader({props,keyword}: { props: SiteConfig,keyword?:string }) {
export default function DefaultHeader({props, keyword, type}: { props: SiteConfig, keyword?: string, type: string }) {
return (
<>
<Head>
Expand All @@ -21,7 +21,7 @@ export default function DefaultHeader({props,keyword}: { props: SiteConfig,keywo
data-recalc-dims="1"/>
</a>
</div>
<DefaultNavbar props={props}/>
<DefaultNavbar props={props} type={type}/>
</header>
</>
)
Expand Down
25 changes: 20 additions & 5 deletions components/themes/default/defaultHomePostList.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import {Post} from "../../../lib/common/post";
import {Card, ListGroup} from "react-bootstrap";
import {getQueryString, isEmptyString} from "../../../lib/util";
import {API_TYPE_CONSTANTS} from "../../../lib/constants";

export default function DefaultHomePostList({posts}: { posts: Post[] }) {
const getPermalink = function (postid: string, type: string) {
let postUrl = "/post/" + postid + ".html"
if (!isEmptyString(type) && type != API_TYPE_CONSTANTS.API_TYPE_SIYUAN) {
postUrl = "/post/" + postid + ".html?t=" + type
}
return postUrl
}

export default function DefaultHomePostList({posts, type}: { posts: Post[], type: string }) {
return (
<Card>
<Card.Header>最近更新</Card.Header>
<ListGroup variant="flush">
{posts.map((post) => (
<ListGroup.Item key={post.postid}>
<a href={post.permalink} rel="noreferrer">{post.title}</a>
{posts.length > 0 ?
posts.map((post) => (
<ListGroup.Item key={post.postid}>
<a href={getPermalink(post.postid, type)} rel="noreferrer">{post.title}</a>
</ListGroup.Item>
)) :
<ListGroup.Item key="norecord">
~未查询到内容~
</ListGroup.Item>
))}
}
</ListGroup>
</Card>
)
Expand Down
9 changes: 7 additions & 2 deletions components/themes/default/defaultLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import SiteConfig from "../../../lib/common/siteconfig";
* @param children
* @constructor
*/
export default function DefaultLayout({props,keyword,children}: { props:SiteConfig,keyword?:string,children: any }) {
export default function DefaultLayout({
props,
keyword,
children,
type
}: { props: SiteConfig, keyword?: string, children: any, type: string }) {
return (
<>
<Container>
<Row>
<DefaultHeader props={props} keyword={keyword}/>
<DefaultHeader props={props} keyword={keyword} type={type}/>
</Row>
<Row>
<Container>
Expand Down
14 changes: 12 additions & 2 deletions components/themes/default/defaultNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {faBook, faDownload, faFile, faFileText, faHome, faPieChart} from '@forta
import SiteConfig from "../../../lib/common/siteconfig";
import {useState} from "react";
import Image from "next/image";
import {isEmptyString} from "../../../lib/util";
import {API_TYPE_CONSTANTS} from "../../../lib/constants";

export default function DefaultNavbar({props, keyword}: { props: SiteConfig, keyword?: string }) {
export default function DefaultNavbar({props, keyword, type}: { props: SiteConfig, keyword?: string, type: string }) {

let [value, setValue] = useState("")

Expand All @@ -16,10 +18,18 @@ export default function DefaultNavbar({props, keyword}: { props: SiteConfig, key
window.location.href = "/s/" + value
}

const getHomelink = function (type: string) {
let homeLink = "/"
if (!isEmptyString(type) && type != API_TYPE_CONSTANTS.API_TYPE_SIYUAN) {
homeLink = "/?t=" + type
}
return homeLink
}

return (
<Navbar bg="light" expand="lg">
<Container>
<Navbar.Brand href="/">
<Navbar.Brand href={getHomelink(type)}>
<Image src="/terwer.svg" width="283" height="64" title={props.webname} alt={props.webname}/>
</Navbar.Brand>
{
Expand Down
8 changes: 6 additions & 2 deletions lib/common/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* 通用文章模型定义
*/
import {POST_STATUS_CONSTANTS} from "../constants/postStatusConstants";

export class Post {
postid: string
title: string
Expand All @@ -17,8 +19,9 @@ export class Post {
dateCreated: Date
categories: Array<string>
mt_text_more?: string
post_status?:string
isPublished: boolean
postPassword: string
wp_password: string

constructor() {
this.postid = ""
Expand All @@ -30,6 +33,7 @@ export class Post {
this.dateCreated = new Date()
this.categories = []
this.isPublished = true
this.postPassword = ""
this.post_status = POST_STATUS_CONSTANTS.POST_STATUS_PUBLISH
this.wp_password = ""
}
}
15 changes: 15 additions & 0 deletions lib/constants/metaweblogMethodConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const GET_USERS_BLOGS = "blogger.getUsersBlogs"
const NEW_POST = "metaWeblog.newPost"
const EDIT_POST = "metaWeblog.editPost"
const DELETE_POST = "blogger.deletePost"
const GET_RECENT_POSTS = "metaWeblog.getRecentPosts"
const GET_POST = "metaWeblog.getPost"

export const METAWEBLOG_METHOD_CONSTANTS = {
GET_USERS_BLOGS,
NEW_POST,
EDIT_POST,
DELETE_POST,
GET_RECENT_POSTS,
GET_POST
}
9 changes: 9 additions & 0 deletions lib/constants/postStatusConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const POST_STATUS_PUBLISH = "publish"
const POST_TYPE_DRAFT = "draft"
const POST_TYPE_INHERIT = "inherit"

export const POST_STATUS_CONSTANTS = {
POST_STATUS_PUBLISH,
POST_TYPE_DRAFT,
POST_TYPE_INHERIT
}
28 changes: 24 additions & 4 deletions lib/htmlUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,44 @@

import {render} from "./markdownUtil";

/**
* 移除标题数字
* @param str
*/
export function removeTitleNumber(str: string) {
let newstr = str

// 移除序号
const publisherRegex = /([0-9]*)\./g;
newstr = newstr.replace(publisherRegex, "")

return newstr
}

/**
* 删除挂件的HTML
* @param str 原字符
* @returns {*|string} 删除后的字符
*/
export function removeWidgetTag(str: string) {
let newstr = str

// 旧版发布挂件
const publisherRegex = /<iframe.*src="\/widgets\/publisher.*<\/iframe>/g;
str = str.replaceAll(publisherRegex, "")
newstr = newstr.replace(publisherRegex, "")

// 新版发布挂件
const syPublisherRegex = /<iframe.*src="\/widgets\/sy-post-publisher.*<\/iframe>/g;
str = str.replaceAll(syPublisherRegex, "")
newstr = newstr.replace(syPublisherRegex, "")

// 文章属性挂件
const noteAttrRegex = /<iframe.*\/widgets\/Note*\sAttrs.*\/iframe>/g
str = str.replaceAll(noteAttrRegex, "")
return str
newstr = newstr.replace(noteAttrRegex, "")

const h1Regex = /<h1.*\/h1>/g
newstr = newstr.replace(h1Regex, "")

return newstr
}

/**
Expand Down
13 changes: 8 additions & 5 deletions lib/logUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const LOG_ERROR_ENABLED = true
const logInfo = (msg: any, param?: any) => {
if (LOG_INFO_ENABLED) {
if (param) {
console.log(msg, param)
console.log(msg)
console.log(param)
} else {
console.log(msg)
}
Expand All @@ -27,7 +28,8 @@ const logInfo = (msg: any, param?: any) => {
const logWarn = (msg: any, param?: any) => {
if (LOG_WARN_ENABLED) {
if (param) {
console.warn(msg, param)
console.warn(msg)
console.warn(param)
} else {
console.warn(msg)
}
Expand All @@ -41,7 +43,8 @@ const logWarn = (msg: any, param?: any) => {
const logError = (msg: any, param?: any) => {
if (LOG_ERROR_ENABLED) {
if (param) {
console.error(msg, param)
console.error(msg)
console.error(param)
} else {
console.error(msg)
}
Expand All @@ -51,10 +54,10 @@ const logError = (msg: any, param?: any) => {
/**
* 日志记录
*/
const log = {
const logUtil = {
logInfo,
logWarn,
logError
}

export default log
export default logUtil
2 changes: 1 addition & 1 deletion lib/metaweblog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```json
{
"dependencies": {
"metaweblog-api": "^1.2.0"
"xmlrpc": "^1.3.2"
}
}
```
6 changes: 4 additions & 2 deletions lib/metaweblog/cnblogsApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IApi} from "../api";
import {API_TYPE_CONSTANTS} from "../constants";
import {MetaWeblogApiAdaptor} from "./metaWeblogApiAdaptor";
import MetaWeblog from "metaweblog-api";
import {MetaWeblogApi} from "./metaWeblogApi";

/**
* 博客园的API适配器
Expand All @@ -10,9 +10,11 @@ export class CnblogsApiAdaptor extends MetaWeblogApiAdaptor implements IApi {
constructor() {
super();

this.metaWeblog = new MetaWeblog(process.env.CNBLOGS_API_URL || "");
this.apiUrl = process.env.CNBLOGS_API_URL || ""
this.username = process.env.CNBLOGS_USERNAME || ""
this.password = process.env.CNBLOGS_PASSWORD || ""
this.appkey = API_TYPE_CONSTANTS.API_TYPE_CNBLOGS

this.metaWeblog = new MetaWeblogApi(this.appkey, this.apiUrl, this.username, this.password);
}
}
6 changes: 4 additions & 2 deletions lib/metaweblog/confApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IApi} from "../api";
import {MetaWeblogApiAdaptor} from "./metaWeblogApiAdaptor";
import {API_TYPE_CONSTANTS} from "../constants";
import MetaWeblog from "metaweblog-api";
import {MetaWeblogApi} from "./metaWeblogApi";

/**
* Confluence的API适配器
Expand All @@ -11,9 +11,11 @@ export class ConfApiAdaptor extends MetaWeblogApiAdaptor implements IApi {
constructor() {
super();

this.metaWeblog = new MetaWeblog(process.env.CONF_API_URL || "");
this.apiUrl = process.env.CONF_API_URL || ""
this.username = process.env.CONF_USERNAME || ""
this.password = process.env.CONF_PASSWORD || ""
this.appkey = API_TYPE_CONSTANTS.API_TYPE_CONF

this.metaWeblog = new MetaWeblogApi(this.appkey, this.apiUrl, this.username, this.password);
}
}
45 changes: 45 additions & 0 deletions lib/metaweblog/customXmlrpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import logUtil from "../logUtil";

const Serializer = require('xmlrpc/lib/serializer')
// const xmlParser = require('xml2json');
const {XMLParser} = require('fast-xml-parser');
const options = {
ignoreAttributes : false
};
const xmlParser = new XMLParser(options);

/**
* 自定义xmlrpc的请求与解析,解决apache xmlrpc的扩展问题
* @param apiUrl
* @param reqMethod
* @param reqParams
*/
export async function fetchCustom(apiUrl: string, reqMethod: string, reqParams: Array<string>) {
let ret

try {
const methodBodyXml = Serializer.serializeMethodCall(reqMethod, reqParams, "utf8")

const response = await fetch(apiUrl, {
method: "POST",
headers: {
"content-type": "text/xml"
},
body: methodBodyXml
})
const resXml = await response.text()
logUtil.logInfo("resXml=>", resXml)

const parseResult: any = xmlParser.parse(resXml)
logUtil.logInfo("parseResult=>", parseResult)

const resJson = parseResult.methodResponse || {}
logUtil.logInfo("resJson=>", JSON.stringify(resJson))

return resJson
} catch (e: any) {
throw new Error(e)
}

return ret
}
6 changes: 4 additions & 2 deletions lib/metaweblog/jvueApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IApi} from "../api";
import {MetaWeblogApiAdaptor} from "./metaWeblogApiAdaptor";
import MetaWeblog from "metaweblog-api";
import {API_TYPE_CONSTANTS} from "../constants";
import {MetaWeblogApi} from "./metaWeblogApi";

/**
* JVue的API适配器
Expand All @@ -10,9 +10,11 @@ export class JvueApiAdaptor extends MetaWeblogApiAdaptor implements IApi {
constructor() {
super();

this.metaWeblog = new MetaWeblog(process.env.JVUE_API_URL || "");
this.apiUrl = process.env.JVUE_API_URL || ""
this.username = process.env.JVUE_USERNAME || ""
this.password = process.env.JVUE_PASSWORD || ""
this.appkey = API_TYPE_CONSTANTS.API_TYPE_JVUE

this.metaWeblog = new MetaWeblogApi(this.appkey, this.apiUrl, this.username, this.password);
}
}
Loading

0 comments on commit 4cfe257

Please sign in to comment.