InkSoul/themes/meme_cdn/layouts/partials/utils/content.html

188 lines
9.1 KiB
HTML

{{- $Content := partial "utils/markdownify.html" (dict "$" . "raw" .Content "isContent" true) -}}
<!-- Link Headings to TOC -->
{{- $enableTOC := .Params.toc | default .Site.Params.enableTOC -}}
{{- if and $enableTOC .Site.Params.linkHeadingsToTOC -}}
{{- $regexPatternLinkHeadings := `(<h[1-6] id="([^"]+)">)(.+)?(</h[1-6]+>)` -}}
{{- $regexReplacementLinkHeadings := `$1<a href="#contents:$2" class="headings">$3</a>$4` -}}
{{- $Content = $Content | replaceRE $regexPatternLinkHeadings $regexReplacementLinkHeadings -}}
{{- end -}}
<!-- Headings Anchor -->
{{- if .Params.anchor | default .Site.Params.enableHeadingsAnchor -}}
{{- with .Site.Params.headingsOpt -}}
{{- $headings := . -}}
{{- $.Scratch.Set "headings" $headings -}}
{{- end -}}
{{- $headings := .Scratch.Get "headings" -}}
{{- $headings := $headings | default "1-6" -}}
{{- with .Site.Params.anchorIcon -}}
{{- $icon := (replace (index $.Site.Data.SVG .) "icon" "icon anchor-icon") -}}
{{- $.Scratch.Set "icon" $icon -}}
{{- end -}}
{{- $icon := .Scratch.Get "icon" -}}
{{- $anchor := $icon | default .Site.Params.anchorSymbol | default "§" -}}
{{- if .Site.Params.enableAnchorLink -}}
{{- $replacement := (printf `$1<a href="#$2" class="anchor-link">%s</a>$3$4` $anchor) -}}
{{- $.Scratch.Set "replacement" $replacement -}}
{{- else -}}
{{- $replacement := (printf `$1<span class="anchor-link">%s</span>$3$4` $anchor) -}}
{{- $.Scratch.Set "replacement" $replacement -}}
{{- end -}}
{{- $replacement := .Scratch.Get "replacement" -}}
{{- $regexPatternHeadingsAnchor := (printf `(<h[%s] id="([^"]+)">)(.+)?(</h[%s]+>)` $headings $headings) -}}
{{- $regexReplacementHeadingsAnchor := $replacement -}}
{{- $Content = $Content | replaceRE $regexPatternHeadingsAnchor $regexReplacementHeadingsAnchor -}}
{{- end -}}
<!-- Drop Cap -->
{{- if eq .Type "poetry" -}}
{{- $enableDropCap := .Params.dropCap | default false -}}
{{- .Scratch.Set "enableDropCap" $enableDropCap -}}
{{- else -}}
{{- $enableDropCap := .Params.dropCap | default .Site.Params.enableDropCap -}}
{{- .Scratch.Set "enableDropCap" $enableDropCap -}}
{{- end -}}
{{- $enableDropCap := .Scratch.Get "enableDropCap" -}}
{{- if $enableDropCap -}}
{{- $regexPatternDropCap := `(<p)(>)([^<])(.+(</p>|\n))` -}}
{{- $regexReplacementDropCap := `$1 style="text-indent:0"$2<span class="drop-cap">$3</span>$4` -}}
{{- $firstParagraphOld := (delimit (findRE $regexPatternDropCap $Content 1) " ") -}}
{{- $firstParagraphNew := (replaceRE $regexPatternDropCap $regexReplacementDropCap $firstParagraphOld) -}}
{{- $Content = replace $Content $firstParagraphOld $firstParagraphNew 1 -}}
{{- end -}}
<!-- Drop Cap After `<hr />` -->
{{- $enableDropCapAfterHr := .Params.dropCapAfterHr | default .Site.Params.enableDropCapAfterHr -}}
{{- if ne .Type "poetry" -}}
{{- if $enableDropCapAfterHr -}}
{{- if .Params.deleteHrBeforeDropCap | default .Site.Params.deleteHrBeforeDropCap -}}
{{- $replacement := `$3 style="text-indent:0"$4<span class="drop-cap">$5</span>` -}}
{{- $.Scratch.Set "replacement" $replacement -}}
{{- else -}}
{{- $replacement := `$1$3 style="text-indent:0"$4<span class="drop-cap">$5</span>` -}}
{{- $.Scratch.Set "replacement" $replacement -}}
{{- end -}}
{{- $replacement := .Scratch.Get "replacement" -}}
{{- $regexPatternDropCapAfterHr := `(\n(<hr />|<hr>))(\n<p)(>)([^<])` -}}
{{- $regexReplacementDropCapAfterHr := $replacement -}}
{{- $Content = $Content | replaceRE $regexPatternDropCapAfterHr $regexReplacementDropCapAfterHr -}}
{{- end -}}
{{- end -}}
<!-- Footnote Ref with Square Brackets `[]` -->
{{- if .Site.Params.squareBrackets -}}
{{- $regexPatternFootnoteRef := `(<sup id="fnref:\d+"><a href="#fn:\d+"[^>]+>)(\d+)(</a></sup>)` -}}
{{- $regexReplacementFootnoteRef := `$1[$2]$3` -}}
{{- $Content = $Content | replaceRE $regexPatternFootnoteRef $regexReplacementFootnoteRef -}}
{{- end -}}
<!-- Delete Footnote `<hr />` -->
{{- if .Site.Params.insertHrBySelf -}}
{{- $regexPatternDeleteHr := `(<hr />|<hr>)\n(<(section|div) class="footnotes" role="doc-endnotes">)` -}}
{{- $regexReplacementDeleteHr := `$2` -}}
{{- $Content = $Content | replaceRE $regexPatternDeleteHr $regexReplacementDeleteHr -}}
{{- end -}}
<!-- Replace `footnoteReturnLinkContents` with icon -->
{{- with .Site.Params.footnoteReturnLinkIcon -}}
{{- $icon := (partial "utils/icon.html" (dict "$" $ "name" . "class" "footnote-icon")) -}}
{{- $replacement := (printf `${1}%s$3` $icon) -}}
{{- $regexPatternfootnoteReturnLinkIcon := `(href="#fnref[^>]+>)([^<]+)(.+)` -}}
{{- $regexReplacementfootnoteReturnLinkIcon := $replacement -}}
{{- $Content = $Content | replaceRE $regexPatternfootnoteReturnLinkIcon $regexReplacementfootnoteReturnLinkIcon -}}
{{- end -}}
<!-- Image & Video Footnote -->
{{- $regexPatternFootnoteImage := `(<sup)(.+</sup><(img|video))` -}}
{{- $regexReplacementFootnoteImage := `$1 style="float:right"$2` -}}
{{- $Content = $Content | replaceRE $regexPatternFootnoteImage $regexReplacementFootnoteImage -}}
<!-- Image & Video Caption -->
{{- if .Site.Params.enableCaption -}}
{{- $captionPrefix := .Site.Params.captionPrefix -}}
{{- $regexPatternCaption := `(<(img|video).+) title="([^"]+)"( controls)?( />|>)(</video>)?` -}}
{{- $regexReplacementCaption := (printf `$1$4$5$6<span class="caption">%s$3</span>` $captionPrefix) -}}
{{- $Content = $Content | replaceRE $regexPatternCaption $regexReplacementCaption -}}
{{- end -}}
<!-- Image Hosting -->
{{- if and .Site.Params.enableImageHost (eq hugo.Environment "production") -}}
{{- $hostURL := strings.TrimSuffix "/" .Site.Params.imageHostURL -}}
{{- $temps := findRE `<(img) src="/?([^":]+)` $Content | uniq -}}
{{- with $temps -}}
{{- range . -}}
{{- if not (in (slice "http" "ttps") (substr . -1 4)) -}}
{{- $url := replaceRE `<(img) src="/?([^":]+)` `$2` . -}}
{{- $prefix := replaceRE `(<(img) src=")/?([^":]+)` `$1` . -}}
{{- $replacement := (printf `%s%s/%s` $prefix $hostURL $url) -}}
{{- $Content = replace $Content . $replacement -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
<!-- Video Hosting -->
{{- if and .Site.Params.enableVideoHost (eq hugo.Environment "production") -}}
{{- $hostURL := strings.TrimSuffix "/" .Site.Params.videoHostURL -}}
{{- $temps := findRE `<(video) src="/?([^":]+)` $Content | uniq -}}
{{- with $temps -}}
{{- range . -}}
{{- if not (in (slice "http" "ttps") (substr . -1 4)) -}}
{{- $url := replaceRE `<(video) src="/?([^":]+)` `$2` . -}}
{{- $prefix := replaceRE `(<(video) src=")/?([^":]+)` `$1` . -}}
{{- $replacement := (printf `%s%s/%s` $prefix $hostURL $url) -}}
{{- $Content = replace $Content . $replacement -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
<!-- Paragraph Indent -->
{{- $enableIndent := (and .Site.Params.enableParagraphIndent .Params.indent) | default (and .Site.Params.enableParagraphIndent (eq .Site.Params.paragraphStyle "indent")) -}}
{{- if ne .Type "poetry" -}}
{{- if $enableIndent -}}
{{- $regexPatternIndent := `((</p>|<blockquote>)\n<p)(>)(.+(<br />|<br>))` -}}
{{- $regexReplacementIndent := `$1 style="text-indent:0;padding-left:2em;margin:1em 0"$3$4` -}}
{{- $Content = $Content | replaceRE $regexPatternIndent $regexReplacementIndent -}}
{{- end -}}
{{- end -}}
<!-- Do NOT Indent The First Paragraph -->
{{- if and (not (.Params.indentFirstParagraph | default .Site.Params.indentFirstParagraph)) $enableIndent -}}
{{- if ne .Type "poetry" -}}
{{- if not $enableDropCap -}}
{{- $regex := `(<p)(>[^<]+)` -}}
{{- $replacement := `$1 style="text-indent:0"$2` -}}
{{- $firstParagraphOld := (delimit (findRE $regex $Content 1) " ") -}}
{{- $firstParagraphNew := (replaceRE $regex $replacement $firstParagraphOld) -}}
{{- $Content = replace $Content $firstParagraphOld $firstParagraphNew -}}
{{- end -}}
{{- if not $enableDropCapAfterHr -}}
{{- $regex := `((</h[1-6]>|<hr>|<hr />)\n(<blockquote>\n)?<p)(>[^<])` -}}
{{- $.Scratch.Set "regex" $regex -}}
{{- else -}}
{{- $regex := `((</h[1-6]>)\n(<blockquote>\n)?<p)(>[^<])` -}}
{{- $.Scratch.Set "regex" $regex -}}
{{- end -}}
{{- $regex := .Scratch.Get "regex" -}}
{{- $replacement := `$1 style="text-indent:0"$4` -}}
{{- $Content = $Content | replaceRE $regex $replacement -}}
{{- end -}}
{{- end -}}
<!-- Responsive tables -->
{{- $Content = $Content | replaceRE `<table\b` `<div class="table-container"><table` -}}
{{- $Content = $Content | replaceRE `</table>` `</table></div>` -}}
<!-- Custom -->
{{- $Content = partial "custom/content.html" (dict "$" $ "Content" $Content) | default $Content -}}
<!-- Final Content -->
{{- $Content | safeHTML -}}