-
-
Notifications
You must be signed in to change notification settings - Fork 966
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
Rework anchor_sections to change default behavior #1964
Conversation
We can now do some customization easily
output:
html_document:
anchor_sections: TRUE
output:
html_document:
anchor_sections:
style: hash And we have predefined a symbol, using output:
html_document:
anchor_sections:
style: symbol and output:
html_document:
anchor_sections:
style: icon Advantage of the svg icon: it does not change as the symbol - always the same. Do you think it is useful ? Currently the implementation forces me to use several css files. I don't think it is the best, but it helps me test the API. I would have prefer to have a logic of templating but I guess that would require lua filter and pandoc template. Or ability to apply JS differently based on information in R. (I should look in bookdown maybe...) I am thinking also of adding something to easily control the level you want to apply - only level 1 for example output:
html_document:
anchor_sections:
style: icon
levels: 1 but it would definitely changing the way it works - currently it would require passing parameters from R to JS. Not sure how and if it is a good idea. Still WIP but feedbacks are welcome. |
We could support something like this output:
html_document:
anchor_sections:
style: icon
maxlevel: 1 if we revert to using a lua filter as @atusy first implementation. We could pass the value as metadata to pandoc ( local anchor_maxlevel
-- retrieve metadata
local function get_metadata(el)
anchor_maxlevel = tonumber(el.rmd_anchor_maxlevel)
if not anchor_maxlevel then
anchor_maxlevel = 6
end
end
-- insert anchor link
function insert_anchor(el)
if el.identifier ~= "" and el.level <= anchor_maxlevel then
el.classes:insert("hasAnchor")
table.insert(el.content,
pandoc.Link("", "#"..el.identifier, "", {class = "anchor-section"})
)
end
return(el)
end
return {
{ Meta = get_metadata },
{ Header = insert_anchor}
} With the current implementation in JS, I am not sure how we could modify the levels to apply to (unless building js script from a template in a temp folder)
In the first place we switch to JS implementation because we where thinking of defaulting to FALSE and requiring checking is anchors where already set (#1884 (comment)). As we default to FALSE now this would be required anymore IMO. Do you think this would be interesting to support this max level header to apply anchor on ? |
@cderv I think sass will be a nice solution.
|
@atusy thanks for your feedback. yes sass would definitely help. I just don’t want yet to introduce this dependency. But when it will be one, we could switch. About ˋanchor_maxlevel` with the solution above there is no more JS. Only a lua filter which adds the html code and css to make the styling. Do you think this would be a useful feature or only anchor_sections for all or non makes more sense ? If you have any comment as review on this PR, you’re welcome to share! |
Hi @cderv - I really like this approach, and the added flexibility of max level. It reminds me of the |
Use MIT for this part
and rewrite to avoid duplication
This regroup the logic in one place
This allows smaller tests, closer to unit test and better readibility in html_document()
Conflicts: NEWS.md
As we decided Not using JS means using a Lua filter instead. That allows us to add a feature I think will be appreciated: selected the depth on which to apply the anchor on. Feedbacks from @apreshill and @mine-cetinkaya-rundel confirmed that there is some potential use case. So a TL;DR on this PR:
IMO this PR is ready. Reviews welcome @yihui @apreshill @atusy ! |
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.
Good job! Let me add some suggestions.
BTW, you've solved two other suggestions I was about to make (rename 'maxlevel' and refactor html_document
by implementing add_anchor_secions
). Nice!!
@atusy I found one drawback with the lua filter: the I guess there is no trick for that on the lua side... With JS, you were able to add |
Yes. That is why my initial implementation did not use the Also, your implementation shows anchors when mouse hovers the section, not header. |
BTW @jooyoungseo I added |
I just come up with an idea to control The htmltools::htmlDependency(
...,
head = sprintf("<meta itemprop='anchor-sections-max-level' content='%s'>", max_level)
) Then, we can catch the value with JS by document.querySelector("meta[itemprop='anchor-sections-max-level']").content |
Oh, the <!-- Generated by the server -->
<script id="data" type="application/json">{"userId":1234,"userName":"John Doe","memberSince":"2000-01-01T00:00:00.000Z"}</script>
<!-- Static -->
<script>
const userInfo = JSON.parse(document.getElementById("data").text);
console.log("User information: %o", userInfo);
</script> https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#embedding_data_in_html |
There is also anchorjs which is used in distill already. Did not know that one so sharing it here for later reference. |
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.
@cderv This PR looked more complicated than I expected, but after another review, I think it looks good overall. I guess you meant "hash" instead of "dash". Other than that, please feel free to merge it. Thanks!
This will close #1946
This PR is a two step process
Reverting the default
First step shared in this PR is to revert the default and use FALSE instead of TRUE. Some examples are also added in
?html_document
about how to customize. The style has not changed and#
is still used. I did not modified the JS - even if we don't need to check for existing anchor I believe as it will not be activated by default.Changes:
https://github.com/rstudio/rmarkdown/pull/1964/files/f26da730418d4b62de3c72b6dd7ba0bc745a31c8
New help section:
Simplifying customization
Second step is experimenting with more helping way of customization. I believe we can make the choice easier for users who wants an icon instead of the dash. I have something like that in mind
I don't think this would be difficult to maintain, and it would be a lot easier for helping non advanced user (who don't know CSS) to customize there outputs.
It is in progress hence the draft PR.
/cc @atusy @apreshill @yihui