Skip to content

Commit

Permalink
Discard changes to src/notebook/Export.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Nov 8, 2024
1 parent e8eefa3 commit 80a86e0
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/notebook/Export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,37 @@ import Pkg
using Base64
using HypertextLiteral
import URIs
using MIMEs: mime_from_path

const default_binder_url = "https://mybinder.org/v2/gh/fonsp/pluto-on-binder/v$(string(PLUTO_VERSION))"

const cdn_version_override = nothing
# const cdn_version_override = "2a48ae2"
const distdir = "frontend-dist-static"

if cdn_version_override !== nothing
@warn "Reminder to fonsi: Using a development version of Pluto for CDN assets. The binder button might not work. You should not see this on a released version of Pluto." cdn_version_override
end

cdnified_editor_html(; kwargs...) = cdnified_html("editor.html"; kwargs...)

function file2base64dataurl(path::AbstractString)
string("data:", mime_from_path(path), ";base64,", base64encode(read(path)))
end

function cdnified_html(filename::AbstractString;
version::Union{Nothing,VersionNumber,AbstractString}=nothing,
pluto_cdn_root::Union{Nothing,AbstractString}=nothing,
base64assets::Bool=true,
)
should_use_bundled_cdn = version (nothing, PLUTO_VERSION) && pluto_cdn_root === nothing

something(
if should_use_bundled_cdn
try
original = read(project_relative_path(distdir, filename), String)
original = read(project_relative_path("frontend-dist", filename), String)

cdn_root = "https://cdn.jsdelivr.net/gh/fonsp/Pluto.jl@$(string(PLUTO_VERSION))/frontend-dist/"

@debug "Using CDN for Pluto assets:" cdn_root

replace_with_cdn(original) do url
@info "$url"
contains(string(url), "escape_txt_for_html") && return url
# Because parcel creates filenames with a hash in them, we can check if the file exists locally to make sure that everything is in order.
@assert isfile(project_relative_path(distdir, url)) "Could not find the file $(project_relative_path(distdir, url)) locally, that's a bad sign."
@info "let's see it $url, $should_use_bundled_cdn "
if base64assets
return file2base64dataurl(project_relative_path(distdir, url))
end
@assert isfile(project_relative_path("frontend-dist", url)) "Could not find the file $(project_relative_path("frontend-dist", url)) locally, that's a bad sign."

URIs.resolvereference(cdn_root, url) |> string
end
catch e
Expand All @@ -57,9 +46,7 @@ function cdnified_html(filename::AbstractString;
cdn_root = something(pluto_cdn_root, "https://cdn.jsdelivr.net/gh/fonsp/Pluto.jl@$(something(cdn_version_override, string(something(version, PLUTO_VERSION))))/frontend/")

@debug "Using CDN for Pluto assets:" cdn_root
if base64assets
@warn("Trying to use bundled assets for $filename. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.")
end

replace_with_cdn(original) do url
URIs.resolvereference(cdn_root, url) |> string
end
Expand Down Expand Up @@ -127,7 +114,7 @@ function generate_html(;
header_html::AbstractString="",
)::String

cdnified = cdnified_editor_html(; version, pluto_cdn_root, base64assets=true)
cdnified = cdnified_editor_html(; version, pluto_cdn_root)

length(statefile_js) > 32000000 && @error "Statefile embedded in HTML is very large. The file can be opened with Chrome and Safari, but probably not with Firefox. If you are using PlutoSliderServer to generate this file, then we recommend the setting `baked_statefile=false`. If you are not using PlutoSliderServer, then consider reducing the size of figures and output in the notebook." length(statefile_js)

Expand All @@ -151,7 +138,6 @@ end

function replace_at_least_once(s, pair)
from, to = pair
@info "replacing once at least once" s from to
@assert occursin(from, s)
replace(s, pair)
end
Expand Down Expand Up @@ -237,7 +223,7 @@ function replace_with_cdn(cdnify::Function, s::String, idx::Integer=1)
s
else
url = only(next_match.captures)
if occursin("//", url) || url dont_cdnify || occursin("data:", url)
if occursin("//", url) || url dont_cdnify
# skip this one
replace_with_cdn(cdnify, s, nextind(s, next_match.offset))
else
Expand Down

0 comments on commit 80a86e0

Please sign in to comment.