Skip to content

Commit

Permalink
Merge pull request #7536 from quarto-dev/bugfix/ojs-b64-contents
Browse files Browse the repository at this point in the history
base64-encode contents of ojs modules
  • Loading branch information
cscheid authored Nov 10, 2023
2 parents f8a1b4d + ac5e0bf commit 3fcc23f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/execute/ojs/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ import { getDivAttributes } from "../../core/handlers/base.ts";
import { pathWithForwardSlashes } from "../../core/path.ts";
import { executeInlineCodeHandlerMapped } from "../../core/execute-inline.ts";

import { encodeBase64 } from "encoding/base64.ts";

export interface OjsCompileOptions {
source: string;
format: Format;
Expand Down Expand Up @@ -816,7 +818,7 @@ export async function ojsCompile(
// script to append
const afterBody = [
`<script type="ojs-module-contents">`,
JSON.stringify({ contents: moduleContents }),
encodeBase64(JSON.stringify({ contents: moduleContents })),
`</script>`,
`<script type="module">`,
...scriptContents,
Expand Down
14 changes: 7 additions & 7 deletions src/resources/filters/quarto-post/ojs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ function ojs()
if uid > 0 then
local div = pandoc.Div({}, pandoc.Attr("", {"ojs-auto-generated", "hidden"}, {}))
div.content:insert(pandoc.RawBlock("html", "<script type='ojs-module-contents'>"))
div.content:insert(pandoc.RawBlock("html", '{"contents":['))
local contents = pandoc.List({})
contents:insert('{"contents":[')
for i, v in ipairs(cells) do
if i > 1 then
div.content:insert(",")
contents:insert(",")
end
div.content:insert(
pandoc.RawBlock(
"html",
contents:insert(
(' {"methodName":"interpret","inline":"true","source":"htl.html`<span>${' ..
escape_quotes(v.src) .. '}</span>`", "cellName":"' .. v.id .. '"}')))
escape_quotes(v.src) .. '}</span>`", "cellName":"' .. v.id .. '"}'))
end
div.content:insert(pandoc.RawBlock("html", ']}'))
contents:insert(']}')
div.content:insert(pandoc.RawBlock("html", quarto.base64.encode(table.concat(contents, ""))))
div.content:insert(pandoc.RawBlock("html", "</script>"))
doc.blocks:insert(div)
end
Expand Down
20 changes: 16 additions & 4 deletions src/resources/formats/html/ojs/quarto-ojs-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -19202,6 +19202,17 @@ class ModuleParser extends CellParser {

/* Fork changes end here */

// https://developer.mozilla.org/en-US/docs/Glossary/Base64

function base64ToBytes(base64) {
const binString = atob(base64);
return Uint8Array.from(binString, (m) => m.codePointAt(0));
}

function base64ToStr(base64) {
return new TextDecoder().decode(base64ToBytes(base64));
}

/*global Shiny, $, DOMParser, MutationObserver, URL
*
* ojs-connector.js
Expand Down Expand Up @@ -19500,18 +19511,19 @@ function importPathResolver(paths, localResolverMap) {
};
}


function createOjsModuleFromHTMLSrc(text) {
const parser = new DOMParser();
const doc = parser.parseFromString(text, "text/html");
const staticDefns = [];
for (const el of doc.querySelectorAll('script[type="ojs-define"]')) {
staticDefns.push(el.text);
staticDefns.push(base64ToStr(el.text));
}
const ojsSource = [];
for (
const content of doc.querySelectorAll('script[type="ojs-module-contents"]')
) {
for (const cell of JSON.parse(content.text).contents) {
for (const cell of JSON.parse(base64ToStr(content.text)).contents) {
ojsSource.push(cell.source);
}
}
Expand Down Expand Up @@ -22922,7 +22934,7 @@ function displayOJSWarning(warning)
for (
const content of document.querySelectorAll('script[type="ojs-module-contents"]')
) {
for (const cellJson of JSON.parse(content.text).contents) {
for (const cellJson of JSON.parse(base64ToStr(content.text)).contents) {
let cell = document.getElementById(cellJson.cellName) || document.getElementById(`${cellJson.cellName}-1`);
if (!cell) {
// give up
Expand Down Expand Up @@ -23932,7 +23944,7 @@ function createRuntime() {
for (const el of document.querySelectorAll(
"script[type='ojs-module-contents']"
)) {
for (const call of JSON.parse(el.text).contents) {
for (const call of JSON.parse(base64ToStr(el.text)).contents) {
let source = window._ojs.isDashboard ? autosizeOJSPlot(call.source, call.cellName) : call.source;
switch (call.methodName) {
case "interpret":
Expand Down
4 changes: 2 additions & 2 deletions src/resources/formats/html/ojs/quarto-ojs-runtime.min.js

Large diffs are not rendered by default.

0 comments on commit 3fcc23f

Please sign in to comment.