-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from weseek/feat/cms
support: merge feat/cms into master
- Loading branch information
Showing
6 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import md from 'markdown-it'; | ||
import emoji from 'markdown-it-emoji'; | ||
|
||
import loggerFactory from '~/utils/logger'; | ||
|
||
const logger = loggerFactory('growi:routes:cms:pages'); | ||
|
||
module.exports = function(crowi) { | ||
const Page = crowi.model('Page'); | ||
|
||
const actions: any = {}; | ||
const api: any = {}; | ||
|
||
actions.api = api; | ||
|
||
/** | ||
* @api {get} /pages.get get pages | ||
* @apiName get | ||
* @apiGroup Page | ||
* | ||
* @apiParam {String} id | ||
*/ | ||
api.get = async function(req, res) { | ||
const pageId = req.params.pageId; | ||
|
||
let page; | ||
|
||
try { | ||
page = await Page.findByIdAndViewer(pageId); | ||
} | ||
catch (err) { | ||
logger.error('get-page-failed', err); | ||
return res.apiv3Err(err, 500); | ||
} | ||
|
||
if (page == null) { | ||
return res.apiv3Err('Page is not found', 404); | ||
} | ||
|
||
if (page != null) { | ||
try { | ||
page.initLatestRevisionField(undefined); | ||
|
||
// populate | ||
page = await page.populateDataToShowRevision(); | ||
} | ||
catch (err) { | ||
logger.error('populate-page-failed', err); | ||
return res.apiv3Err(err, 500); | ||
} | ||
} | ||
|
||
const htmlString = md({ html: true }).use(emoji).render(page.revision.body); | ||
|
||
return res.apiv3({ ...page, htmlString }); | ||
}; | ||
|
||
return actions; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
|
||
import parse from 'html-react-parser'; | ||
import { NextPage, GetServerSideProps } from 'next'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import axios from '~/utils/axios'; | ||
|
||
type Props = { | ||
pageId: string | ||
} | ||
|
||
const DetailPage: NextPage<Props> = (props: Props) => { | ||
const router = useRouter(); | ||
const pageId = router.query.pageId ?? props.pageId; | ||
const [htmlString, setHTMLString] = useState(); | ||
const [error, setError] = useState<string>(); | ||
|
||
useEffect(() => { | ||
axios.get(`http://localhost:3000/_cms/${pageId}.json`) | ||
.then((response) => { | ||
setHTMLString(response.data.htmlString); | ||
}) | ||
.catch((error) => { | ||
setError(`データの取得に失敗しました。\n${JSON.stringify(error)}`); | ||
}); | ||
}, [pageId]); | ||
|
||
return ( | ||
<div className="border bg-white p-5"> | ||
{error == null ? ( | ||
<> | ||
{htmlString == null ? ( | ||
<div className="spinner-border" role="status"> | ||
<span className="visually-hidden">Loading...</span> | ||
</div> | ||
) : ( | ||
<>{parse(htmlString)}</> | ||
)} | ||
</> | ||
) : ( | ||
<p>{error}</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export const getServerSideProps: GetServerSideProps = async(context) => { | ||
const pageId = context.query.pageId; | ||
|
||
if (typeof pageId !== 'string') { | ||
return { | ||
notFound: true, | ||
}; | ||
} | ||
|
||
return { props: { pageId } }; | ||
}; | ||
|
||
export default DetailPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters