Skip to content

Commit

Permalink
Merge pull request #52 from terwer/dev
Browse files Browse the repository at this point in the history
feat:#51 支持展示Wordpress分类
  • Loading branch information
terwer authored Sep 4, 2022
2 parents 34b439f + 1849c36 commit 926136e
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 19 deletions.
5 changes: 3 additions & 2 deletions components/themes/default/defaultHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import DefaultNavbar from "./defaultNavbar";
import SiteConfig from "../../../lib/common/siteconfig";
import Image from "next/image";
import headerStyles from "./css/header.module.css"
import {CategoryInfo} from "../../../lib/common/categoryInfo";

const getTitle = (webname: string, webslogen: string) => {
return webname + " - " + webslogen
}

export default function DefaultHeader({props, keyword, type}: { props: SiteConfig, keyword?: string, type: string }) {
export default function DefaultHeader({props, keyword, type, cats}: { props: SiteConfig, keyword?: string, type: string, cats?: CategoryInfo[] }) {
return (
<>
<Head>
Expand All @@ -25,7 +26,7 @@ export default function DefaultHeader({props, keyword, type}: { props: SiteConfi
data-recalc-dims="1"/>
</a>
</div>
<DefaultNavbar props={props} type={type}/>
<DefaultNavbar props={props} type={type} cats={cats}/>
</header>
</>
)
Expand Down
8 changes: 5 additions & 3 deletions components/themes/default/defaultLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DefaultHeader from "./defaultHeader";
import DefaultFooter from "./defaultFooter";
import {Alert, Container, Row} from "react-bootstrap";
import SiteConfig from "../../../lib/common/siteconfig";
import {CategoryInfo} from "../../../lib/common/categoryInfo";

/**
* 默认布局
Expand All @@ -15,13 +16,14 @@ export default function DefaultLayout({
props,
keyword,
children,
type
}: { props: SiteConfig, keyword?: string, children: any, type: string }) {
type,
cats
}: { props: SiteConfig, keyword?: string, children: any, type: string, cats?: CategoryInfo[] }) {
return (
<>
<Container>
<Row>
<DefaultHeader props={props} keyword={keyword} type={type}/>
<DefaultHeader props={props} keyword={keyword} type={type} cats={cats}/>
</Row>
<Row>
<Container>
Expand Down
18 changes: 17 additions & 1 deletion components/themes/default/defaultNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import {useState} from "react";
import Image from "next/image";
import {isEmptyString} from "../../../lib/util";
import {API_TYPE_CONSTANTS} from "../../../lib/constants";
import {CategoryInfo} from "../../../lib/common/categoryInfo";

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

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

Expand All @@ -24,6 +30,16 @@ export default function DefaultNavbar({props, keyword, type}: { props: SiteConfi
<Navbar.Brand href={props.weburl}>
<Image src="/terwer.svg" width="283" height="64" title={props.webname} alt={props.webname}/>
</Navbar.Brand>
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="me-auto">
{
cats && cats.map((item: CategoryInfo) => (
<Nav.Link key={item.categoryId} href={item.htmlUrl}><FontAwesomeIcon
icon={faHome}/>&nbsp;{item.categoryName}</Nav.Link>
))
}
</Nav>
</Navbar.Collapse>
{
false &&
<div>
Expand Down
18 changes: 14 additions & 4 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {CnblogsApiAdaptor} from "./metaweblog/cnblogsApiAdaptor";
import {Post} from "./common/post";
import {UserBlog} from "./common/userBlog";
import {WordpressApiAdaptor} from "./metaweblog/WordpressApiAdaptor";
import {CategoryInfo} from "./common/categoryInfo";

export interface IApi {
/**
Expand All @@ -27,6 +28,11 @@ export interface IApi {
* @param useSlug 是否使用的是别名(可选,部分平台不支持)
*/
getPost(postid: string, useSlug?: boolean): Promise<any>

/**
* 获取分类列表
*/
getCategories(): Promise<CategoryInfo[]>
}

export class API implements IApi {
Expand Down Expand Up @@ -57,15 +63,19 @@ export class API implements IApi {
}

async getRecentPosts(numOfPosts: number, page?: number, keyword?: string): Promise<Array<Post>> {
return this.apiAdaptor.getRecentPosts(numOfPosts, page, keyword);
return await this.apiAdaptor.getRecentPosts(numOfPosts, page, keyword);
}

async getUsersBlogs(): Promise<Array<UserBlog>> {
return this.apiAdaptor.getUsersBlogs();
return await this.apiAdaptor.getUsersBlogs();
}

async getPost(postid: string, useSlug?: boolean): Promise<Post> {
return await this.apiAdaptor.getPost(postid, useSlug);
}

getPost(postid: string, useSlug?: boolean): Promise<Post> {
return this.apiAdaptor.getPost(postid, useSlug);
async getCategories(): Promise<CategoryInfo[]> {
return await this.apiAdaptor.getCategories()
}
}

23 changes: 23 additions & 0 deletions lib/common/categoryInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 通用分类模型定义
*/
export class CategoryInfo {
categoryId: string
parentId: string
description: string
categoryDescription: string
categoryName: string
htmlUrl: string
rssUrl: string


constructor() {
this.categoryId = ""
this.parentId = "0"
this.description = ""
this.categoryDescription = ""
this.categoryName = ""
this.htmlUrl = ""
this.rssUrl = ""
}
}
4 changes: 2 additions & 2 deletions lib/common/post.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {POST_STATUS_CONSTANTS} from "../constants/postStatusConstants";

/**
* 通用文章模型定义
*/
import {POST_STATUS_CONSTANTS} from "../constants/postStatusConstants";

export class Post {
postid: string
title: string
Expand Down
4 changes: 3 additions & 1 deletion lib/constants/metaweblogMethodConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const EDIT_POST = "metaWeblog.editPost"
const DELETE_POST = "blogger.deletePost"
const GET_RECENT_POSTS = "metaWeblog.getRecentPosts"
const GET_POST = "metaWeblog.getPost"
const GET_CATEGORIES = "metaWeblog.getCategories"

export const METAWEBLOG_METHOD_CONSTANTS = {
GET_USERS_BLOGS,
NEW_POST,
EDIT_POST,
DELETE_POST,
GET_RECENT_POSTS,
GET_POST
GET_POST,
GET_CATEGORIES
}
37 changes: 35 additions & 2 deletions lib/metaweblog/metaWeblogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import logUtil from "../logUtil";
import {METAWEBLOG_METHOD_CONSTANTS} from "../constants/metaweblogMethodConstants";
import {POST_STATUS_CONSTANTS} from "../constants/postStatusConstants";
import {inBrowser, isEmptyString} from "../util";
import {render} from "../markdownUtil";
import {CategoryInfo} from "../common/categoryInfo";

export class MetaWeblogApi {
private readonly apiType: string
Expand Down Expand Up @@ -121,7 +121,7 @@ export class MetaWeblogApi {
result.push(post)
}
}

logUtil.logInfo("result=>", result)
return result
}
Expand Down Expand Up @@ -284,4 +284,37 @@ export class MetaWeblogApi {
// wp_password: post.wp_password || ''
// }
}

public async getCategories(blogid: string, username: string, password: string,): Promise<CategoryInfo[]> {
let result = <Array<CategoryInfo>>[]

let ret = await this.xmlrpcClient.methodCallEntry(METAWEBLOG_METHOD_CONSTANTS.GET_CATEGORIES,
[this.apiType, username, password])
logUtil.logInfo("getCategories ret=>", ret)

// 错误处理
this.doFault(ret)

let catArray = ret.params.param.value.array.data.value || []
catArray.forEach((item: any) => {
const cat = this.parseCat(item)
result.push(cat)
})

return result
}

private parseCat(catStruct: any): CategoryInfo {
const cat = new CategoryInfo()
const catObj = catStruct.struct.member
cat.categoryId = this.parseFieldValue(catObj, "categoryId")
cat.parentId = this.parseFieldValue(catObj, "parentId")
cat.description = this.parseFieldValue(catObj, "description")
cat.categoryDescription = this.parseFieldValue(catObj, "categoryDescription")
cat.categoryName = this.parseFieldValue(catObj, "categoryName")
cat.htmlUrl = this.parseFieldValue(catObj, "htmlUrl")
cat.rssUrl = this.parseFieldValue(catObj, "rssUrl")

return cat
}
}
13 changes: 13 additions & 0 deletions lib/metaweblog/metaWeblogApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {mdToHtml, mdToPlainText, parseHtml, removeTitleNumber} from "../htmlUtil
import {CONSTANTS} from "../constants";
import logUtil from "../logUtil";
import {isEmptyString} from "../util";
import {CategoryInfo} from "../common/categoryInfo";

/**
* 博客园的API适配器
Expand Down Expand Up @@ -116,4 +117,16 @@ export class MetaWeblogApiAdaptor implements IApi {

return commonPost;
}

/**
* getCategories
* https://codex.wordpress.org/XML-RPC_MetaWeblog_API#metaWeblog.getCategories
*
* @returns {Promise<CategoryInfo[]>}
*/
public async getCategories(): Promise<CategoryInfo[]> {
const cats = await this.metaWeblog.getCategories(this.appkey, this.username, this.password)
logUtil.logInfo("获取分类列表=>", cats)
return cats
}
}
5 changes: 5 additions & 0 deletions lib/siyuan/siYuanApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {API_TYPE_CONSTANTS} from "../constants";
import logUtil from "../logUtil";
import {render} from "../markdownUtil";
import {mdToHtml, removeTitleNumber, removeWidgetTag} from "../htmlUtil";
import {CategoryInfo} from "../common/categoryInfo";

/**
* 思源笔记API适配器
Expand Down Expand Up @@ -117,4 +118,8 @@ export class SiYuanApiAdaptor implements IApi {

return commonPost
}

public async getCategories(): Promise<CategoryInfo[]> {
return Promise.resolve([])
}
}
13 changes: 9 additions & 4 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import DefaultLayout from "../components/themes/default/defaultLayout";
import SiteConfig from "../lib/common/siteconfig";
import DefaultHomePostList from "../components/themes/default/defaultHomePostList";
import {assginPreviewUrlForPosts, getHomelink, isEmptyString} from "../lib/util";
import {CategoryInfo} from "../lib/common/categoryInfo";

type Props = {
type: string,
layoutCfg: SiteConfig,
posts: Post[]
posts: Post[],
cats: CategoryInfo[]
}

const Home: NextPage<Props> = (props, context) => {
return (
<DefaultLayout props={props.layoutCfg} type={props.type}>
<DefaultLayout props={props.layoutCfg} type={props.type} cats={props.cats}>
<DefaultHomePostList posts={props.posts} type={props.type}/>
</DefaultLayout>
)
Expand Down Expand Up @@ -72,14 +74,17 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
} else {
result = await api.getRecentPosts(10)
}

assginPreviewUrlForPosts(type, result)

// 分类
const cats = await api.getCategories()

return {
props: {
type: type,
layoutCfg: JSON.parse(JSON.stringify(cfg)),
posts: JSON.parse(JSON.stringify(result))
posts: JSON.parse(JSON.stringify(result)),
cats: JSON.parse(JSON.stringify(cats))
}
}
}
Expand Down

1 comment on commit 926136e

@vercel
Copy link

@vercel vercel bot commented on 926136e Sep 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.