Skip to content

Commit

Permalink
Merge pull request #613 from kaushalmodi/remove-style-element-injection
Browse files Browse the repository at this point in the history
fix!: html validation: Do not export <style> elements above TOC
  • Loading branch information
kaushalmodi committed Apr 6, 2022
2 parents a0d1c8e + 35d9552 commit 2f37d27
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 67 deletions.
11 changes: 11 additions & 0 deletions doc/ox-hugo-manual.org
Expand Up @@ -1803,6 +1803,17 @@ tries to get the "summary you mean". This partial is used by the
/Note that you would need to use the =summary_minus_toc.html= partial
wherever you do not intend to have TOC included in the summary (for
example, in the Opengraph =og:description= meta tag)./
**** Hiding bullets when TOC has numbered headings
Add this to the CSS to hide bullets in table of contents when the
headings are numbered. Export options set like ~#+options: toc:t
num:t~ will cause this.

#+begin_src css
/* Hide bullets in TOC when headings are numbered. */
.toc.has-section-numbers ul {
list-style: none;
}
#+end_src
*** Table Styling
:PROPERTIES:
:EXPORT_FILE_NAME: table-styling
Expand Down
59 changes: 28 additions & 31 deletions ox-hugo.el
Expand Up @@ -1048,37 +1048,34 @@ contents according to the current heading."
(replace-regexp-in-string "\n\\{2,\\}" "\n" toc-items))))
;; (message "[ox-hugo build-toc DBG] toc-items:%s" toc-items)
(when toc-items
(concat (when (string-match-p "^\\s-*\\-\\s-<span class=\"section\\-num\"" toc-items)
;; Hide the bullets if section numbers are present for
;; even one heading.
(concat "<style>\n"
" .ox-hugo-toc ul {\n"
" list-style: none;\n"
" }\n"
"</style>\n"))
(format "<div class=\"ox-hugo-toc toc%s\">\n"
(if local
" local"
""))
(unless (org-hugo--plist-get-true-p info :hugo-goldmark)
"<div></div>\n") ;This is a nasty workaround till Hugo/Blackfriday support
toc-heading ;wrapping Markdown in HTML div's.
"\n"
toc-items ;https://github.com/kaushalmodi/ox-hugo/issues/93
"\n\n"
"</div>\n"
;; Special comment that can be use to filter out the TOC
;; from .Summary in Hugo templates.
;;
;; {{ $summary_splits := split .Summary "<!--endtoc-->" }}
;; {{ if eq (len $summary_splits) 2 }}
;; <!-- If that endtoc special comment is present, output only the part after that comment as Summary. -->
;; {{ index $summary_splits 1 | safeHTML }}
;; {{ else }}
;; <!-- Print the whole Summary if endtoc special comment is not found. -->
;; {{ .Summary }}
;; {{ end }}
"<!--endtoc-->\n"))))
(let ((toc-classes '("toc" "ox-hugo-toc"))
;; `has-section-numbers' is non-nil if section numbers are
;; present for even one heading.
(has-section-numbers (string-match-p "^\\s-*\\-\\s-<span class=\"section\\-num\"" toc-items)))
(when has-section-numbers
(push "has-section-numbers" toc-classes))
(when local
(push "local" toc-classes))
(concat (format "<div class=\"%s\">\n" (string-join (reverse toc-classes) " "))
(unless (org-hugo--plist-get-true-p info :hugo-goldmark)
"<div></div>\n") ;This is a nasty workaround till Hugo/Blackfriday support
toc-heading ;wrapping Markdown in HTML div's.
"\n"
toc-items ;https://github.com/kaushalmodi/ox-hugo/issues/93
"\n\n"
"</div>\n"
;; Special comment that can be use to filter out the TOC
;; from .Summary in Hugo templates.
;;
;; {{ $summary_splits := split .Summary "<!--endtoc-->" }}
;; {{ if eq (len $summary_splits) 2 }}
;; <!-- If that endtoc special comment is present, output only the part after that comment as Summary. -->
;; {{ index $summary_splits 1 | safeHTML }}
;; {{ else }}
;; <!-- Print the whole Summary if endtoc special comment is not found. -->
;; {{ .Summary }}
;; {{ end }}
"<!--endtoc-->\n")))))

;;;; Escape Hugo shortcode
(defun org-hugo--escape-hugo-shortcode (code lang)
Expand Down
7 changes: 1 addition & 6 deletions test/site/content/dir-locals-test/dir-locals-test.md
Expand Up @@ -7,12 +7,7 @@ draft: false
creator: "Dummy creator string"
---

<style>
.ox-hugo-toc ul {
list-style: none;
}
</style>
<div class="ox-hugo-toc toc">
<div class="ox-hugo-toc toc has-section-numbers">

<div class="heading">Table of Contents</div>

Expand Down
7 changes: 1 addition & 6 deletions test/site/content/posts/link-to-headings-by-name.md
Expand Up @@ -4,12 +4,7 @@ tags = ["links", "internal-links", "toc", "headings", "export-option"]
draft = false
+++

<style>
.ox-hugo-toc ul {
list-style: none;
}
</style>
<div class="ox-hugo-toc toc">
<div class="ox-hugo-toc toc has-section-numbers">

<div class="heading">Table of Contents</div>

Expand Down
Expand Up @@ -4,12 +4,7 @@ tags = ["export-option", "toc"]
draft = false
+++

<style>
.ox-hugo-toc ul {
list-style: none;
}
</style>
<div class="ox-hugo-toc toc">
<div class="ox-hugo-toc toc has-section-numbers">

<div class="heading">Table of Contents</div>

Expand Down
Expand Up @@ -4,12 +4,7 @@ tags = ["export-option", "toc"]
draft = false
+++

<style>
.ox-hugo-toc ul {
list-style: none;
}
</style>
<div class="ox-hugo-toc toc">
<div class="ox-hugo-toc toc has-section-numbers">

<div class="heading">Table of Contents</div>

Expand Down
Expand Up @@ -4,12 +4,7 @@ tags = ["export-option", "toc"]
draft = false
+++

<style>
.ox-hugo-toc ul {
list-style: none;
}
</style>
<div class="ox-hugo-toc toc">
<div class="ox-hugo-toc toc has-section-numbers">

<div class="heading">Table of Contents</div>

Expand Down
Expand Up @@ -4,12 +4,7 @@ tags = ["export-option", "toc"]
draft = false
+++

<style>
.ox-hugo-toc ul {
list-style: none;
}
</style>
<div class="ox-hugo-toc toc">
<div class="ox-hugo-toc toc has-section-numbers">

<div class="heading">Table of Contents</div>

Expand Down

0 comments on commit 2f37d27

Please sign in to comment.