|
| 1 | +import { getPages, getPosts, prevNext } from "../../functions/blog-doc.js" |
| 2 | +import { initializeApp } from "../../functions/initialize.js" |
| 3 | +import { idsInHeadings } from "../../functions/helpers.js" |
| 4 | +import { getSettings } from "../../functions/settings.js" |
| 5 | +import { marked } from "marked" |
| 6 | + |
| 7 | +const { eta } = initializeApp() |
| 8 | + |
| 9 | +// Markdown Route |
| 10 | +export function adminPreviewRoute(app) { |
| 11 | + /** |
| 12 | + * Due to Velocy architecture, a route without a defined start point cannot be reached. |
| 13 | + * This is why two routes are created, one for the pages and the other for posts. |
| 14 | + * In short, the following route "/:folder/:filename" doesn't work in Velocy. |
| 15 | + */ |
| 16 | + app.get("/admin-preview-page/:filename", async (req, res) => { |
| 17 | + const settings = await getSettings() |
| 18 | + |
| 19 | + const pages = await getPages() |
| 20 | + const unpublishedPages = pages.filter((page) => page[1].frontmatter.published == "false") |
| 21 | + const currentFile = unpublishedPages.find((file) => file.path === `pages/${req.params.filename}.md`) |
| 22 | + |
| 23 | + if (currentFile) { |
| 24 | + const fileData = currentFile[1].frontmatter |
| 25 | + fileData.favicon = settings.favicon |
| 26 | + const fileContent = marked.parse(currentFile[1].content) |
| 27 | + const response = eta.render(`themes/${settings.currentTheme}/layouts/base.html`, { |
| 28 | + // Passing Route data |
| 29 | + mdRoute: true, |
| 30 | + // Passing Markdown file data |
| 31 | + data: fileData, |
| 32 | + content: settings.addIdsToHeadings ? idsInHeadings(fileContent) : fileContent, |
| 33 | + // Passing data to edit the file |
| 34 | + editable: true, |
| 35 | + filename: req.params.filename, |
| 36 | + // Passing needed settings for the template |
| 37 | + siteTitle: settings.siteTitle, |
| 38 | + menuLinks: settings.menuLinks, |
| 39 | + footerCopyright: settings.footerCopyright, |
| 40 | + }) |
| 41 | + res.writeHead(200, { "Content-Type": "text/html" }) |
| 42 | + res.end(response) |
| 43 | + } else { |
| 44 | + // Proceed to the 404 route if no file is found |
| 45 | + res.writeHead(302, { Location: "/404" }) |
| 46 | + res.end() |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + app.get("/admin-preview-post/:filename", async (req, res) => { |
| 51 | + const settings = await getSettings() |
| 52 | + |
| 53 | + const posts = await getPosts() |
| 54 | + const unpublishedPosts = posts.filter((post) => post[1].frontmatter.published == "false") |
| 55 | + const currentFile = unpublishedPosts.find((file) => file.path === `posts/${req.params.filename}.md`) |
| 56 | + |
| 57 | + if (currentFile) { |
| 58 | + const fileData = currentFile[1].frontmatter |
| 59 | + fileData.favicon = settings.favicon |
| 60 | + const fileContent = marked.parse(currentFile[1].content) |
| 61 | + const response = eta.render(`themes/${settings.currentTheme}/layouts/base.html`, { |
| 62 | + // Passing Route data |
| 63 | + mdRoute: true, |
| 64 | + // Passing Markdown file data |
| 65 | + data: fileData, |
| 66 | + content: settings.addIdsToHeadings ? idsInHeadings(fileContent) : fileContent, |
| 67 | + prevNext: currentFile.dir === "posts" ? await prevNext(`${req.params.filename}.md`) : null, |
| 68 | + // Passing data to edit the file |
| 69 | + editable: true, |
| 70 | + filename: req.params.filename, |
| 71 | + // Passing needed settings for the template |
| 72 | + siteTitle: settings.siteTitle, |
| 73 | + menuLinks: settings.menuLinks, |
| 74 | + footerCopyright: settings.footerCopyright, |
| 75 | + }) |
| 76 | + res.writeHead(200, { "Content-Type": "text/html" }) |
| 77 | + res.end(response) |
| 78 | + } else { |
| 79 | + // Proceed to the 404 route if no file is found |
| 80 | + res.writeHead(302, { Location: "/404" }) |
| 81 | + res.end() |
| 82 | + } |
| 83 | + }) |
| 84 | +} |
0 commit comments