Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate search index without configuring ouputs. #587

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/search/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- partial "search/index.json" . -}}
3 changes: 2 additions & 1 deletion assets/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare global {
fuseOptions: any;
searchResultContentWordCount: number;
searchPaginate: number;
searchIndex: string;
}
}

Expand Down Expand Up @@ -93,7 +94,7 @@ export class Search {
}
};
xhr.responseType = 'json';
xhr.open('GET', document.head.querySelector('meta[data-name="search-index"]').getAttribute('content'), true);
xhr.open('GET', window.searchIndex, true);
xhr.send(null);
}

Expand Down
2 changes: 1 addition & 1 deletion exampleSite/config/_default/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pygmentsUseClasses = true
mediaType = "application/manifest+json"

[outputs]
home = ["HTML", "RSS", "JSON", "MANIFEST"]
home = ["HTML", "RSS", "MANIFEST"]

[taxonomies]
category = "categories"
Expand Down
12 changes: 1 addition & 11 deletions exampleSite/content/docs/layouts/search/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,11 @@ It may be difficult for readers to retrieve an article in the case if there are

## Prerequisites

First of all, we need to append `JSON` to the `home` outputs in the configuration file.

```toml
[outputs]
home = ["HTML", "RSS", "JSON"]
```

> This step generates an index file `index.json`, in order to retrieve content by JavaScript.

Secondary, create a page called `search/_index.md` in the `content` directory.
All you need to do is create a page called `search/_index.md` in the `content` directory.

```toml
+++
title = "Search"
layout = "search"
+++
```

Expand Down
12 changes: 1 addition & 11 deletions exampleSite/content/docs/layouts/search/index.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,11 @@ aliases = [

## 前置条件

首先,我们需要在配置文件的 `home` 输出格式中追加 `JSON`

```toml
[outputs]
home = ["HTML", "RSS", "JSON"]
```

> 此步骤生成一个索引文件 `index.json`,以便 JavaScript 可以检索内容。
然后,在 `content` 目录下创建一个名为 `search/_index.md` 的页面。
你只需要在 `content` 目录下创建一个名为 `search/_index.md` 的页面。

```toml
+++
title = "Search"
layout = "search"
+++
```

Expand Down
11 changes: 4 additions & 7 deletions exampleSite/content/docs/layouts/search/index.zh-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ aliases = [

## 前置條件

首先,我們需要在設定檔的 `home` 輸出格式中追加 `JSON`
你只需要在 `content` 目錄下創建一個名為 `search/_ index.md` 的頁面

```toml
[outputs]
home = ["HTML", "RSS", "JSON"]
+++
title = "Search"
+++
```

> 此步驟生成一個索引檔案 `index.json`,以便 JavaScript 可以檢索內容。

然後,在 `content` 目錄下創建一個名為 `search/_ index.md` 的頁面。

## 網站參數

| 名稱 | 類型 | 預設值 | 描述
Expand Down
1 change: 0 additions & 1 deletion exampleSite/content/search/_index.fr.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
+++
title = "Recherche"
layout = "search"
+++
1 change: 0 additions & 1 deletion exampleSite/content/search/_index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
+++
title = "Search"
layout = "search"
+++
1 change: 0 additions & 1 deletion exampleSite/content/search/_index.zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
+++
title = "搜索"
layout = "search"
+++
1 change: 0 additions & 1 deletion exampleSite/content/search/_index.zh-tw.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
+++
title = "搜索"
layout = "search"
+++
29 changes: 1 addition & 28 deletions layouts/index.json
Original file line number Diff line number Diff line change
@@ -1,28 +1 @@
{{- $.Scratch.Add "index" slice -}}
{{- range .Site.RegularPages -}}
{{- $date := .Date.Format $.Site.Params.dateFormat -}}
{{- $title := .Title }}
{{- if $.Site.Params.titleCase -}}
{{- $title = title $title -}}
{{- end -}}
{{- $img := "" -}}
{{- $smallImg := "" -}}
{{- $largeImg := "" -}}
{{- if .Params.Images -}}
{{- $img = index .Params.Images 0 | absURL -}}
{{- else -}}
{{- $images := .Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*feature*" -}}
{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}
{{- with $featured -}}
{{- $smallImage := $featured.Resize "180x" -}}
{{- $smallImg = $smallImage.Permalink -}}
{{- $largeImage := $featured.Resize "500x" -}}
{{- $largeImg = $largeImage.Permalink -}}
{{- $img = $featured.Permalink -}}
{{- end -}}
{{- end -}}
{{- $item := (dict "title" $title "tags" .Params.tags "categories" .Params.categories "series" .Params.series "content" .Plain "permalink" .Permalink "date" $date "img" $img "smallImg" $smallImg "largeImg" $largeImg) -}}
{{- $.Scratch.Add "index" $item -}}
{{- end -}}
{{- $.Scratch.Get "index" | uniq | jsonify -}}
{{- partial "search/index.json" . -}}
10 changes: 9 additions & 1 deletion layouts/partials/assets/search/js.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if eq .Layout "search" -}}
{{- if eq .Type "search" -}}
{{- $titleKey := dict "name" "title" "weight" 0.8 -}}
{{- $contentKey := dict "name" "content" "weight" 0.5 -}}
{{- $tagsKey := dict "name" "tags" "weight" 0.3 -}}
Expand All @@ -17,12 +17,20 @@
{{- end -}}
{{- end -}}

{{- $indexURL := relURL "/search/index.json" -}}
{{- if gt (len $.Site.Home.AllTranslations) 1 -}}
{{- $indexURL = relLangURL "/search/index.json" -}}
{{- end -}}
{{- $index := resources.Get "search/index.json" | resources.ExecuteAsTemplate $indexURL . -}}

<script>
window.searchResultContentWordCount = parseInt('{{ default 240 .Site.Params.search.resultContentWordCount }}');
window.searchPaginate = parseInt('{{ default 10 .Site.Params.search.paginate }}');
window.fuseOptions = JSON.parse('{{ $options | jsonify | safeHTML }}');
window.searchIndex = '{{ $index.Permalink }}';
</script>
{{- $options := dict "targetPath" "assets/search/bundle.min.js" "minify" true -}}
{{- $script := resources.Get "search/index.ts" | js.Build $options | fingerprint -}}
<script src="{{ $script.Permalink }}" integrity="{{ $script.Data.Integrity }}" crossorigin="anonymous"></script>

{{- end -}}
3 changes: 0 additions & 3 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@
{{- partial "head/meta" . -}}
{{- partial "head/feed" . -}}
{{- partial "head/site-verification" . -}}
{{- if or (eq .Permalink (absURL "search/")) (eq .Permalink (absLangURL "search/")) -}}
<meta data-name="search-index" content="{{ if gt (len $.Site.Home.AllTranslations) 1 }}{{ absLangURL "/index.json" }}{{ else }}{{ absURL "/index.json" }}{{ end }}">
{{- end -}}
{{- partial "head/assets" . -}}
28 changes: 28 additions & 0 deletions layouts/partials/search/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{- $.Scratch.Add "index" slice -}}
{{- range .Site.RegularPages -}}
{{- $date := .Date.Format $.Site.Params.dateFormat -}}
{{- $title := .Title }}
{{- if $.Site.Params.titleCase -}}
{{- $title = title $title -}}
{{- end -}}
{{- $img := "" -}}
{{- $smallImg := "" -}}
{{- $largeImg := "" -}}
{{- if .Params.Images -}}
{{- $img = index .Params.Images 0 | absURL -}}
{{- else -}}
{{- $images := .Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*feature*" -}}
{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}
{{- with $featured -}}
{{- $smallImage := $featured.Resize "180x" -}}
{{- $smallImg = $smallImage.Permalink -}}
{{- $largeImage := $featured.Resize "500x" -}}
{{- $largeImg = $largeImage.Permalink -}}
{{- $img = $featured.Permalink -}}
{{- end -}}
{{- end -}}
{{- $item := (dict "title" $title "tags" .Params.tags "categories" .Params.categories "series" .Params.series "content" .Plain "permalink" .Permalink "date" $date "img" $img "smallImg" $smallImg "largeImg" $largeImg) -}}
{{- $.Scratch.Add "index" $item -}}
{{- end -}}
{{- $.Scratch.Get "index" | uniq | jsonify -}}