Skip to content

Commit

Permalink
Fix links in search improve indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
cmahnke committed Dec 31, 2024
1 parent e5ba80a commit ea92c57
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
81 changes: 38 additions & 43 deletions layouts/partials/index-doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,41 @@
"tags": {{- $tags | jsonify -}},
{{- end -}}

{{- $dir := split (string (path.Dir .Path)) "/" -}}
{{- $dir := substr (string (path.Dir .Path)) 1 -}}
{{- $dir = split $dir "/" -}}

{{- $displayName := "" -}}
{{- $postLink := "" -}}
{{- $linkPrefix := "" -}}
{{- $prefix := index $dir 1 -}}
{{- if ne $prefix "" -}}
"blog": {{- $prefix | jsonify -}},
{{- end -}}
{{- $sectionHeading := printf "/%s" (delimit (slice "blog" $prefix) "/") -}}
{{- $postLink := split (string .Path) "/" -}}
{{- $linkPrefix := "" -}}
{{- $external := true -}}
{{- if and (ne $prefix nil) (ne (index $dir 0) "blog") -}}
{{- if not (fileExists $sectionHeading) -}}
{{- $external = false -}}
{{- else -}}
{{- $blogPage := .Site.GetPage $sectionHeading -}}
{{/* This is a workaround for a Hugo bug regarding dots in path names */}}
{{- if and (in $prefix ".") (eq (printf "%T" $blogPage) "*page.nopPage") -}}
{{- $blogPage = .Site.GetPage (printf "%s/index.md" $sectionHeading) -}}
{{- end -}}
{{- if eq (printf "%T" $blogPage) "*page.nopPage" -}}
{{- $blogPage = .Site.GetPage "/blog/projektemacher/index.md" -}}
{{- $sectionHeading = "" -}}
{{- $prefix = "projektemacher" -}}
{{- end -}}
{{- if ($blogPage.Param "linkPrefix") -}}
{{ $linkPrefix = $blogPage.Param "linkPrefix" -}}
{{- else -}}
{{ $linkPrefix = $blogPage.Param "link" -}}
{{- end -}}
{{- end -}}
{{ else }}
{{- $external = false -}}
{{- $blogPage := .Site.GetPage $sectionHeading -}}
{{- if eq (printf "%T" $blogPage) "*page.nopPage" -}}
{{- $blogPage = .Site.GetPage "/blog/projektemacher/index.md" -}}
{{- $sectionHeading = "" -}}
{{- $prefix = "projektemacher" -}}
{{- end -}}

{{- if $external -}}
{{- $displayName = printf "%s: " $blogPage.Title -}}
{{- if ($blogPage.Param "linkPrefix") -}}
{{ $linkPrefix = $blogPage.Param "linkPrefix" -}}
{{- else -}}
{{ $linkPrefix = $blogPage.Param "link" -}}
{{- end -}}
{{- $postLink = split (substr (string .Path) 1) "/" -}}
{{/* TODO: Check why this is different from the archive */}}
{{- if or (eq (len $dir) 2) (eq (len $dir) 1) -}}
{{- $postLink = last (sub (len $postLink) 0) $postLink -}}
{{ else }}
{{- $postLink = last (sub (len $postLink) 2) $postLink -}}
{{- $postLink = delimit $postLink "/" -}}
{{- $postLink = replaceRE "^(.*/)\\w*\\..*$" "$1" $postLink -}}

{{/* If pages aren't contained in directories, rewrite them to directories */}}
{{- $postLink = replaceRE "[\\.den]{0,3}.md$" "/" $postLink -}}

{{- if in .Permalink (printf "/%s/" .Language.Lang) -}}
{{- $postLink = printf "%s/%s" .Language.Lang $postLink -}}
{{- end -}}
{{- end -}}
{{- $postLink = delimit $postLink "/" -}}
{{- $postLink = replaceRE "^(.*/)\\w*\\..*$" "$1" $postLink -}}
{{- $postLink = printf "%s%s" $linkPrefix $postLink -}}
{{/* This is a hack, empty $linkPrefix means this site */}}
{{- if eq $linkPrefix nil -}}
{{- $postLink = .Permalink -}}

{{- $postLink = printf "%s%s" $linkPrefix $postLink -}}
{{ else }}
{{- $postLink = .Permalink -}}
{{- end -}}

"url": "{{ $postLink }}",
Expand Down Expand Up @@ -100,5 +87,13 @@
{{- end -}}

{{/* replace "\n" " " | */}}
"content": {{- (trim $content " \n") | jsonify -}}
{{ $skip := false }}
{{- if and (isset .Params "archive") (eq .Params.archive false) -}}
{{ $skip = true }}
{{- else if and (isset .Params "private") (eq .Params.private true) -}}
{{ $skip = true }}
{{- end -}}
{{- if not $skip -}}
"content": {{- (trim $content " \n") | jsonify -}}
{{- end -}}
}
6 changes: 5 additions & 1 deletion scripts/build-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ fg([`${siteDir}/**/index.json`]).then((res) => {
docs = [];
res.forEach(function(file) {
console.log('Found ' + file + ', indexing');
docs.push(JSON.parse(fs.readFileSync(file, 'utf8')))
try {
docs.push(JSON.parse(fs.readFileSync(file, 'utf8')))
} catch (e) {
console.warn(`Failed to index ${file}`, e)
}
});

//}
Expand Down

0 comments on commit ea92c57

Please sign in to comment.