-
Notifications
You must be signed in to change notification settings - Fork 53
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
Add support for custom scripts #903
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool to see this in action, and I know this kind of customization will be super useful to some developers. Nice work!
I'm very interested to hear other feedback from the community on your forums pitch.
Here's some initial feedback on the renderer implementation itself in the meantime.
// The "on-load" scripts are run here (on the routing hook), not on `created` or `mounted`, | ||
// so that the scripts have access to the dynamically-added documentation HTML for the | ||
// current topic. | ||
await runCustomPageLoadScripts(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm doing a quick test with a very simple script that looks like this:
[
{
"code": "window.alert(\"hello world\");",
"run": "on-load-and-navigate"
}
]
My expectation is that the popup happens once on the initial page load (1) and also after the page loads for every link I click (2).
What actually happens for scenario 2 is that I get the popup immediately after clicking a link when the URL has changed to the new page, but the HTML is still showing the previous page I was navigating away from.
Question: Is my expectation wrong or is this just possibly an issue with the implementation?
If it's an issue with the implementation, we would probably want to look into using the vue-router hooks and guards for calling these functions instead of the $route
watcher that you have in the root component here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The visual issue you’re describing in scenario 2 is a quirk of alerts: despite what the browser may be displaying, the alert in scenario 2 is fired strictly after the link is clicked and the subpage is loaded. To prove this, try the following script instead:
window.alert(document.querySelector('h1').innerText);
and notice that the alert text matches the H1 of the subpage to which you’re navigating, not of the subpage you’re in, regardless of what the browser is displaying. This means that, in scenario 2, the script is running strictly after navigation.
As an additional test, also try:
document.querySelector('h1').style.fontStyle = 'italic';
and notice, in scenario 2, that the H1 (dynamically inserted into the subpage to which you're navigating) is successfully styled, which means that the script did run strictly after navigation.
As for using vue-router hooks instead of the $route
watcher, I agree that that would be the more correct approach. I’ll work on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The visual issue you’re describing in scenario 2 is a quirk of alerts: despite what the browser may be displaying, the alert in scenario 2 is fired strictly after the link is clicked and the subpage is loaded. To prove this, try the following script instead:
window.alert(document.querySelector('h1').innerText);and notice that the alert text matches the H1 of the subpage to which you’re navigating, not of the subpage you’re in, regardless of what the browser is displaying. This means that, in scenario 2, the script is running strictly after navigation.
As an additional test, also try:
document.querySelector('h1').style.fontStyle = 'italic';and notice, in scenario 2, that the H1 (dynamically inserted into the subpage to which you're navigating) is successfully styled, which means that the script did run strictly after navigation.
Whoops. Got it, that makes sense.
As for using vue-router hooks instead of the
$route
watcher, I agree that that would be the more correct approach. I’ll work on it.
Sounds good. I think this approach is fine after realizing my problem was with my example, although it still might be good to use those hooks if possible to be safe from any potential issues related to the navigation cycle.
* @returns {Promise<void>} | ||
*/ | ||
export async function runCustomNavigateScripts() { | ||
await runCustomScripts(shouldRunOnNavigate, evalScript); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m using eval
instead of import
just to support inline scripts; though, for sandboxing and performance, I should’ve used new Function
instead. Please let me know whether I should:
- Replace
eval
withnew Function
and keep inline scripts. - Replace
eval
with dynamic imports and remove inline scripts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personally prefer the latter since all inline scripts could be expressed as simple local scripts, and I would much prefer using dynamic imports over manually evaluating the code. I could be convinced otherwise if there's a strong need to explicitly support inline code, but I think the dynamic import approach would be simpler code-wise and also less prone to possible issues.
* @param {RequestInit?} options Optional request settings. | ||
* @returns {Promise<string>} The text contents of the file. | ||
*/ | ||
export default async function fetchText(filepath, options) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this maybe make sense to live in the src/utils/data.js
file where we have similar functions for fetching data instead of creating a new file just for this?
(Not a major issue, and there's a possibility this function might not even be needed based on my other comment about avoiding eval if possible.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I’ll move fetchText
to src/utils/data.js
— unless we switch to dynamic imports, in which case I’ll remove it.
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
/* eslint-disable */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the linter needed to be disabled for this entire file or was it just for the one line where you disabled it inline? Just curious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s a mistake! A relic from when I littered the file with console.log
statements for debugging. Thanks for catching it. 😮💨
const response = await fetch(url); | ||
if (!response.ok) { | ||
// If the file is absent, fail silently. | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One existing problem we have with the same pattern from theme-settings.json
is that we always try to fetch the file, and the 404 is always reported in the console in the case where one wasn't provided.
I don't think this needs to be solved with this PR, but it would be nice to fix this for both files so that the renderer is informed in some way of the fact that the catalog even has one of these files before it tries to fetch them, so this "always 404" problem goes away in the normal case.
(This would need to be addressed in a coordinated way between DocC and DocC-Render in the future)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's an unfortunate behavior of fetch
... something we can look into in a later PR.
Sidenote: I should probably point out that the way I’m error-handling custom-scripts.json
intentionally does not match how theme-settings.json
is error-handled. fetchThemeSettings
has a blanket catch
statement, which means that documentation authors aren’t notified if the file is malformed or if it violates the theme-settings schema. Messing with this choice for theme-settings.json
is outside the scope of the proposal, so I didn’t; but for custom-scripts.json
, if the file is malformed or violates the custom-scripts schema then we throw an error and don’t catch it. Please let me know if schema violations should be caught for custom-scripts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds good.
I don't think it's necessarily a problem with the fetch behavior—it's just that we shouldn't even be trying to fetch any of these JSON files if they haven't been provided in the documentation catalog in the first place—we would just need some added infrastructure so that DocC could inform the renderer that it will never even need to make an http request for these files if they weren't used.
Again, not any issue with your implementation—just noting an existing problem that will happen here as well.
*/ | ||
function shouldRunOnPageLoad(customScript) { | ||
return !has(customScript, 'run') | ||
|| customScript.run === 'on-load' || customScript.run === 'on-load-and-navigate'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very minor thing, but it would be great to have some constants or an enum-like Object for some of these strings so that we don't have to rely on them always being typed exactly the same in all the places in the code. (Wish we were using typescript so this kind of thing was a little more straightforward)
Something like:
const Run = {
onLoad: 'on-load',
onLoadAndNavigate: 'on-load-and-navigate',
};
or
const RunOnLoad = 'on-load';
const RunOnLoadAndNavigate = 'on-load-and-navigate';
etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!
Summary
First-class support for adding custom scripts to a DocC-generated website. These may be local scripts, in which case the website will continue to work offline, or they may be external scripts at a user-specified URL. This support is in the form of a
custom-scripts.json
file, the scripting analog oftheme-settings.json
.Full pitch: https://forums.swift.org/t/pitch-support-for-custom-scripts-in-docc/75357.
Dependencies
Corresponding change in swift-docc.
Testing
custom-scripts.json
and the custommathjax-config.js
script (shown in the pitch) to your documentation catalog.docc convert
the documentation catalog with your custom scripts. Observe that the modified swift-docc copiedcustom-scripts.json
and the custom scripts into the documentation archive.Checklist
Make sure you check off the following items. If they cannot be completed, provide a reason.
npm test
, and it succeeded