Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading user assets before requirejs #2472

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/html/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,22 @@
class :: Symbol
uri :: String
islocal :: Bool
load_early :: Bool
attributes::Dict{Symbol, String}

function HTMLAsset(class::Symbol, uri::String, islocal::Bool, attributes::Dict{Symbol, String}=Dict{Symbol,String}())
function HTMLAsset(class::Symbol, uri::String, islocal::Bool, load_early::Bool=false, attributes::Dict{Symbol, String}=Dict{Symbol,String}())

Check warning on line 105 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L105

Added line #L105 was not covered by tests
if !islocal && match(r"^https?://", uri) === nothing
error("Remote asset URL must start with http:// or https://")
end
if islocal && isabspath(uri)
@error("Local asset should not have an absolute URI: $uri")
end
class in [:ico, :css, :js] || error("Unrecognised asset class $class for `$(uri)`")
new(class, uri, islocal, attributes)
new(class, uri, islocal, load_early, attributes)

Check warning on line 113 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L113

Added line #L113 was not covered by tests
end
end


"""
asset(uri)

Expand All @@ -133,6 +135,9 @@
relative to the documentation source directory (conventionally `src/`). This can be useful
when it is necessary to override the asset class of a local asset.

**`load_early`** can be used to load the JS before requirejs when the script
is not Asynchronous Module Definition (AMD) compatable.

Check warning on line 139 in src/html/HTMLWriter.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"compatable" should be "compatible".
mortenpi marked this conversation as resolved.
Show resolved Hide resolved

# Usage

```julia
Expand All @@ -148,15 +153,15 @@
])
```
"""
function asset(uri; class = nothing, islocal=false, attributes=Dict{Symbol,String}())
function asset(uri; class = nothing, islocal=false, load_early=false, attributes=Dict{Symbol,String}())

Check warning on line 156 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L156

Added line #L156 was not covered by tests
if class === nothing
class = assetclass(uri)
(class === nothing) && error("""
Unable to determine asset class for: $(uri)
It can be set explicitly with the `class` keyword argument.
""")
end
HTMLAsset(class, uri, islocal, attributes)
HTMLAsset(class, uri, islocal, load_early, attributes)

Check warning on line 164 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L164

Added line #L164 was not covered by tests
end

function assetclass(uri)
Expand Down Expand Up @@ -1005,6 +1010,10 @@
end,

script("documenterBaseURL=\"$(relhref(src, "."))\""),

# Custom user-provided assets which are loaded early for non-AMD compatable modules.

Check warning on line 1014 in src/html/HTMLWriter.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"compatable" should be "compatible".
mortenpi marked this conversation as resolved.
Show resolved Hide resolved
asset_links(src, filter(x -> x.load_early, ctx.settings.assets)),

Check warning on line 1015 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L1015

Added line #L1015 was not covered by tests

script[
:src => RD.requirejs_cdn,
Symbol("data-main") => relhref(src, ctx.documenter_js)
Expand All @@ -1026,8 +1035,9 @@
return e
end,
script[:src => relhref(src, ctx.themeswap_js)],

# Custom user-provided assets.
asset_links(src, ctx.settings.assets),
asset_links(src, filter(x -> !x.load_early, ctx.settings.assets))

Check warning on line 1040 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L1040

Added line #L1040 was not covered by tests
)
end

Expand Down
2 changes: 1 addition & 1 deletion test/examples/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function html_doc(
"assets/favicon.ico",
"assets/custom.css",
asset("https://example.com/resource.js"),
asset("http://example.com/fonts?param=foo", class=:css),
asset("http://example.com/fonts?param=foo", class=:css, load_early=true),
asset("https://fonts.googleapis.com/css?family=Nanum+Brush+Script&display=swap", class=:css),
],
prettyurls = true,
Expand Down
Loading