Use JSON file to replace raw string in DOM (#147)

* chore(Geist): bump geist font to v1.0.1

* refactor(_typography.scss): split the CSS lang tag to ensure compatibility with Chromium-based browsers

* refactor(stage.ts): bump up preload count on desktop

* feat(params.toml): add support for user defined resize option

* feat(_fonts.scss): add a new font file to better support fullwidth plus and minus

* refactor(info): update info

* chore(build.yml): update branch name for updating bundled artifacts to include the current date

* chore(package.json): update version from 0.0.1 to v0.0.2 to reflect changes made in the codebase

* feat: refactor initialization process for retrieving and sorting image data

- Modify `main.ts` to use async initialization of resources
- Change the return type of `initResources` function to `Promise<ImageJSON[]>`
- Add try-catch block to handle errors in `initResources` function
- Use fetch API to retrieve image data from `index.json`
- Sort the image data based on the index field

* refactor: update file naming and variable assignments in layouts

- Add new file `exampleSite/config/_default/outputs.toml` with the contents `page = ["HTML", "JSON"]`
- Rename file `layouts/partials/resources/imageJSON.html` to `layouts/_default/single.json`
- Modify variable assignments and references in the `single.json` layou

* chore: remove code that includes imageJSON.html partials in layouts

- Remove the code that includes the "resources/imageJSON.html" partial in the 404.html layout
- Remove the code that includes the "resources/imageJSON.html" partial in the single.html layout
This commit is contained in:
Spedon
2023-11-13 20:51:08 +08:00
committed by GitHub
parent b751623530
commit 9b08d255f1
7 changed files with 26 additions and 15 deletions

View File

@@ -3,3 +3,4 @@ static
exmapleSite exmapleSite
*.yaml *.yaml
*.yml *.yml
single.json

View File

@@ -5,7 +5,7 @@ import { initState } from './state'
import { isMobile } from './utils' import { isMobile } from './utils'
initContainer() initContainer()
const ijs = initResources() const ijs = await initResources()
initState(ijs.length) initState(ijs.length)
initNav() initNav()

View File

@@ -10,17 +10,21 @@ export interface ImageJSON {
hiImgW: number hiImgW: number
} }
export function initResources(): ImageJSON[] { export async function initResources(): Promise<ImageJSON[]> {
const imagesJson = document.getElementById('imagesSource') try {
if (imagesJson === null) { const response = await fetch(`${window.location.href}index.json`, {
return [] headers: {
} Accept: 'application/json'
return JSON.parse(imagesJson.textContent as string).sort( }
(a: ImageJSON, b: ImageJSON) => { })
const data: ImageJSON[] = await response.json()
return data.sort((a: ImageJSON, b: ImageJSON) => {
if (a.index < b.index) { if (a.index < b.index) {
return -1 return -1
} }
return 1 return 1
} })
) } catch (_) {
return []
}
} }

View File

@@ -0,0 +1 @@
page = ["HTML", "JSON"]

View File

@@ -2,7 +2,6 @@
{{- $params := .Scratch.Get "params" -}} {{- $params := .Scratch.Get "params" -}}
{{- $currentPage := . -}} {{- $currentPage := . -}}
{{- with partial "function/currentMenuItem.html" . -}} {{- with partial "function/currentMenuItem.html" . -}}
{{- partial "resources/imageJSON.html" (dict "Path" .DirName "Page" $currentPage) -}}
<script>document.getElementById("main").setAttribute("currentMenuItemIndex", "{{- (sub .ID 1) -}}")</script> <script>document.getElementById("main").setAttribute("currentMenuItemIndex", "{{- (sub .ID 1) -}}")</script>
{{- end -}} {{- end -}}
<div class="container"> <div class="container">

View File

@@ -2,7 +2,6 @@
{{- $params := .Scratch.Get "params" -}} {{- $params := .Scratch.Get "params" -}}
{{- $currentPage := . -}} {{- $currentPage := . -}}
{{- with partial "function/currentMenuItem.html" . -}} {{- with partial "function/currentMenuItem.html" . -}}
{{- partial "resources/imageJSON.html" (dict "Path" .DirName "Page" $currentPage) -}}
<script> <script>
document.getElementById("main").setAttribute("currentMenuItemIndex", "{{- (sub .ID 1) -}}") document.getElementById("main").setAttribute("currentMenuItemIndex", "{{- (sub .ID 1) -}}")
document.getElementById("main").setAttribute("nextText", "{{- i18n "next" -}}") document.getElementById("main").setAttribute("nextText", "{{- i18n "next" -}}")

View File

@@ -1,7 +1,12 @@
{{- $Page := .Page -}} {{- $Page := . -}}
{{- $Path := "" -}}
{{- $params := .Page.Params | merge .Site.Params.Page -}} {{- $params := .Page.Params | merge .Site.Params.Page -}}
{{- $gallery := site.GetPage .Path -}} {{- with partial "function/currentMenuItem.html" . -}}
{{- $Path = .DirName -}}
{{- end -}}
{{- $gallery := site.GetPage $Path -}}
{{- with $gallery.Resources.ByType "image" -}} {{- with $gallery.Resources.ByType "image" -}}
{{- $index := len . -}} {{- $index := len . -}}
{{- $Page.Scratch.Add "img" slice -}} {{- $Page.Scratch.Add "img" slice -}}
@@ -26,5 +31,7 @@
) )
-}} -}}
{{- end -}} {{- end -}}
<script id="imagesSource" type="application/json">{{ $Page.Scratch.Get "img" | jsonify | safeJS }}</script> {{ $Page.Scratch.Get "img" | jsonify }}
{{- else -}}
[]
{{- end -}} {{- end -}}