From c117e38899a7e122fb4dee87f5d091e654e0939f Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sat, 2 Apr 2022 20:06:31 -0700
Subject: [PATCH] feat: wikilinks implementation

---
 assets/js/popover.js                        |  1 +
 content/notes/CJK + Latex Support (测试).md |  8 +++++-
 layouts/_default/single.html                |  2 +-
 layouts/index.html                          |  2 +-
 layouts/partials/textprocessing.html        | 31 +++++++++++++++++++++
 5 files changed, 41 insertions(+), 3 deletions(-)
 create mode 100644 layouts/partials/textprocessing.html

diff --git a/assets/js/popover.js b/assets/js/popover.js
index 3d0d18b..83cdc03 100644
--- a/assets/js/popover.js
+++ b/assets/js/popover.js
@@ -11,6 +11,7 @@ function initPopover(baseURL) {
     fetchData.then(({ content }) => {
       const links = [...document.getElementsByClassName("internal-link")]
       links.forEach(li => {
+        console.log(li.dataset)
         const linkDest = content[li.dataset.src.replace(basePath, "")]
         if (linkDest) {
           const popoverElement = `<div class="popover">
diff --git a/content/notes/CJK + Latex Support (测试).md b/content/notes/CJK + Latex Support (测试).md
index 5dc0b48..e4692d7 100644
--- a/content/notes/CJK + Latex Support (测试).md	
+++ b/content/notes/CJK + Latex Support (测试).md	
@@ -38,4 +38,10 @@ a & b & c
 $$
 
 ## RTL
-More information on configuring RTL languages like Arabic in the [config](notes/config.md) page
\ No newline at end of file
+More information on configuring RTL languages like Arabic in the [config](notes/config.md) page
+
+[[notes/troubleshooting]]
+
+[[notes/editing#Folder Structure|edit]]
+
+[[asdfasdf|todo]]
\ No newline at end of file
diff --git a/layouts/_default/single.html b/layouts/_default/single.html
index 21e7066..c09f2dc 100644
--- a/layouts/_default/single.html
+++ b/layouts/_default/single.html
@@ -28,7 +28,7 @@
           {{ .TableOfContents }}
       </aside>
       {{end}}
-      {{.Content | safeHTML}}
+      {{partial "textprocessing.html" . }}
     </article>
     {{partial "footer.html" .}}
     {{partial "popover.html" .}}
diff --git a/layouts/index.html b/layouts/index.html
index 75b24bf..d449c27 100644
--- a/layouts/index.html
+++ b/layouts/index.html
@@ -19,7 +19,7 @@
             {{ .TableOfContents }}
         </aside>
         {{end}}
-        {{.Content | safeHTML}}
+        {{partial "textprocessing.html" . }}
     </article>
     {{partial "footer.html" .}}
     {{partial "popover.html" .}}
diff --git a/layouts/partials/textprocessing.html b/layouts/partials/textprocessing.html
new file mode 100644
index 0000000..8e4a794
--- /dev/null
+++ b/layouts/partials/textprocessing.html
@@ -0,0 +1,31 @@
+{{ $content := .Content }}
+{{ $page := .Page }}
+
+{{/* Replace right arrow */}}
+{{ $content = replace $content "-&gt;" "→" }}
+
+{{/* Escape slashes for Latex to fix line breaks */}}
+{{ $content = replaceRE "\\\\ *\n" "\\\\" $content }}
+
+{{/* Wikilinks */}}
+{{$wikilinks := $content | findRE "\\[\\[[^\\[\\]\\|]*(?:\\|[^\\[\\]]*)?\\]\\]" }}
+{{range $wikilinks}}
+  {{$inner := . | strings.TrimPrefix "[[" | strings.TrimSuffix "]]" }}
+  {{$split := split $inner "|"}}
+  {{$path := index $split 0}}
+  {{$reference := split $path "#"}}
+  {{$title := index $reference 0}}
+  {{$block := default "" (index $reference 1)}}
+  {{$block = strings.TrimRight "/" (cond (eq $block "") $block (printf "#%s" $block))}}
+  {{$href := strings.TrimRight "/" ($page.GetPage $title).RelPermalink}}
+  {{$display := default $title (index $split 1)}}
+  {{if not $href}}
+    {{$link := printf "<a class=\"internal-link broken\">%s</a>" $display}}
+    {{ $content = replace $content . $link }}
+  {{else}}
+    {{$fullhref := printf "%s%s" $href $block }}
+    {{$link := printf "<a href=\"%s\" rel=\"noopener\" class=\"internal-link\" data-src=\"%s\">%s</a>" $fullhref $href $display}}
+    {{ $content = replace $content . $link }}
+  {{end}}
+{{end}}
+{{ $content | safeHTML }}