diff --git a/build.js b/build.js new file mode 100644 index 000000000..82efd0279 --- /dev/null +++ b/build.js @@ -0,0 +1,107 @@ +var Fs = require("fs"); +var Fse = require("fs-extra"); +var Path = require("path"); +var config = require("./lib/load-config"); + +var swap = function (s, o) { + return s + .replace(/\{\{([^}]+?)\}\}/g, function (all, token) { + var content = o[token]; + + if (typeof(content) === 'number') { + content = String(content); + } + + if (typeof(content) !== 'string') { + console.error("expected {{%s}}", token); + throw new Error("invalid input"); + } + + if (!content) { + console.error("expected {{%s}}", token); + throw new Error("insufficient input"); + } + return content; + }); +}; + +const ogData = ` + + + + `; + +var previewExists = function (name) { + if (Fs.existsSync('customize/images/opengraph_preview/')) { + return Fs.existsSync(`customize/images/opengraph_preview/${name}`); + } + return Fs.existsSync(`customize.dist/images/opengraph_preview/${name}`); +}; + +var templateOG = function (a, type) { + return swap(ogData, { + rootUrl: config.httpUnsafeOrigin, + app: a, + title: type && `Encrypted ${type}` || 'CryptPad', + image: previewExists(`og-${a}.png`) && `og-${a}.png` || `og-default.png` + }); +}; + +var insert = function (src, template) { + var matchs = src.match(/()|(.*<\/title>)/g); + + if (!matchs || !matchs.length) { + return src; + } + return src.replace(matchs.at(-1), `$& ${template}`); +}; + +var buildPath = './customize/www'; +var tmpPath = 'CRYPTPAD_TEMP_BUILD'; + +var write = function (content, dest) { + console.log(`Creating ${dest}`); + + var path = Path.join(tmpPath, dest); + var dirPath = Path.dirname(path); + Fse.mkdirpSync(dirPath); + Fs.writeFileSync(path, content); +}; + +console.log("Creating target directories"); +// remove tmp path so we start fresh +Fse.removeSync(tmpPath); +Fse.mkdirpSync(tmpPath); + +var srcAppTypes = Fs.readFileSync('./www/common/translations/messages.json', 'utf8'); +var types = JSON.parse(srcAppTypes).type; + +var appIndexesToBuild = [ + 'sheet', + 'doc', + 'presentation', + 'pad', + 'kanban', + 'code', + 'form', + 'poll', + 'whiteboard', + 'slide' +]; + +appIndexesToBuild.forEach(function (app) { + console.log(`Parsing ./www/${app}/index.html`); + var src = Fs.readFileSync(`./www/${app}/index.html`, 'utf8'); + + write( + insert(src, templateOG(app, types[app])), + `${app}/index.html` + ); +}); + +Fse.removeSync(buildPath); +Fse.moveSync(tmpPath, buildPath); +/* Fse.renameSync(tmpPath, buildPath); // is rename doing more than move here? +// cos not working with buildPath like 'pre-path/main-dir'. If so, + var dirPath = Path.dirname(path); + Fse.mkdirpSync(dirPath); // may work */ diff --git a/customize.dist/images/opengraph_preview/og-default.png b/customize.dist/images/opengraph_preview/og-default.png new file mode 100644 index 000000000..629a55717 Binary files /dev/null and b/customize.dist/images/opengraph_preview/og-default.png differ diff --git a/package.json b/package.json index 164d9f4f7..3d197d029 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,8 @@ "test": "node scripts/TestSelenium.js", "test-rpc": "cd scripts/tests && node test-rpc", "template": "cd customize.dist/src && for page in ../index.html ../contact.html ../features.html ../../www/login/index.html ../../www/register/index.html ../../www/user/index.html;do echo $page; cp template.html $page; done;", - "evict-inactive": "node scripts/evict-inactive.js" + "evict-inactive": "node scripts/evict-inactive.js", + "build": "node build.js", + "clean-built": "rm -rf customize/www/" } } diff --git a/server.js b/server.js index 3830a1fbc..0a05ae70b 100644 --- a/server.js +++ b/server.js @@ -148,6 +148,7 @@ app.use(function (req, res, next) { next(); }); +app.use(Express.static(__dirname + '/customize/www')); app.use(Express.static(__dirname + '/www')); // FIXME I think this is a regression caused by a recent PR diff --git a/www/code/index.html b/www/code/index.html index 66b56362c..f4f11aef9 100644 --- a/www/code/index.html +++ b/www/code/index.html @@ -5,10 +5,6 @@ - - - - diff --git a/www/doc/index.html b/www/doc/index.html index a1d5c477f..321b16709 100644 --- a/www/doc/index.html +++ b/www/doc/index.html @@ -5,10 +5,6 @@ - - - - diff --git a/www/form/index.html b/www/form/index.html index 71f1371bc..96a3cce86 100644 --- a/www/form/index.html +++ b/www/form/index.html @@ -5,10 +5,6 @@ - - - - diff --git a/www/kanban/index.html b/www/kanban/index.html index 054e57533..c2076f8e5 100644 --- a/www/kanban/index.html +++ b/www/kanban/index.html @@ -6,10 +6,6 @@ - - - - diff --git a/www/pad/index.html b/www/pad/index.html index 2779db112..a370d0536 100644 --- a/www/pad/index.html +++ b/www/pad/index.html @@ -5,10 +5,6 @@ - - - - diff --git a/www/poll/index.html b/www/poll/index.html index a4a84e003..96a3cce86 100644 --- a/www/poll/index.html +++ b/www/poll/index.html @@ -5,10 +5,6 @@ - - - - diff --git a/www/presentation/index.html b/www/presentation/index.html index 0038690e4..321b16709 100644 --- a/www/presentation/index.html +++ b/www/presentation/index.html @@ -5,10 +5,6 @@ - - - - diff --git a/www/sheet/index.html b/www/sheet/index.html index e58f6b897..321b16709 100644 --- a/www/sheet/index.html +++ b/www/sheet/index.html @@ -5,10 +5,6 @@ - - - - diff --git a/www/slide/index.html b/www/slide/index.html index 8b8b102a2..f4f11aef9 100644 --- a/www/slide/index.html +++ b/www/slide/index.html @@ -5,10 +5,6 @@ - - - - diff --git a/www/whiteboard/index.html b/www/whiteboard/index.html index cc3fa85b2..f4f11aef9 100644 --- a/www/whiteboard/index.html +++ b/www/whiteboard/index.html @@ -5,10 +5,6 @@ - - - -