feat: page css + scripts

This commit is contained in:
NGPixel
2020-06-20 16:39:36 -04:00
parent 53ddb50b51
commit 83b83a7510
6 changed files with 55 additions and 16 deletions

View File

@@ -2,6 +2,7 @@ const express = require('express')
const router = express.Router()
const pageHelper = require('../helpers/page')
const _ = require('lodash')
const CleanCSS = require('clean-css')
/* global WIKI */
@@ -153,6 +154,11 @@ router.get(['/e', '/e/*'], async (req, res, next) => {
await page.$relatedQuery('tags')
page.tags = _.map(page.tags, 'tag')
// -> Beautify Script CSS
if (!_.isEmpty(page.extra.css)) {
page.extra.css = new CleanCSS({ format: 'beautify' }).minify(page.extra.css).styles
}
_.set(res.locals, 'pageMeta.title', `Edit ${page.title}`)
_.set(res.locals, 'pageMeta.description', page.description)
page.mode = 'update'
@@ -454,6 +460,14 @@ router.get('/*', async (req, res, next) => {
body: WIKI.config.theming.injectBody
}
if (!_.isEmpty(page.extra.css)) {
injectCode.css = `${injectCode.css}\n${page.extra.css}`
}
if (!_.isEmpty(page.extra.js)) {
injectCode.body = `${injectCode.body}\n${page.extra.js}`
}
if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) {
// -> Convert page TOC
if (_.isString(page.toc)) {

View File

@@ -51,6 +51,10 @@ module.exports = class Page extends Model {
}
}
static get jsonAttributes() {
return ['extra']
}
static get relationMappings() {
return {
tags: {
@@ -139,6 +143,10 @@ module.exports = class Page extends Model {
title: 'string'
}
],
extra: {
js: 'string',
css: 'string'
},
title: 'string',
toc: 'string',
updatedAt: 'string'
@@ -248,7 +256,7 @@ module.exports = class Page extends Model {
throw new WIKI.Error.PageEmptyContent()
}
// -> Format JS Scripts
// -> Format CSS Scripts
let scriptCss = ''
if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
locale: opts.locale,
@@ -375,7 +383,7 @@ module.exports = class Page extends Model {
ogPage.extra = {}
}
// -> Format JS Scripts
// -> Format CSS Scripts
let scriptCss = _.get(ogPage, 'extra.css', '')
if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
locale: opts.locale,
@@ -787,6 +795,7 @@ module.exports = class Page extends Model {
'pages.localeCode',
'pages.authorId',
'pages.creatorId',
'pages.extra',
{
authorName: 'author.name',
authorEmail: 'author.email',
@@ -846,6 +855,10 @@ module.exports = class Page extends Model {
creatorId: page.creatorId,
creatorName: page.creatorName,
description: page.description,
extra: {
css: page.extra ? page.extra.css : '',
js: page.extra ? page.extra.js : ''
},
isPrivate: page.isPrivate === 1 || page.isPrivate === true,
isPublished: page.isPublished === 1 || page.isPublished === true,
publishEndDate: page.publishEndDate,

View File

@@ -14,6 +14,8 @@ block body
description=page.description
:tags=page.tags
:is-published=page.isPublished
script-css=page.extra.css
script-js=page.extra.js
init-mode=page.mode
init-editor=page.editorKey
init-content=page.content

View File

@@ -4,7 +4,7 @@ block body
#root.is-fullscreen
.app-error
a(href='/')
img(src='/svg/logo-wikijs.svg')
img(src='/_assets/svg/logo-wikijs.svg')
strong Oops, something went wrong...
span= message