-
Notifications
You must be signed in to change notification settings - Fork 146
Add support for custom scripts #1056
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
base: main
Are you sure you want to change the base?
Changes from all commits
013ceb4
8d513d8
8671a6b
e715ee4
b8512d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{ | ||
"openapi": "3.0.0", | ||
"info": { | ||
"title": "Custom Scripts", | ||
"description": "This spec describes the permissible contents of a custom-scripts.json file in a documentation catalog, which is used to add custom scripts to a DocC-generated website.", | ||
"version": "0.0.1" | ||
}, | ||
"paths": {}, | ||
"components": { | ||
"schemas": { | ||
"Scripts": { | ||
"type": "array", | ||
"description": "An array of custom scripts, which is the top-level container in a custom-scripts.json file.", | ||
"items": { | ||
"oneOf": [ | ||
{ "$ref": "#/components/schemas/ExternalScript" }, | ||
{ "$ref": "#/components/schemas/LocalScript" }, | ||
{ "$ref": "#/components/schemas/InlineScript" } | ||
] | ||
} | ||
}, | ||
"Script": { | ||
"type": "object", | ||
"description": "An abstract schema representing any script, from which all three script types inherit.", | ||
"properties": { | ||
"type": { | ||
"type": "string", | ||
"description": "The `type` attribute of the HTML script element." | ||
}, | ||
"run": { | ||
"type": "string", | ||
"enum": ["on-load", "on-navigate", "on-load-and-navigate"], | ||
"description": "Whether the custom script should be run only on the initial page load, each time the reader navigates after the initial page load, or both." | ||
} | ||
} | ||
}, | ||
"ScriptFromFile": { | ||
"description": "An abstract schema representing a script from an external or local file; that is, not an inline script.", | ||
"allOf": [ | ||
{ "$ref": "#/components/schemas/Script" }, | ||
{ | ||
"properties": { | ||
"async": { "type": "boolean" }, | ||
"defer": { "type": "boolean" }, | ||
"integrity": { "type": "string" }, | ||
} | ||
} | ||
] | ||
}, | ||
"ExternalScript": { | ||
"description": "A script at an external URL.", | ||
"allOf": [ | ||
{ "$ref": "#/components/schemas/ScriptFromFile" }, | ||
{ | ||
"required": ["url"], | ||
"properties": { | ||
"url": { "type": "string" } | ||
} | ||
} | ||
] | ||
}, | ||
"LocalScript": { | ||
"description": "A script from a local file.", | ||
"allOf": [ | ||
{ "$ref": "#/components/schemas/ScriptFromFile" }, | ||
{ | ||
"required": ["name"], | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "The name of the local script file, optionally including the '.js' extension." | ||
}, | ||
} | ||
} | ||
] | ||
}, | ||
"InlineScript": { | ||
"description": "A script whose source code is in the custom-scripts.json file itself.", | ||
"allOf": [ | ||
{ "$ref": "#/components/schemas/Script" }, | ||
{ | ||
"required": ["code"], | ||
"properties": { | ||
"code": { | ||
"type": "string", | ||
"description": "The source code of the inline script." | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"requestBodies": {}, | ||
"securitySchemes": {}, | ||
"links": {}, | ||
"callbacks": {} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,18 @@ struct ConvertFileWritingConsumer: ConvertOutputConsumer { | |
for downloadAsset in context.registeredDownloadsAssets(for: bundleID) { | ||
try copyAsset(downloadAsset, to: downloadsDirectory) | ||
} | ||
|
||
// Create custom scripts directory if needed. Do not append the bundle identifier. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not append the bundle ID here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
(Also, if the relative path to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. That's not how combined documentation works (which is already available as an experimental feature). Non-content files aren't automatically included in the combined archive. If this was treated as an asset, a collision between archives would be treated as an error. If this file is intended to be copied over into the combined archive, it should have an archive-unique prefix and this PR should update the merge command to copy over the expected files. |
||
let scriptsDirectory = targetFolder | ||
.appendingPathComponent("custom-scripts", isDirectory: true) | ||
if !fileManager.directoryExists(atPath: scriptsDirectory.path) { | ||
try fileManager.createDirectory(at: scriptsDirectory, withIntermediateDirectories: true, attributes: nil) | ||
} | ||
|
||
// Copy all registered custom scripts to the output directory. | ||
for customScript in context.registeredCustomScripts(for: bundleID) { | ||
try copyAsset(customScript, to: scriptsDirectory) | ||
} | ||
|
||
// If the bundle contains a `header.html` file, inject a <template> into | ||
// the `index.html` file using its contents. This will only be done if | ||
|
@@ -145,6 +157,16 @@ struct ConvertFileWritingConsumer: ConvertOutputConsumer { | |
} | ||
try fileManager.copyItem(at: themeSettings, to: targetFile) | ||
} | ||
|
||
// Copy the `custom-scripts.json` file into the output directory if one | ||
// is provided. | ||
if let customScripts = bundle.customScripts { | ||
let targetFile = targetFolder.appendingPathComponent(customScripts.lastPathComponent, isDirectory: false) | ||
if fileManager.fileExists(atPath: targetFile.path) { | ||
try fileManager.removeItem(at: targetFile) | ||
} | ||
try fileManager.copyItem(at: customScripts, to: targetFile) | ||
} | ||
} | ||
|
||
func consume(linkableElementSummaries summaries: [LinkDestinationSummary]) throws { | ||
|
Uh oh!
There was an error while loading. Please reload this page.