Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get nonlanguage [view] templates #998

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions mod/utils/languageTemplates.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
const getFrom = require('../provider/getFrom')

const getTemplate = require('../workspace/getTemplate')

const workspaceCache = require('../workspace/cache')

module.exports = async (params) => {

if (params.template === undefined) return;
if (params.template === undefined) return;

// Set english as default template language.
params.language ??= 'en'

const workspace = await workspaceCache()

if (!Object.hasOwn(workspace.templates, params.template)) {

// Set english as default template language.
params.language ??= 'en'
console.warn(`Template ${params.template} not found.`)
return params.template;
}

const workspace = await workspaceCache()
// NOT a language template
if (workspace.templates[params.template].src) {

if (!Object.hasOwn(workspace.templates, params.template)) {
const nonLanguage = await getTemplate(workspace.templates[params.template])

console.warn(`Template ${params.template} not found.`)
return params.template;
}
return nonLanguage.template
}

const allLanguages = workspace.templates[params.template]
const allLanguages = workspace.templates[params.template]

let template = Object.hasOwn(allLanguages, params.language)? allLanguages[params.language]: allLanguages.en;
let template = Object.hasOwn(allLanguages, params.language)
? allLanguages[params.language]
: allLanguages.en;

if (typeof template === 'string' && Object.hasOwn(getFrom, template.split(':')[0])) {
if (typeof template === 'string' && Object.hasOwn(getFrom, template.split(':')[0])) {

// Get template from method.
template = await getFrom[template.split(':')[0]](template)
}
// Get template from method.
template = await getFrom[template.split(':')[0]](template)
}

return template
return template
}
5 changes: 5 additions & 0 deletions mod/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ module.exports = async (req, res) => {

const template = await languageTemplates(params)

if (!template) {
res.status(400).send(`Template undefined`)
return;
}

const view = template.replace(/{{2}([A-Za-z][A-Za-z0-9]*)}{2}/g, matched => {

// regex matches {{ or }}
Expand Down