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

feat: make published html scripts configurable #11149

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

CNLHC
Copy link
Contributor

@CNLHC CNLHC commented Mar 21, 2024

Currently generated html has fixed scripts list, sometimes the user may want to insert extra scripts to their generated html (e.g. google analytics script). It is a good idea to make the scripts list configurable.

@CNLHC
Copy link
Contributor Author

CNLHC commented Apr 18, 2024

@andelf @logseq-cldwalker Hi guys, just notice that this PR exists for some time. I think it is a good feature for those who are using logseq to publish their blog. Could you please review it or leave some comments when you are free?

@logseq-cldwalker
Copy link
Collaborator

@CNLHC Hi. Sure. Could you provide a specific example of how you're using the new option(s)? I'll take a look at this next week

@CNLHC
Copy link
Contributor Author

CNLHC commented Apr 20, 2024

@CNLHC Hi. Sure. Could you provide a specific example of how you're using the new option(s)? I'll take a look at this next week

Ok. I think two common use cases are:

  1. Add the Google Analyticals to the published sites
  2. Add comments feature to the published sites

With this PR and logseq/publish-spa#27 merged, a user can define a html-options in his config.edn, and then the scripts tag will be injected into the generated(or "published") html file.

I will post an real example later.

@CNLHC
Copy link
Contributor Author

CNLHC commented Apr 23, 2024

@logseq-cldwalker

Use case 1 : integrate with Google Analytics

  1. user add some custom scripts in their config-edn
;; config.edn
:html-options {:scripts [{:src "https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"},
                         {:content "  
                                window.dataLayer = window.dataLayer || [];
                                function gtag(){dataLayer.push(arguments);}
                                gtag('js', new Date()); 
                                gtag('config', 'G-XXXXXXXX');"}]}
  1. user use publish-spa to generate html
logseq-publish-spa <OUTPUT>  -s <STATIC>   -d <INPUT> --theme-mode dark --accent-color indigo
  1. generated html will contain the user-defined scripts
  <!-- these two scripts were generated from config.edn -->
  <script src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
  <script>  window.dataLayer = window.dataLayer || [];
    function gtag() { dataLayer.push(arguments); }
    gtag('js', new Date());

    gtag('config', 'G-XXXXXXXX');</script>
  <!-- end -->
  <script src="static/js/react.production.min.js"></script>
  <script src="static/js/react-dom.production.min.js"></script>
  <script src="static/js/ui.js"></script>
  <script src="static/js/main.js"></script>
  <script src="static/js/interact.min.js"></script>
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/katex.min.js"></script>
  <script src="static/js/html2canvas.min.js"></script>
  <script src="static/js/code-editor.js"></script>
  <script src="static/js/custom.js"></script>

Copy link
Collaborator

@logseq-cldwalker logseq-cldwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CNLHC Interesting idea on letting users configure this from their config.edn. Currently :html-options is just an unused, lower level option. I'm open to exposing it to config.edn but there is more work to do:

  • A config option needs to be read from repo-config in this fn and passed to publishing-html. Let's call the config option :publishing/html-options
  • Since a new config option is added, a new entry needs to be added to https://github.com/logseq/logseq/blob/master/src/resources/templates/config.edn. No need to set a default value but some documentation describing :before-scripts would be helpful
  • Since a new config option is added, a new entry needs to be added to this var to validate the new config value

With these updates, a user can configure html-options in their config.edn and it will work with publish-spa or the built-in export. There won't be a need for logseq/publish-spa#27

@@ -120,7 +120,8 @@ necessary db filtering"
}
}
}(window.location))"]
;; TODO: should make this configurable
(map #(identity (let [{:keys [src,content]} %]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised hiccup supports this. Regardless, we shouldn't be injecting a seq in a vector. Would prefer that the scripts in :body are built dynamically e.g. (into [:body [:div {:id "root"}]] scripts) where scripts is the user configured ones and all the default ones below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised hiccup supports this. Regardless, we shouldn't be injecting a seq in a vector. Would prefer that the scripts in :body are built dynamically e.g. (into [:body [:div {:id "root"}]] scripts) where scripts is the user configured ones and all the default ones below

Sorry I do not get you. What do you mean by "injecting a seq in a vector"? I guess the seq is the user defined scripts (or scripts-before) ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current map is returning a seq which results in:

[:body
   [:div {:id "root"}]
   ...
   ;; from scripts-before
   '([:script {:src ""} ""])
   [:script {:src "static/js/react.production.min.js"}]
   ...

All the scripts-before scripts should look like the existing :script vectors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I got you this time. I have added a new function to fullfill this.

@CNLHC CNLHC force-pushed the feat/publishing/make_js_configurable branch from e6a2435 to f8b27f5 Compare May 10, 2024 00:54
@CNLHC
Copy link
Contributor Author

CNLHC commented May 10, 2024

@logseq-cldwalker I fixed all the lint error, may be you can review this PR again when you are free

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants