Put the highlight logic into one common template

It now loads unconditionally and highlights all code blocks.

TODO: optimize to not load when there are no blocks
pull/31/head
Marcel van der Boom 6 years ago
parent 03ac402c37
commit a82882fddc
  1. 4
      templates.go
  2. 13
      templates/collection-post.tmpl
  3. 14
      templates/collection-tags.tmpl
  4. 14
      templates/collection.tmpl
  5. 14
      templates/include/render.tmpl
  6. 17
      templates/post.tmpl

@ -55,12 +55,14 @@ func initTemplate(name string) {
filepath.Join(templatesDir, name+".tmpl"),
filepath.Join(templatesDir, "include", "posts.tmpl"),
filepath.Join(templatesDir, "include", "footer.tmpl"),
filepath.Join(templatesDir, "include", "render.tmpl"),
filepath.Join(templatesDir, "base.tmpl"),
))
} else {
templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles(
filepath.Join(templatesDir, name+".tmpl"),
filepath.Join(templatesDir, "include", "footer.tmpl"),
filepath.Join(templatesDir, "include", "render.tmpl"),
filepath.Join(templatesDir, "base.tmpl"),
))
}
@ -74,6 +76,7 @@ func initPage(path, key string) {
pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles(
path,
filepath.Join(templatesDir, "include", "footer.tmpl"),
filepath.Join(templatesDir, "include", "render.tmpl"),
filepath.Join(templatesDir, "base.tmpl"),
))
}
@ -87,6 +90,7 @@ func initUserPage(path, key string) {
path,
filepath.Join(templatesDir, "user", "include", "header.tmpl"),
filepath.Join(templatesDir, "user", "include", "footer.tmpl"),
filepath.Join(templatesDir, "user", "include", "render.tmpl"),
))
}

@ -46,17 +46,8 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>{{end}}
{{if .Collection.CodeHighlight}}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/atom-one-light.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script>
addEventListener('load', function () {
var x = document.querySelectorAll("code[class^='language-']");
for (i=0; i<x.length; i++) {
hljs.highlightBlock(x[i]);
}});
</script>
{{end}}
<!-- Add highlighting logic -->
{{template "highlighting" .}}
</head>
<body id="post">

@ -45,18 +45,8 @@
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>{{end}}
{{if .CodeHighlight}}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/atom-one-light.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script>
addEventListener('load', function () {
var x = document.querySelectorAll("code[class^='language-']");
for (i=0; i<x.length; i++) {
hljs.highlightBlock(x[i]);
}});
</script>
{{end}}
<!-- Add highlighting logic -->
{{template "highlighting" . }}
</head>
<body id="subpage">

@ -44,17 +44,9 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>{{end}}
{{if .CodeHighlight}}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/atom-one-light.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script>
addEventListener('load', function () {
var x = document.querySelectorAll("code[class^='language-']");
for (i=0; i<x.length; i++) {
hljs.highlightBlock(x[i]);
}});
</script>
{{end}}
<!-- Add highlighting logic -->
{{template "highlighting" . }}
</head>
<body id="collection" itemscope itemtype="http://schema.org/WebPage">
{{if or .IsOwner .SingleUser}}<nav id="manage"><ul>

@ -0,0 +1,14 @@
<!-- Miscelaneous render related template parts we use multiple times -->
{{define "highlighting"}}
<!-- TODO: make this conditional on presence of code blocks -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/atom-one-light.min.css"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js" type="text/javascript"></script>
<script>
addEventListener('load', function () {
var x = document.querySelectorAll("code[class^='language-']");
for (i=0; i<x.length; i++) {
hljs.highlightBlock(x[i]);
}});
</script>
{{end}}

@ -32,21 +32,8 @@
<meta property="og:image" content="https://write.as/img/w-sq-light.png">
{{if .Author}}<meta property="article:author" content="https://{{.Author}}" />{{end}}
<!-- Collection not known during single User draft? this test feels wrong -->
{{if not .SingleUser }}
{{if .Collection.CodeHighlight}}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/atom-one-light.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script>
addEventListener('load', function () {
var x = document.querySelectorAll("code[class^='language-']");
for (i=0; i<x.length; i++) {
hljs.highlightBlock(x[i]);
}});
</script>
{{end}}
{{end}}
<!-- Add highlighting logic -->
{{template "highlighting" .}}
</head>
<body id="post">

Loading…
Cancel
Save