Commit 10f6ab36 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Split out config proper from script-embed script

- Allow PDFs to benefit from shared/easily-updated client config
parent da9c1d78
...@@ -21,28 +21,40 @@ const TEMPLATE_PATH = `${__dirname}/templates/`; ...@@ -21,28 +21,40 @@ const TEMPLATE_PATH = `${__dirname}/templates/`;
*/ */
/** /**
* Generate `<script>` content for client configuration and injection * Render client config and script embed
* *
* @param {string} clientUrl * @param {Object} context
* @return {string}
*/ */
function renderConfig(clientUrl) { function renderScript(context) {
const scriptTemplate = fs.readFileSync( const scriptTemplate = `
`${TEMPLATE_PATH}client-config.js.mustache`, {{{hypothesisConfig}}}
'utf-8' <script>
); const embedScript = document.createElement('script');
return Mustache.render(scriptTemplate, { clientUrl }); embedScript.src = '{{{clientUrl}}}'.replace('{current_host}', document.location.hostname);
document.body.appendChild(embedScript);
</script>
`;
return Mustache.render(scriptTemplate, context);
} }
/** /**
* Build context for rendering templates in the defined views directory. This * Build context for rendering templates in the defined views directory.
* is dead simple at present but could be extended as needs grow.
* *
* @param {Config} config * @param {Config} config
*/ */
function templateContext(config) { function templateContext(config) {
// Just the config by itself, in contrast with `hypothesisScript`, which
// combines this config with a <script> that adds the embed script
const hypothesisConfig = fs.readFileSync(
`${TEMPLATE_PATH}client-config.js.mustache`,
'utf-8'
);
return { return {
hypothesisScript: renderConfig(config.clientUrl), hypothesisConfig,
hypothesisScript: renderScript({
hypothesisConfig,
clientUrl: config.clientUrl,
}),
}; };
} }
...@@ -89,11 +101,15 @@ function serveDev(port, config) { ...@@ -89,11 +101,15 @@ function serveDev(port, config) {
// Serve PDF documents with PDFJS viewer and client script // Serve PDF documents with PDFJS viewer and client script
app.get('/pdf/:pdf', (req, res, next) => { app.get('/pdf/:pdf', (req, res, next) => {
if (fs.existsSync(`${PDF_PATH}${req.params.pdf}.pdf`)) { if (fs.existsSync(`${PDF_PATH}${req.params.pdf}.pdf`)) {
const relativeSourceUrl = `/pdf-source/${req.params.pdf}.pdf`;
const fullUrl = `${req.protocol}://${req.hostname}:${port}${req.originalUrl}`; const fullUrl = `${req.protocol}://${req.hostname}:${port}${req.originalUrl}`;
const context = templateContext(config);
res.render('pdfjs-viewer', { res.render('pdfjs-viewer', {
...context,
clientUrl: config.clientUrl, // URL to embed source
documentUrl: fullUrl, // The URL that annotations are associated with documentUrl: fullUrl, // The URL that annotations are associated with
url: `/pdf-source/${req.params.pdf}.pdf`, // The URL for the PDF source file url: relativeSourceUrl, // The URL for the PDF source file
clientUrl: config.clientUrl,
}); });
} else { } else {
next(); next();
......
<script> <script>
var appHost = document.location.hostname;
window.hypothesisConfig = function() { window.hypothesisConfig = function() {
return { return {
// enableExperimentalNewNoteButton: true, // enableExperimentalNewNoteButton: true,
...@@ -25,7 +24,4 @@ ...@@ -25,7 +24,4 @@
openSidebar: true, openSidebar: true,
}; };
}; };
var embedScript = document.createElement('script');
embedScript.src = '{{{clientUrl}}}'.replace('{current_host}', document.location.hostname);
document.body.appendChild(embedScript);
</script> </script>
\ No newline at end of file
...@@ -49,13 +49,7 @@ window.CLIENT_URL = '{{{clientUrl}}}'.replace('{current_host}', document.locatio ...@@ -49,13 +49,7 @@ window.CLIENT_URL = '{{{clientUrl}}}'.replace('{current_host}', document.locatio
<script src="/scripts/pdfjs-init.js"></script> <script src="/scripts/pdfjs-init.js"></script>
<!-- Configure Hypothesis client. --> <!-- Configure Hypothesis client. -->
<script> {{{hypothesisConfig}}}
window.hypothesisConfig = function() {
return {
openSidebar: true,
};
};
</script>
<!-- End Hypothesis modifications --> <!-- End Hypothesis modifications -->
......
...@@ -48,13 +48,7 @@ window.CLIENT_URL = '{{{clientUrl}}}'.replace('{current_host}', document.locatio ...@@ -48,13 +48,7 @@ window.CLIENT_URL = '{{{clientUrl}}}'.replace('{current_host}', document.locatio
<script src="/scripts/pdfjs-init.js"></script> <script src="/scripts/pdfjs-init.js"></script>
<!-- Configure Hypothesis client. --> <!-- Configure Hypothesis client. -->
<script> {{{hypothesisConfig}}}
window.hypothesisConfig = function() {
return {
openSidebar: true,
};
};
</script>
<!-- End Hypothesis modifications --> <!-- End Hypothesis modifications -->
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment