Skip to content

Commit

Permalink
generate page data for CMS
Browse files Browse the repository at this point in the history
  • Loading branch information
atsuki-t committed Oct 12, 2023
1 parent a61d2a7 commit c8a4fa5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
27 changes: 17 additions & 10 deletions apps/app/src/server/routes/cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ const logger = loggerFactory('growi:routes:cms:pages');
module.exports = function(crowi) {
const Page = crowi.model('Page');

const generatePageDataForCMS = (page) => {
const md = new MarkdownIt({ html: true, linkify: true });
md.use(markdownItMeta).use(markdownItEmoji);
const htmlString = md.render(page.revision.body);
const frontMatter = md.meta.cms;
// frontMatter に title が存在しなければ、path の最後の '/' 以降の文字列をタイトルにする
const title = frontMatter?.title || page.path.match(/([^/]+)$/)[1];

return {
page, title, htmlString, frontMatter,
};
};

const actions: any = {};
const api: any = {};

Expand Down Expand Up @@ -62,14 +75,9 @@ module.exports = function(crowi) {
return timeB - timeA;
});

const pagesWithHTMLString = pagesSortedByPublishedAt.map((page) => {
const md = new MarkdownIt({ html: true, linkify: true });
md.use(markdownItMeta).use(markdownItEmoji);
const pagesDataForCMS = pagesSortedByPublishedAt.map(page => generatePageDataForCMS(page));

return { page, htmlString: md.render(page.revision.body), frontMatter: md.meta.cms };
});

return res.json(ApiResponse.success(pagesWithHTMLString));
return res.json(ApiResponse.success(pagesDataForCMS));
};

api.get = async function(req, res) {
Expand Down Expand Up @@ -106,10 +114,9 @@ module.exports = function(crowi) {
}
}

const md = new MarkdownIt({ html: true, linkify: true });
md.use(markdownItMeta).use(markdownItEmoji);
const pageDataForCMS = generatePageDataForCMS(page);

return res.json(ApiResponse.success({ page, htmlString: md.render(page.revision.body), frontMatter: md.meta.cms }));
return res.json(ApiResponse.success(pageDataForCMS));
};

return actions;
Expand Down
1 change: 1 addition & 0 deletions apps/mobliz-clone/src/pages/[pageId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const DetailPage: NextPage<Props> = (props: Props) => {
useEffect(() => {
axios.get(`${process.env.NEXT_PUBLIC_APP_SITE_URL}/_cms/${pageId}.json`)
.then((response) => {
console.log(response);
setHTMLString(response.data.htmlString);
})
.catch((error) => {
Expand Down

0 comments on commit c8a4fa5

Please sign in to comment.