Publish
Publish copied to clipboard
Addition Nodes in HTML head
Hi @JohnSundell, sorry for requesting pulling so frequently. Here is one about an API change. The idea is in early stage and I want to discuss about it with you if you are interested.
About the change
- To add the
additionalNodes
argument inhead
function to specify addition nodes for pages. - To add a whole-site additional Nodes for head.
I really appreciate the idea of "javascript-free", but sometimes we need to embed javascript in html head, such as when using Google Analytics. Therefore, I needed a way to add customized additional whole site nodes.
I have tried to write an extension in extension Node where Context == HTML.HeadContext
in my own package. But I think it might be better to include the ability to add additional nodes in the official Publish repo.
usage example One example is adding Google Analytics to Publish, which requires script node in head. I have made demo plugin which uses the additionalWholeSiteHeadNodes property.
Another example is writing articles which need specific .js files. I have tried to show LaTeX equations with MathJax, so I need to add MathJaX script on some pages (not all). In this case the 'additionalNodes' argument is helpful.
Please consider the request when convienent. Cheers, Zhijin
Oh, I was about to create same PR 👍 Right now we end up doing a pretty much the copy of the already prepared .head()
which kinda sucks.
Oh, I was about to create same PR 👍 Right now we end up doing a pretty much the copy of the already prepared
.head()
which kinda sucks.
It seems we all have this issue.
Since I want to keep using current code of my theme, I am using forked version of Publish and I have to keep it updated with upstream. It sometimes causes conflicts with other plugins.
Btw do you have any suggestion on improvement this implementation? I am wondering if there is a better way to do it...
@Ze0nC I just made my own header function for now until this PR is merged.
Oh, I was about to create same PR 👍 Right now we end up doing a pretty much the copy of the already prepared
.head()
which kinda sucks.
Off topic question: do you find any way to modify the content after generation of HTML, just like the Modifier in Plot.
Off topic question: do you find any way to modify the content after generation of HTML, just like the Modifier in Plot.
I put a STRING_KEY in my article which I replace with the actual text while building page.
Hi @JohnSundell ! I have finished my defense so now I have more free time on it!
After some considerations and trials I found it is hard for me to implement it without using global variable, so I would like to discuss with you.
First of all, we need to store the nodes in some place, and I have considered:
-
Node<HTML.DocumentContext>
: This is bad since it is global. But it is the most convenient place to put global head nodes? -
Website
instance: Probably a good place, but I think 'Node' probably shouldn't appear here -
PublishContext
: Same with above. -
HTMLFactory
: Seems to be a good place. This is where HTML are generated. -
Theme
: Seems to be a good place?
I have considered for some days but haven't figured it out. Do you have some ideas/suggestions?
Cheers, Zhijin
Hi @JohnSundell. I hope you are doing well recently. I committed a draft of new way to manage global head nodes of websites. Here are the changes:
- 'ConditionalHeadNode' has been added. It defines a
var node: Node<HTML.HeadContext>
with avar condition: (Location) -> Bool
, andvar target: String
which is description ofWebsite
. - 'ConditionalHeadNode' has a static variable
var all: [ConditionalHeadNode]
, which holds all head nodes to add. - 2 new functions were added to extension of
Website
to add and get the head nodes.
When the above change, it is possible to:
-
Add global head nodes to certain websites.
-
Add head nodes to certain pages of website.
However, it is not perfect:
-
Should
Website
protocol has functions which deal withNode
? -
Current way to identify target websites is by using description of websites. This is not safe and Swiftly. However due to limitation of my skills I didn't find a better to do it.
-
Instead of
var all: [ConditionalHeadNode]
, a dictionary might be better to store the nodes.
I hope this implementation is better then previous one.
Cheers, Zhijin
why isn't this PR merged ?
I hope this PR will be merged soon.
I'm looking forward to this great feature! When can this PR be merged?
So there's no implemented way of adding a Script tag at this moment?
I was looking for a solution to this as well, but I'm thinking it would be nice to have it as a plugin or additional publishing step. My reason for this is that I'd like to easily switch it on/off depending on whether I'm deploying for production or test.
@Ze0nC My approach to this problem is in PR-120. It's working with raw HTML, so not type-safe in any way, but allows someone to easily make a GoogleAnalytics plugin.