diff --git a/src/routes/docs.js b/src/routes/docs.js index 8201d76..58fef96 100644 --- a/src/routes/docs.js +++ b/src/routes/docs.js @@ -1,15 +1,29 @@ const { Router } = require('express'); const router = new Router(); +const util = require('../util'); -const fs = require('fs'); -const path = require('path'); -const { marked } = require('marked'); const errorList = require('../../docs/common/errorList.json'); router.get('/', async (request, response) => { response.redirect('/docs/welcome'); }); +router.get('/welcome', async (request, response) => { + const renderData = { + currentPage: 'welcome', + }; + + response.render('docs/welcome', renderData); +}); + +router.get('/install', async (request, response) => { + const renderData = { + currentPage: 'install', + }; + + response.render('docs/install', renderData); +}); + router.get('/search', async (request, response) => { const renderData = { errorList: JSON.stringify(errorList), @@ -18,53 +32,6 @@ router.get('/search', async (request, response) => { response.render('docs/search', renderData); }); -router.get('/install', async (request, response) => { - const renderData = { - currentPage: 'install', - }; - - response.render('docs/install', renderData); -}); - -function getRawDocs(locale, subpath, pageName) { - - const localePath = path.join(__dirname, '../../docs', locale.replace('-', '_'), subpath, `${pageName}.md`); - const defaultPath = path.join(__dirname, '../../docs', 'en_US', subpath, `${pageName}.md`); - - if (fs.existsSync(localePath)) { - return { - content: parseDocs(fs.readFileSync(localePath, 'utf8')), - MDLocale: locale, - }; - } else if (fs.existsSync(defaultPath)) { - return { - content: parseDocs(fs.readFileSync(defaultPath, 'utf8')), - MDLocale: 'en-US' - }; - } else { - return { - content: null, - MDLocale: null - }; - } -} - -function parseDocs(rawDocs) { - if (rawDocs) { - let markedContent = marked(rawDocs); - markedContent = markedContent.replaceAll(/\[yt-iframe\]\(.{11}\)/g, (match) => { - const videoIDRegex = /(?<=\[yt-iframe\]\().*(?=\))/g; - const videoID = match.match(videoIDRegex)[0]; - return `
`; - }); - - const htmlContent = marked.parse(markedContent); - return htmlContent; - } else { - return null; - } -} - router.get('/:page', async (request, response, next) => { const renderData = {}; @@ -72,7 +39,7 @@ router.get('/:page', async (request, response, next) => { const pageName = request.params.page; renderData.currentPage = pageName; - const { content, MDLocale } = getRawDocs(locale, '', pageName); + const { content, MDLocale } = util.getRawDocs(locale, '', pageName); if (content) { renderData.content = content; } else { @@ -80,27 +47,26 @@ router.get('/:page', async (request, response, next) => { } renderData.missingInLocale = locale !== MDLocale; - // A boolean to show the quick links grid or not. - if (pageName === 'welcome') { - renderData.showQuickLinks = true; - } - response.render('docs/docs', renderData); }); -router.get('/:subpath/:page', async (request, response, next) => { - const renderData = {}; +router.get('/:subpath/:page', async (request, response, next) => { const locale = response.locals.localeString; const pageName = request.params.page; const subpath = request.params.subpath; - renderData.currentPage = `${subpath}/${pageName}`; - const { content, MDLocale } = getRawDocs(locale, subpath, pageName); + const renderData = { + currentPage: `${subpath}/${pageName}` + }; + + const { content, MDLocale } = util.getRawDocs(locale, subpath, pageName); + if (content) { renderData.content = content; } else { return next(); } + renderData.missingInLocale = locale !== MDLocale; response.render('docs/docs', renderData); diff --git a/src/util.js b/src/util.js index 6e01ea2..7e068c9 100644 --- a/src/util.js +++ b/src/util.js @@ -1,12 +1,15 @@ +const path = require('path'); const fs = require('fs-extra'); const got = require('got'); const crypto = require('crypto'); const Stripe = require('stripe'); +const { marked } = require('marked'); const mailer = require('./mailer'); const database = require('./database'); const logger = require('./logger'); const config = require('../config.json'); + const stripe = new Stripe(config.stripe.secret_key); function fullUrl(request) { @@ -25,6 +28,45 @@ function getLocale(region, language) { return require(`${__dirname}/../locales/US_en.json`); } +function getRawDocs(locale, subpath, pageName) { + + const localePath = path.join(__dirname, '../docs', locale.replace('-', '_'), subpath, `${pageName}.md`); + const defaultPath = path.join(__dirname, '../docs', 'en_US', subpath, `${pageName}.md`); + + if (fs.existsSync(localePath)) { + return { + content: parseDocs(fs.readFileSync(localePath, 'utf8')), + MDLocale: locale, + }; + } else if (fs.existsSync(defaultPath)) { + return { + content: parseDocs(fs.readFileSync(defaultPath, 'utf8')), + MDLocale: 'en-US' + }; + } + + return { + content: null, + MDLocale: null + }; +} + +function parseDocs(rawDocs) { + if (!rawDocs) { + return null; + } + + let markedContent = marked(rawDocs); + markedContent = markedContent.replaceAll(/\[yt-iframe\]\(.{11}\)/g, (match) => { + const videoIDRegex = /(?<=\[yt-iframe\]\().*(?=\))/g; + const videoID = match.match(videoIDRegex)[0]; + return `
`; + }); + + const htmlContent = marked.parse(markedContent); + return htmlContent; +} + function apiGetRequest(path, headers) { return got.get(`https://api.pretendo.cc${path}`, { responseType: 'json', @@ -338,6 +380,8 @@ async function handleStripeEvent(event) { module.exports = { fullUrl, getLocale, + getRawDocs, + parseDocs, apiGetRequest, apiPostGetRequest, apiDeleteGetRequest, diff --git a/views/docs/docs.handlebars b/views/docs/docs.handlebars index fd05392..19eed2b 100644 --- a/views/docs/docs.handlebars +++ b/views/docs/docs.handlebars @@ -42,27 +42,6 @@

{{ localeHelper locale "docs" "missingInLocale" }}

{{/if}} - {{#if showQuickLinks}} -

{{ localeHelper locale "docs" "quickLinks" "header" }}

- - {{/if}} - {{{ content }}} diff --git a/views/docs/welcome.handlebars b/views/docs/welcome.handlebars new file mode 100644 index 0000000..cb1583d --- /dev/null +++ b/views/docs/welcome.handlebars @@ -0,0 +1,100 @@ + + +
+ + + + + + + Pretendo + + + + + +
+ + {{> header}} +
+ + {{> docs-sidebar}} + +
+
+

{{ localeHelper locale "docs" "quickLinks" "header" }}

+ + +

Welcome

+

Welcome to the official documentation for Pretendo Network. Here you'll be able to find installation instructions, error + codes, and more.

+

To begin, check out the quick links or take a look at the sidebar

+
+
+
+ + + + + + + \ No newline at end of file