Skip to content

Commit

Permalink
Merge pull request #9 from weseek/feat/cms
Browse files Browse the repository at this point in the history
support: merge feat/cms into master
  • Loading branch information
yuki-takei authored Oct 11, 2023
2 parents f30663b + 4f043fe commit 59a0768
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 2 deletions.
2 changes: 2 additions & 0 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
"is-absolute-url": "^4.0.1",
"is-iso-date": "^0.0.1",
"lucene-query-parser": "^1.2.0",
"markdown-it": "^13.0.2",
"markdown-it-emoji": "^2.0.2",
"markdown-table": "^1.1.1",
"md5": "^2.2.1",
"mermaid": "^10.1.0",
Expand Down
59 changes: 59 additions & 0 deletions apps/app/src/server/routes/cms.ts
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;
};
2 changes: 2 additions & 0 deletions apps/app/src/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module.exports = function(crowi, app) {
const search = require('./search')(crowi, app);
const hackmd = require('./hackmd')(crowi, app);
const ogp = require('./ogp')(crowi);
const cms = require('./cms')(crowi, app);

const next = nextFactory(crowi);

Expand Down Expand Up @@ -186,6 +187,7 @@ module.exports = function(crowi, app) {

app.use('/ogp', express.Router().get('/:pageId([0-9a-z]{0,})', loginRequired, ogp.pageIdRequired, ogp.ogpValidator, ogp.renderOgp));

app.get('/_cms/:pageId.json', cms.api.get);
app.get('/_cms/tags', tag.api.list);

app.get('/*/$' , loginRequired, next.delegateToNext);
Expand Down
5 changes: 3 additions & 2 deletions apps/mobliz-clone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
},
"dependencies": {
"axios": "^0.24.0",
"html-react-parser": "^4.2.2",
"next": "^13.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"next": "^13.3.0"
"react-dom": "^18.2.0"
},
"devDependencies": {
"bootstrap": "^5.3.2"
Expand Down
60 changes: 60 additions & 0 deletions apps/mobliz-clone/src/pages/[pageId].tsx
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;
98 changes: 98 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6662,11 +6662,41 @@ dom-helpers@^5.2.0:
"@babel/runtime" "^7.8.7"
csstype "^3.0.2"

dom-serializer@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-2.0.0.tgz#e41b802e1eedf9f6cae183ce5e622d789d7d8e53"
integrity sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==
dependencies:
domelementtype "^2.3.0"
domhandler "^5.0.2"
entities "^4.2.0"

domelementtype@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-2.3.0.tgz#5c45e8e869952626331d7aab326d01daf65d589d"
integrity sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==

domhandler@5.0.3, domhandler@^5.0.2, domhandler@^5.0.3:
version "5.0.3"
resolved "https://registry.yarnpkg.com/domhandler/-/domhandler-5.0.3.tgz#cc385f7f751f1d1fc650c21374804254538c7d31"
integrity sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==
dependencies:
domelementtype "^2.3.0"

dompurify@2.4.5:
version "2.4.5"
resolved "https://registry.yarnpkg.com/dompurify/-/dompurify-2.4.5.tgz#0e89a27601f0bad978f9a924e7a05d5d2cccdd87"
integrity sha512-jggCCd+8Iqp4Tsz0nIvpcb22InKEBrGz5dw3EQJMs8HPJDsKbFIO3STYtAvCfDx26Muevn1MHVI0XxjgFfmiSA==

domutils@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/domutils/-/domutils-3.1.0.tgz#c47f551278d3dc4b0b1ab8cbb42d751a6f0d824e"
integrity sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==
dependencies:
dom-serializer "^2.0.0"
domelementtype "^2.3.0"
domhandler "^5.0.3"

dot-case@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/dot-case/-/dot-case-3.0.4.tgz#9b2b670d00a431667a8a75ba29cd1b98809ce751"
Expand Down Expand Up @@ -6895,6 +6925,11 @@ entities@2.2.0:
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==

entities@^4.2.0, entities@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48"
integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==

entities@~3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/entities/-/entities-3.0.1.tgz#2b887ca62585e96db3903482d336c1006c3001d4"
Expand Down Expand Up @@ -8673,6 +8708,14 @@ hpagent@^1.0.0:
resolved "https://registry.yarnpkg.com/hpagent/-/hpagent-1.2.0.tgz#0ae417895430eb3770c03443456b8d90ca464903"
integrity sha512-A91dYTeIB6NoXG+PxTQpCCDDnfHsW9kc06Lvpu1TEe9gnd6ZFeiBoRO9JvzEv6xK7EX97/dUE8g/vBMTqTS3CA==

html-dom-parser@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/html-dom-parser/-/html-dom-parser-4.0.0.tgz#dc382fbbc9306f8c9b5aae4e3f2822e113a48709"
integrity sha512-TUa3wIwi80f5NF8CVWzkopBVqVAtlawUzJoLwVLHns0XSJGynss4jiY0mTWpiDOsuyw+afP+ujjMgRh9CoZcXw==
dependencies:
domhandler "5.0.3"
htmlparser2 "9.0.0"

html-escaper@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/html-escaper/-/html-escaper-2.0.0.tgz#71e87f931de3fe09e56661ab9a29aadec707b491"
Expand All @@ -8685,6 +8728,16 @@ html-parse-stringify@^3.0.1:
dependencies:
void-elements "3.1.0"

html-react-parser@^4.2.2:
version "4.2.2"
resolved "https://registry.yarnpkg.com/html-react-parser/-/html-react-parser-4.2.2.tgz#91f1cc2138bc069d65cbd8b9d97b1e71ed423300"
integrity sha512-lh0wEGISnFZEAmvQqK4xc0duFMUh/m9YYyAhFursWxdtNv+hCZge0kj1y4wep6qPB5Zm33L+2/P6TcGWAJJbjA==
dependencies:
domhandler "5.0.3"
html-dom-parser "4.0.0"
react-property "2.0.0"
style-to-js "1.1.4"

html-tags@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/html-tags/-/html-tags-3.1.0.tgz#7b5e6f7e665e9fb41f30007ed9e0d41e97fb2140"
Expand All @@ -8695,6 +8748,16 @@ html-void-elements@^2.0.0:
resolved "https://registry.yarnpkg.com/html-void-elements/-/html-void-elements-2.0.1.tgz#29459b8b05c200b6c5ee98743c41b979d577549f"
integrity sha512-0quDb7s97CfemeJAnW9wC0hw78MtW7NU3hqtCD75g2vFlDLt36llsYD7uB7SUzojLMP24N5IatXf7ylGXiGG9A==

htmlparser2@9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-9.0.0.tgz#e431142b7eeb1d91672742dea48af8ac7140cddb"
integrity sha512-uxbSI98wmFT/G4P2zXx4OVx04qWUmyFPrD2/CNepa2Zo3GPNaCaaxElDgwUrwYWkK1nr9fft0Ya8dws8coDLLQ==
dependencies:
domelementtype "^2.3.0"
domhandler "^5.0.3"
domutils "^3.1.0"
entities "^4.5.0"

http-errors@1.7.2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.7.2.tgz#4f5029cf13239f31036e5b2e55292bcfbcc85c8f"
Expand Down Expand Up @@ -10561,6 +10624,11 @@ map-values@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/map-values/-/map-values-1.0.1.tgz#768b8e79c009bf2b64fee806e22a7b1c4190c990"

markdown-it-emoji@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/markdown-it-emoji/-/markdown-it-emoji-2.0.2.tgz#cd42421c2fda1537d9cc12b9923f5c8aeb9029c8"
integrity sha512-zLftSaNrKuYl0kR5zm4gxXjHaOI3FAOEaloKmRA5hijmJZvSjmxcokOLlzycb/HXlUFWzXqpIEoyEMCE4i9MvQ==

markdown-it-front-matter@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/markdown-it-front-matter/-/markdown-it-front-matter-0.2.3.tgz#d6fa0f4b362e02086dd4ce8219fadf3f4c9cfa37"
Expand All @@ -10577,6 +10645,17 @@ markdown-it@^13.0.1:
mdurl "^1.0.1"
uc.micro "^1.0.5"

markdown-it@^13.0.2:
version "13.0.2"
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-13.0.2.tgz#1bc22e23379a6952e5d56217fbed881e0c94d536"
integrity sha512-FtwnEuuK+2yVU7goGn/MJ0WBZMM9ZPgU9spqlFs7/A/pDIUNSOQZhUgOqYCficIuR2QaFnrt8LHqBWsbTAoI5w==
dependencies:
argparse "^2.0.1"
entities "~3.0.1"
linkify-it "^4.0.1"
mdurl "^1.0.1"
uc.micro "^1.0.5"

markdown-table@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/markdown-table/-/markdown-table-1.1.1.tgz#4b3dd3a133d1518b8ef0dbc709bf2a1b4824bc8c"
Expand Down Expand Up @@ -13157,6 +13236,11 @@ react-popper@^2.2.5:
react-fast-compare "^3.0.1"
warning "^4.0.2"

react-property@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/react-property/-/react-property-2.0.0.tgz#2156ba9d85fa4741faf1918b38efc1eae3c6a136"
integrity sha512-kzmNjIgU32mO4mmH5+iUyrqlpFQhF8K2k7eZ4fdLSOPFrD1XgEuSBv9LDEgxRXTMBqMd8ppT0x6TIzqE5pdGdw==

react-refresh@^0.14.0:
version "0.14.0"
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.14.0.tgz#4e02825378a5f227079554d4284889354e5f553e"
Expand Down Expand Up @@ -14842,6 +14926,20 @@ style-search@^0.1.0:
resolved "https://registry.yarnpkg.com/style-search/-/style-search-0.1.0.tgz#7958c793e47e32e07d2b5cafe5c0bf8e12e77902"
integrity sha1-eVjHk+R+MuB9K1yv5cC/jhLneQI=

style-to-js@1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/style-to-js/-/style-to-js-1.1.4.tgz#5fa07a181ec3ca354d699edf442549e0ac61ed32"
integrity sha512-zEeU3vy9xL/hdLBFmzqjhm+2vJ1Y35V0ctDeB2sddsvN1856OdMZUCOOfKUn3nOjjEKr6uLhOnY4CrX6gLDRrA==
dependencies:
style-to-object "0.4.2"

style-to-object@0.4.2:
version "0.4.2"
resolved "https://registry.yarnpkg.com/style-to-object/-/style-to-object-0.4.2.tgz#a8247057111dea8bd3b8a1a66d2d0c9cf9218a54"
integrity sha512-1JGpfPB3lo42ZX8cuPrheZbfQ6kqPPnPHlKMyeRYtfKD+0jG+QsXgXN57O/dvJlzlB2elI6dGmrPnl5VPQFPaA==
dependencies:
inline-style-parser "0.1.1"

style-to-object@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/style-to-object/-/style-to-object-0.3.0.tgz#b1b790d205991cc783801967214979ee19a76e46"
Expand Down

0 comments on commit 59a0768

Please sign in to comment.