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:#14 代码高亮 #16

Merged
merged 1 commit into from
Aug 4, 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
26 changes: 26 additions & 0 deletions components/themes/default/css/post.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.postBody {

}

.postBody p code {
border: solid 1px;
border-radius: 4px;
padding: 1px 2px;
margin: 0 2px;
}

.postBody pre {
}

.postBody pre code {
font-family: -apple-system, Menlo, Microsoft Yahei, Consolas, Courier New, monospace, Monaco;
font-size: 14px;
display: block;
overflow-x: auto;
padding: 0.5em;
line-height: 1.6;
color: black;
background-color: #f5f5f5 !important;
border: 1px solid #ccc !important;
border-radius: 3px !important;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@fortawesome/react-fontawesome": "^0.2.0",
"bootstrap": "^5.2.0",
"clsx": "^1.2.1",
"highlight.js": "^11.6.0",
"metaweblog-api": "^1.2.0",
"next": "12.2.3",
"react": "18.2.0",
Expand Down
82 changes: 41 additions & 41 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,16 @@ export default Home

// https://github.com/siyuan-note/siyuan/blob/master/API_zh_CN.md#%E9%89%B4%E6%9D%83
// https://github.com/vercel/next.js/blob/canary/examples/cms-wordpress/pages/index.js
// export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
// const query = context.query || {}
// if (query.t instanceof Array) {
// throw new Error("参数类型错误")
// }
//
// let cfg: SiteConfig = new SiteConfig()
// let result: Array<Post> = []
// const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN
// const pageno = query.p
//
// const api = new API(type)
//
// // 配置
// const cfgs = await api.getUsersBlogs() || []
// if (cfgs.length > 0) {
// cfg.userBlog = cfgs[0]
// }
// // 文章
// if (pageno) {
// let num = 1
// if (typeof pageno === "string") {
// num = parseInt(pageno) || 1
// }
// result = await api.getRecentPosts(10, num - 1)
// } else {
// result = await api.getRecentPosts(10)
// }
//
// return {
// props: {
// type: type,
// layoutCfg: JSON.parse(JSON.stringify(cfg)),
// posts: JSON.parse(JSON.stringify(result))
// }
// }
// }
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const query = context.query || {}
if (query.t instanceof Array) {
throw new Error("参数类型错误")
}

export const getStaticProps: GetStaticProps<Props> = async (context) => {
let cfg: SiteConfig = new SiteConfig()
let result: Array<Post> = []
const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN
const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN
const pageno = query.p

const api = new API(type)

Expand All @@ -75,7 +43,15 @@ export const getStaticProps: GetStaticProps<Props> = async (context) => {
cfg.userBlog = cfgs[0]
}
// 文章
result = await api.getRecentPosts(10)
if (pageno) {
let num = 1
if (typeof pageno === "string") {
num = parseInt(pageno) || 1
}
result = await api.getRecentPosts(10, num - 1)
} else {
result = await api.getRecentPosts(10)
}

return {
props: {
Expand All @@ -84,4 +60,28 @@ export const getStaticProps: GetStaticProps<Props> = async (context) => {
posts: JSON.parse(JSON.stringify(result))
}
}
}
}

// export const getStaticProps: GetStaticProps<Props> = async (context) => {
// let cfg: SiteConfig = new SiteConfig()
// let result: Array<Post> = []
// const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN
//
// const api = new API(type)
//
// // 配置
// const cfgs = await api.getUsersBlogs() || []
// if (cfgs.length > 0) {
// cfg.userBlog = cfgs[0]
// }
// // 文章
// result = await api.getRecentPosts(10)
//
// return {
// props: {
// type: type,
// layoutCfg: JSON.parse(JSON.stringify(cfg)),
// posts: JSON.parse(JSON.stringify(result))
// }
// }
// }
126 changes: 72 additions & 54 deletions pages/post/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {GetStaticProps, NextPage} from "next";
import {GetServerSideProps, GetStaticProps, NextPage} from "next";
import {Post} from "../../lib/common/post";
import {API} from "../../lib/api";
import {API_TYPE_CONSTANTS} from "../../lib/constants";
import styles from "../../components/themes/default/css/layout.module.css";
import postStyles from "../../components/themes/default/css/post.module.css"
import SiteConfig from "../../lib/common/siteconfig";
import DefaultLayout from "../../components/themes/default/defaultLayout";
import {useEffect} from "react";
import hljs from 'highlight.js'
import 'highlight.js/styles/vs.css'

type Props = {
type: string,
Expand All @@ -14,6 +18,20 @@ type Props = {

const PostDetail: NextPage<Props> = (props, context) => {

useEffect(() => {
// 配置 highlight.js
hljs.configure({
// 忽略未经转义的 HTML 字符
ignoreUnescapedHTML: true
})
// 获取到内容中所有的code标签
const codes = document.querySelectorAll('pre code')
codes.forEach((el) => {
// 让code进行高亮
hljs.highlightElement(el as HTMLElement)
})
}, [])

function createMarkup() {
return {__html: props.post?.description};
}
Expand All @@ -22,54 +40,20 @@ const PostDetail: NextPage<Props> = (props, context) => {
<DefaultLayout props={props.propCfg}>
<main className={styles.main}>
<p>{props.post?.mt_keywords}</p>
<div dangerouslySetInnerHTML={createMarkup()}/>
<div className={postStyles.postBody} dangerouslySetInnerHTML={createMarkup()}/>
</main>
</DefaultLayout>
)
}

export default PostDetail

// export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
// const query = context.query || {}
// if (query.t instanceof Array) {
// throw new Error("参数类型错误")
// }
//
// const slug = context?.params?.slug
// if (!slug || typeof slug !== "string") {
// throw new Error("文章路径错误")
// }
// let smartSlug = ""
// if (slug.indexOf(".html") > -1) {
// smartSlug = slug.replace(".html", "")
// }
//
// const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN
//
// const api = new API(type)
//
// let cfg: SiteConfig = new SiteConfig()
// // 配置
// const cfgs = await api.getUsersBlogs() || []
// if (cfgs.length > 0) {
// cfg.userBlog = cfgs[0]
// }
//
// // 文章
// log.logInfo("smartSlug=>", smartSlug)
// const post = await api.getPost(smartSlug)
//
// return {
// props: {
// type: type,
// propCfg: JSON.parse(JSON.stringify(cfg)),
// post: JSON.parse(JSON.stringify(post))
// }
// }
// }
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const query = context.query || {}
if (query.t instanceof Array) {
throw new Error("参数类型错误")
}

export const getStaticProps: GetStaticProps<Props> = async (context) => {
const slug = context?.params?.slug
if (!slug || typeof slug !== "string") {
throw new Error("文章路径错误")
Expand All @@ -79,7 +63,8 @@ export const getStaticProps: GetStaticProps<Props> = async (context) => {
smartSlug = slug.replace(".html", "")
}

const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN
const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN

const api = new API(type)

let cfg: SiteConfig = new SiteConfig()
Expand All @@ -102,15 +87,48 @@ export const getStaticProps: GetStaticProps<Props> = async (context) => {
}
}

export async function getStaticPaths() {
const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN
const api = new API(type)
const result = await api.getRecentPosts(10)

// 静态路径
const fpath = result.map(({postid}) => `/post/${postid}.html`) || [];
return {
paths: fpath,
fallback: true,
}
}
// export const getStaticProps: GetStaticProps<Props> = async (context) => {
// const slug = context?.params?.slug
// if (!slug || typeof slug !== "string") {
// throw new Error("文章路径错误")
// }
// let smartSlug = ""
// if (slug.indexOf(".html") > -1) {
// smartSlug = slug.replace(".html", "")
// }
//
// const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN
// const api = new API(type)
//
// let cfg: SiteConfig = new SiteConfig()
// // 配置
// const cfgs = await api.getUsersBlogs() || []
// if (cfgs.length > 0) {
// cfg.userBlog = cfgs[0]
// }
//
// // 文章
// // log.logInfo("smartSlug=>", smartSlug)
// const post = await api.getPost(smartSlug)
//
// return {
// props: {
// type: type,
// propCfg: JSON.parse(JSON.stringify(cfg)),
// post: JSON.parse(JSON.stringify(post))
// }
// }
// }
//
// export async function getStaticPaths() {
// const type = API_TYPE_CONSTANTS.API_TYPE_SIYUAN
// const api = new API(type)
// const result = await api.getRecentPosts(10)
//
// // 静态路径
// const fpath = result.map(({postid}) => `/post/${postid}.html`) || [];
// return {
// paths: fpath,
// fallback: true,
// }
// }
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,11 @@ has@^1.0.3:
dependencies:
function-bind "^1.1.1"

highlight.js@^11.6.0:
version "11.6.0"
resolved "https://registry.npmmirror.com/highlight.js/-/highlight.js-11.6.0.tgz#a50e9da05763f1bb0c1322c8f4f755242cff3f5a"
integrity sha512-ig1eqDzJaB0pqEvlPVIpSSyMaO92bH1N2rJpLMN/nX396wTpDA4Eq0uK+7I/2XG17pFaaKE0kjV/XPeGt7Evjw==

ignore@^5.2.0:
version "5.2.0"
resolved "https://registry.npmmirror.com/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a"
Expand Down