popover implementation
parent
2b432d7f0b
commit
4a3c4fdef5
@ -1,8 +1,9 @@
|
|||||||
{{$trimmed := strings.TrimSuffix ".md" (.Destination | safeURL)}}
|
{{$trimmed := strings.TrimSuffix ".md" (.Destination | safeURL)}}
|
||||||
{{$trimmed = replace $trimmed "%20" "-" }}
|
{{$trimmed = replace $trimmed "%20" "-" }}
|
||||||
{{$external := strings.HasPrefix $trimmed "http" }}
|
{{$external := strings.HasPrefix $trimmed "http" }}
|
||||||
{{ if $external }}
|
{{- if $external -}}
|
||||||
<a href="{{ $trimmed }}" rel="noopener">{{ .Text | safeHTML }}</a>
|
<a href="{{ $trimmed }}" rel="noopener">{{ .Text | safeHTML }}</a>
|
||||||
{{else}}
|
{{- else -}}
|
||||||
<a href="{{ if (hasPrefix $trimmed "/") }}{{ $trimmed | urlize }}{{ else }}{{ print "/" $trimmed | urlize }}{{ end }}" rel="noopener">{{ .Text | safeHTML }}</a>
|
{{$fixedUrl := (cond (hasPrefix $trimmed "/") $trimmed (print "/" $trimmed)) | urlize}}
|
||||||
{{end}}
|
<a href="{{$fixedUrl}}" rel="noopener" class="internal-link" data-src="{{$fixedUrl}}">{{ .Text | safeHTML }}</a>
|
||||||
|
{{- end -}}
|
@ -0,0 +1,29 @@
|
|||||||
|
<script>
|
||||||
|
function htmlToElement(html) {
|
||||||
|
const template = document.createElement('template')
|
||||||
|
html = html.trim()
|
||||||
|
template.innerHTML = html
|
||||||
|
return template.content.firstChild
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
|
[...document.getElementsByClassName("internal-link")]
|
||||||
|
.forEach(li => {
|
||||||
|
const linkDest = content[li.dataset.src]
|
||||||
|
if (linkDest) {
|
||||||
|
const popoverElement = `<div class="popover">
|
||||||
|
<h3>${linkDest.title}</h3>
|
||||||
|
<p>${removeMarkdown(linkDest.content).split(" ", 15).join(" ")}...</p>
|
||||||
|
</div>`
|
||||||
|
const el = htmlToElement(popoverElement)
|
||||||
|
li.appendChild(el)
|
||||||
|
li.addEventListener("mouseover", () => {
|
||||||
|
el.classList.add("visible")
|
||||||
|
})
|
||||||
|
li.addEventListener("mouseout", () => {
|
||||||
|
el.classList.remove("visible")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
</script>
|
Loading…
Reference in New Issue