From 3b0060187294e9d4b64892c735c9ca68d31f6804 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Mon, 21 Jun 2021 20:14:29 +0200 Subject: [PATCH] Inline CSS & JS into HTML export template Previously, the HTML export template `html.hbs` included CDN links for the HTML and CSS resources. This commit enables Webpack to create a new `htmlexport.html` at build-time, which includes all resources inline. That template is then used as before by the frontend to be populated with the rendered note content. The tradeoff is that each exported .html file is about 5.6 MB in size, as we need to inline all fonts (icons & emojis). Signed-off-by: David Mehren --- public/js/extra.js | 25 ++++++++++------------- public/js/htmlExport.js | 21 ++++++++++++++++++- public/views/{html.hbs => htmlexport.ejs} | 21 ++----------------- webpack.htmlexport.js | 20 +++++++++++++++--- webpack.prod.js | 3 +++ 5 files changed, 53 insertions(+), 37 deletions(-) rename public/views/{html.hbs => htmlexport.ejs} (71%) diff --git a/public/js/extra.js b/public/js/extra.js index 6e3b0ed0e..c72007b6f 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -681,21 +681,18 @@ export function exportToHTML (view) { const tocAffix = $('#ui-toc-affix').clone() tocAffix.find('*').removeClass('active').find("a[href^='#'][smoothhashscroll]").removeAttr('smoothhashscroll') // generate html via template - $.get(`${serverurl}/build/html.min.css`, css => { - $.get(`${serverurl}/views/html.hbs`, template => { - let html = template.replace('{{{url}}}', serverurl) - html = html.replace('{{title}}', title) - html = html.replace('{{{css}}}', css) - html = html.replace('{{{html}}}', src[0].outerHTML) - html = html.replace('{{{ui-toc}}}', toc.html()) - html = html.replace('{{{ui-toc-affix}}}', tocAffix.html()) - html = html.replace('{{{lang}}}', (md && md.meta && md.meta.lang) ? `lang="${md.meta.lang}"` : '') - html = html.replace('{{{dir}}}', (md && md.meta && md.meta.dir) ? `dir="${md.meta.dir}"` : '') - const blob = new Blob([html], { - type: 'text/html;charset=utf-8' - }) - saveAs(blob, filename, true) + $.get(`${serverurl}/build/htmlexport.html`, template => { + let html = template.replace('{{{url}}}', serverurl) + html = html.replace('{{title}}', title) + html = html.replace('{{{html}}}', src[0].outerHTML) + html = html.replace('{{{ui-toc}}}', toc.html()) + html = html.replace('{{{ui-toc-affix}}}', tocAffix.html()) + html = html.replace('{{{lang}}}', (md && md.meta && md.meta.lang) ? `lang="${md.meta.lang}"` : '') + html = html.replace('{{{dir}}}', (md && md.meta && md.meta.dir) ? `dir="${md.meta.dir}"` : '') + const blob = new Blob([html], { + type: 'text/html;charset=utf-8' }) + saveAs(blob, filename, true) }) } diff --git a/public/js/htmlExport.js b/public/js/htmlExport.js index 1a873aca0..676e2b1bc 100644 --- a/public/js/htmlExport.js +++ b/public/js/htmlExport.js @@ -1,6 +1,25 @@ +require('bootstrap/dist/css/bootstrap.min.css') +require('fork-awesome/css/fork-awesome.min.css') +require('ionicons/css/ionicons.min.css') +require('prismjs/prism') +require('prismjs/themes/prism.css') +require('prismjs/components/prism-wiki') +require('prismjs/components/prism-haskell') +require('prismjs/components/prism-go') +require('prismjs/components/prism-typescript') +require('prismjs/components/prism-jsx') +require('prismjs/components/prism-makefile') +require('prismjs/components/prism-gherkin') +require('highlight.js/styles/github-gist.css') +require('emojify.js/dist/css/basic/emojify.min.css') require('../css/github-extract.css') require('../css/markdown.css') require('../css/extra.css') require('../css/slide-preview.css') -require('../css/google-font.css') +require('../css/font.css') require('../css/site.css') +const $ = require('jquery') +window.jQuery = $ +window.$ = $ +require('bootstrap') +require('gist-embed/gist-embed.min') diff --git a/public/views/html.hbs b/public/views/htmlexport.ejs similarity index 71% rename from public/views/html.hbs rename to public/views/htmlexport.ejs index 7e8268ba3..ee83d8505 100644 --- a/public/views/html.hbs +++ b/public/views/htmlexport.ejs @@ -19,22 +19,7 @@ - - - - - - - - - - + <% _.forEach(htmlWebpackPlugin.files.css, function(cssFile) { %><% }); %> @@ -52,9 +37,7 @@ - - - + <% _.forEach(htmlWebpackPlugin.files.js, function(jsFile) { %><% }); %>