fix: missing figcaption close #46

This commit is contained in:
liqingwei 2021-05-09 08:56:51 +08:00
parent c733c45311
commit ea8207dd8f
8 changed files with 58 additions and 15 deletions

View File

@ -2,20 +2,19 @@ import NoteState from 'libs/web/state/note'
import { removeMarkdown } from 'libs/web/utils/markdown' import { removeMarkdown } from 'libs/web/utils/markdown'
import { FC, useMemo } from 'react' import { FC, useMemo } from 'react'
import { NextSeo } from 'next-seo' import { NextSeo } from 'next-seo'
import { renderMarkdown } from 'libs/web/render-markdown'
// TODO: Maybe can custom // TODO: Maybe can custom
import 'highlight.js/styles/zenburn.css' import 'highlight.js/styles/zenburn.css'
import { PageMode } from 'libs/shared/page' import { PageMode } from 'libs/shared/page'
import Error from 'next/error' import Error from 'next/error'
import useI18n from 'libs/web/hooks/use-i18n' import useI18n from 'libs/web/hooks/use-i18n'
export const PostContainer: FC<{ baseURL: string; pageMode: PageMode }> = ({ export const PostContainer: FC<{
baseURL, baseURL: string
pageMode, pageMode: PageMode
}) => { post?: string
}> = ({ baseURL, pageMode, post = '' }) => {
const { t } = useI18n() const { t } = useI18n()
const { note } = NoteState.useContainer() const { note } = NoteState.useContainer()
const content = useMemo(() => renderMarkdown(note?.content ?? ''), [note])
const description = useMemo( const description = useMemo(
() => removeMarkdown(note?.content).slice(0, 100), () => removeMarkdown(note?.content).slice(0, 100),
[note] [note]
@ -47,11 +46,11 @@ export const PostContainer: FC<{ baseURL: string; pageMode: PageMode }> = ({
</header> </header>
<main <main
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: content, __html: post,
}} }}
></main> ></main>
<style jsx global>{` <style jsx>{`
[title='left-50'] { .prose :glboal([title='left-50']) {
float: left; float: left;
width: 50%; width: 50%;
margin-right: 2em; margin-right: 2em;
@ -59,13 +58,17 @@ export const PostContainer: FC<{ baseURL: string; pageMode: PageMode }> = ({
clear: initial; clear: initial;
} }
[title='right-50'] { .prose :glboal([title='right-50']) {
float: right; float: right;
width: 50%; width: 50%;
margin-left: 2em; margin-left: 2em;
margin-bottom: 1em; margin-bottom: 1em;
clear: initial; clear: initial;
} }
.prose :glboal(figcaption) {
text-align: center;
}
`}</style> `}</style>
</article> </article>
) )

View File

@ -35,6 +35,7 @@ export interface ServerProps {
tree?: TreeModel tree?: TreeModel
ua?: UserAgentType ua?: UserAgentType
disablePassword: boolean disablePassword: boolean
post: string
} }
export type ApiRequest = NextApiRequest & { export type ApiRequest = NextApiRequest & {

View File

@ -0,0 +1,19 @@
import { SSRMiddeware } from '../connect'
import { renderMarkdown } from '../render-markdown'
export const applyPost: SSRMiddeware = async (req, _res, next) => {
req.props = {
...req.props,
post: renderMarkdown(req.props.note?.content ?? ''),
}
next()
}
export const applyPostWithAuth: SSRMiddeware = async (req, res, next) => {
if (req.props.isLoggedIn) {
return next()
}
return applyPost(req, res, next)
}

View File

@ -8,8 +8,10 @@ import noticesPlugin from 'rich-markdown-editor/dist/lib/markdown/notices'
import underlinesPlugin from 'rich-markdown-editor/dist/lib/markdown/underlines' import underlinesPlugin from 'rich-markdown-editor/dist/lib/markdown/underlines'
import hljs from 'highlight.js' import hljs from 'highlight.js'
export function renderMarkdown(markdown: string) { export function renderMarkdown(src: string) {
return markdownit('default', { src = src.replace(/\\\n/g, '\n')
const html = markdownit('default', {
breaks: false, breaks: false,
html: false, html: false,
linkify: true, linkify: true,
@ -33,6 +35,12 @@ export function renderMarkdown(markdown: string) {
.use(underlinesPlugin) .use(underlinesPlugin)
.use(tablesPlugin) .use(tablesPlugin)
.use(noticesPlugin) .use(noticesPlugin)
.render(markdown) .use(require('markdown-it-implicit-figures'), {
figcaption: true,
dataType: true,
})
.render(src)
.trim() .trim()
return html
} }

View File

@ -42,6 +42,7 @@
"lodash": "^4.17.21", "lodash": "^4.17.21",
"lzutf8": "^0.6.0", "lzutf8": "^0.6.0",
"markdown-it": "^12.0.6", "markdown-it": "^12.0.6",
"markdown-it-implicit-figures": "^0.10.0",
"md5": "^2.3.0", "md5": "^2.3.0",
"minio": "^7.0.18", "minio": "^7.0.18",
"nanoid": "^3.1.22", "nanoid": "^3.1.22",

View File

@ -10,6 +10,7 @@ import { PostContainer } from 'components/container/post-container'
import { applyCsrf } from 'libs/server/middlewares/csrf' import { applyCsrf } from 'libs/server/middlewares/csrf'
import { ssr, SSRContext, ServerProps } from 'libs/server/connect' import { ssr, SSRContext, ServerProps } from 'libs/server/connect'
import { applyUA } from 'libs/server/middlewares/ua' import { applyUA } from 'libs/server/middlewares/ua'
import { applyPostWithAuth } from 'libs/server/middlewares/post'
export default function EditNotePage({ export default function EditNotePage({
tree, tree,
@ -17,6 +18,7 @@ export default function EditNotePage({
pageMode, pageMode,
baseURL, baseURL,
isLoggedIn, isLoggedIn,
post,
}: ServerProps) { }: ServerProps) {
if (isLoggedIn) { if (isLoggedIn) {
return ( return (
@ -28,7 +30,7 @@ export default function EditNotePage({
return ( return (
<LayoutPublic tree={tree} note={note}> <LayoutPublic tree={tree} note={note}>
<PostContainer pageMode={pageMode} baseURL={baseURL} /> <PostContainer post={post} pageMode={pageMode} baseURL={baseURL} />
</LayoutPublic> </LayoutPublic>
) )
} }
@ -53,6 +55,7 @@ export const getServerSideProps = async (
.use(applySettings) .use(applySettings)
.use(applyCsrf) .use(applyCsrf)
.use(applyUA) .use(applyUA)
.use(applyPostWithAuth)
.run(ctx.req, ctx.res) .run(ctx.req, ctx.res)
return { return {

View File

@ -6,16 +6,18 @@ import LayoutPublic from 'components/layout/layout-public'
import { PostContainer } from 'components/container/post-container' import { PostContainer } from 'components/container/post-container'
import { ServerProps, ssr, SSRContext } from 'libs/server/connect' import { ServerProps, ssr, SSRContext } from 'libs/server/connect'
import { useSession } from 'libs/server/middlewares/session' import { useSession } from 'libs/server/middlewares/session'
import { applyPost } from 'libs/server/middlewares/post'
export default function SharePage({ export default function SharePage({
tree, tree,
note, note,
pageMode, pageMode,
baseURL, baseURL,
post,
}: ServerProps) { }: ServerProps) {
return ( return (
<LayoutPublic tree={tree} note={note}> <LayoutPublic tree={tree} note={note}>
<PostContainer pageMode={pageMode} baseURL={baseURL} /> <PostContainer post={post} pageMode={pageMode} baseURL={baseURL} />
</LayoutPublic> </LayoutPublic>
) )
} }
@ -33,6 +35,7 @@ export const getServerSideProps = async (
.use(applyTree) .use(applyTree)
.use(applySettings) .use(applySettings)
.use(applyUA) .use(applyUA)
.use(applyPost)
.run(ctx.req, ctx.res) .run(ctx.req, ctx.res)
return { return {

View File

@ -4765,6 +4765,11 @@ markdown-it-container@^3.0.0:
resolved "https://registry.yarnpkg.com/markdown-it-container/-/markdown-it-container-3.0.0.tgz#1d19b06040a020f9a827577bb7dbf67aa5de9a5b" resolved "https://registry.yarnpkg.com/markdown-it-container/-/markdown-it-container-3.0.0.tgz#1d19b06040a020f9a827577bb7dbf67aa5de9a5b"
integrity sha512-y6oKTq4BB9OQuY/KLfk/O3ysFhB3IMYoIWhGJEidXt1NQFocFK2sA2t0NYZAMyMShAGL6x5OPIbrmXPIqaN9rw== integrity sha512-y6oKTq4BB9OQuY/KLfk/O3ysFhB3IMYoIWhGJEidXt1NQFocFK2sA2t0NYZAMyMShAGL6x5OPIbrmXPIqaN9rw==
markdown-it-implicit-figures@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/markdown-it-implicit-figures/-/markdown-it-implicit-figures-0.10.0.tgz#0e75e80b846da1e3117a3fc6d6b013d0cc3f2a8c"
integrity sha512-1TWr6+apyoJvRa4Z7eIolZdeajZCRBcc1ckVXon7XwdL8MfydIWsHnZOS5zRrpUNX5b0/O9giWcmuItSkleK5A==
markdown-it@^10.0.0: markdown-it@^10.0.0:
version "10.0.0" version "10.0.0"
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-10.0.0.tgz#abfc64f141b1722d663402044e43927f1f50a8dc" resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-10.0.0.tgz#abfc64f141b1722d663402044e43927f1f50a8dc"