summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVolpeon <git@volpeon.ink>2022-08-21 18:33:02 +0200
committerVolpeon <git@volpeon.ink>2022-08-21 18:33:02 +0200
commitbd42fbff628ca5831d7a0f9b18e3466bd6dbe8a5 (patch)
tree7ca3187ed9d6a03799bed780f93920fa546b71ce
parentShow related content (diff)
downloadvolpeon.ink-bd42fbff628ca5831d7a0f9b18e3466bd6dbe8a5.tar.gz
volpeon.ink-bd42fbff628ca5831d7a0f9b18e3466bd6dbe8a5.tar.bz2
volpeon.ink-bd42fbff628ca5831d7a0f9b18e3466bd6dbe8a5.zip
Don't use random pages for "related"
-rw-r--r--scripts/metadata.lua20
-rw-r--r--scripts/page.lua41
-rw-r--r--templates/partials/related.html18
3 files changed, 54 insertions, 25 deletions
diff --git a/scripts/metadata.lua b/scripts/metadata.lua
index 49a9c55..9c8a964 100644
--- a/scripts/metadata.lua
+++ b/scripts/metadata.lua
@@ -130,6 +130,7 @@ end
130 130
131function process_base(build, meta) 131function process_base(build, meta)
132 meta.namespace = prep_namespace(meta.namespace) 132 meta.namespace = prep_namespace(meta.namespace)
133 meta.id = meta.namespace.full:gsub("^.*/(.-)$", "%1")
133 meta.file_out = utils.stringify(meta.file_out) 134 meta.file_out = utils.stringify(meta.file_out)
134 meta.unlisted = meta.unlisted or (meta.draft and build.mode ~= "dev") 135 meta.unlisted = meta.unlisted or (meta.draft and build.mode ~= "dev")
135 meta.redirect = meta.url and true 136 meta.redirect = meta.url and true
@@ -164,6 +165,24 @@ function process(global, build, meta)
164 resolve_dates(meta) 165 resolve_dates(meta)
165end 166end
166 167
168function find_pos(meta)
169 for _, p in pairs(meta.pages.by_id) do
170 p.parent = meta
171 find_pos(p)
172 p.parent = nil
173 end
174
175 if not meta.parent then return end
176
177 local pages = meta.parent.list_order == "date_desc" and meta.parent.pages.date_desc or meta.parent.pages.all
178 local _, index = pages:find(meta.id)
179
180 if index == nil then return end
181
182 meta.prev = index >= 2 and pages[index - 1] or nil
183 meta.next = index <= #pages - 1 and pages[index + 1] or nil
184end
185
167function Meta(meta) 186function Meta(meta)
168 local build = {} 187 local build = {}
169 188
@@ -180,6 +199,7 @@ function Meta(meta)
180 199
181 find_depth(meta) 200 find_depth(meta)
182 process(meta, build, meta) 201 process(meta, build, meta)
202 find_pos(meta)
183 203
184 return meta 204 return meta
185end 205end
diff --git a/scripts/page.lua b/scripts/page.lua
index dcbb36f..7378bfc 100644
--- a/scripts/page.lua
+++ b/scripts/page.lua
@@ -130,32 +130,35 @@ function generate_list(meta)
130 end 130 end
131end 131end
132 132
133function deref_page(pages)
134 return function(ref)
135 return pages[utils.stringify(ref)]
136 end
137end
138
133function generate_related(meta) 139function generate_related(meta)
134 if not meta.parent then return nil end 140 if not meta.parent then return nil end
135 141
136 meta = meta.parent 142 local deref = deref_page(meta.parent.pages.by_id)
137 143
138 local items = meta.pages.random:take(2):map(function (p) return d1_page_to_list_item(p) end) 144 meta.prev = meta.prev and d1_page_to_list_item(deref(meta.prev))
145 meta.next = meta.next and d1_page_to_list_item(deref(meta.next))
139 146
140 if #items == 0 then return nil end 147 if not meta.prev and not meta.next then return nil end
141 148
142 return { 149 return {
143 url = meta.url, 150 url = meta.parent.url,
144 layout = meta.list_layout, 151 layout = meta.parent.list_layout,
145 items = items 152 prev = meta.prev,
153 next = meta.next
146 } 154 }
147end 155end
148 156
149function deref_page(pages)
150 return function(ref)
151 return pages[utils.stringify(ref)]
152 end
153end
154
155function process_pages(meta) 157function process_pages(meta)
156 meta.pages.all = meta.pages.all:filterMap(deref_page(meta.pages.by_id)) 158 local deref = deref_page(meta.pages.by_id)
157 meta.pages.date_desc = meta.pages.date_desc:filterMap(deref_page(meta.pages.by_id)) 159
158 meta.pages.random = meta.pages.all :clone():shuffle() 160 meta.pages.all = meta.pages.all:filterMap(deref)
161 meta.pages.date_desc = meta.pages.date_desc:filterMap(deref)
159end 162end
160 163
161function resolve_urls(global, build, meta) 164function resolve_urls(global, build, meta)
@@ -179,6 +182,7 @@ function resolve_urls(global, build, meta)
179end 182end
180 183
181function process_base(meta) 184function process_base(meta)
185 meta.id = utils.stringify(meta.id)
182 meta.depth = tonumber(utils.stringify(meta.depth)) 186 meta.depth = tonumber(utils.stringify(meta.depth))
183 meta.file_out = utils.stringify(meta.file_out) 187 meta.file_out = utils.stringify(meta.file_out)
184 meta.url = utils.stringify(meta.url) 188 meta.url = utils.stringify(meta.url)
@@ -187,6 +191,8 @@ function process_base(meta)
187 meta.list_order = meta.list_order and utils.stringify(meta.list_order) 191 meta.list_order = meta.list_order and utils.stringify(meta.list_order)
188 meta.position = meta.position and tonumber(utils.stringify(meta.position)) 192 meta.position = meta.position and tonumber(utils.stringify(meta.position))
189 meta.thumbnail = meta.thumbnail and utils.stringify(meta.thumbnail) 193 meta.thumbnail = meta.thumbnail and utils.stringify(meta.thumbnail)
194 meta.prev = meta.prev and utils.stringify(meta.prev)
195 meta.next = meta.next and utils.stringify(meta.next)
190end 196end
191 197
192function process(global, build, meta, dir) 198function process(global, build, meta, dir)
@@ -207,7 +213,6 @@ function process(global, build, meta, dir)
207end 213end
208 214
209function prune_pages(meta, up, down) 215function prune_pages(meta, up, down)
210 meta.pages.random = pandoc.List()
211 meta.pages.by_id = {} 216 meta.pages.by_id = {}
212 217
213 if down == 0 then 218 if down == 0 then
@@ -231,8 +236,6 @@ function prune(meta, up, down)
231end 236end
232 237
233function Meta(meta) 238function Meta(meta)
234 math.randomseed(os.time())
235
236 local build = {} 239 local build = {}
237 240
238 for key, value in pairs(meta) do 241 for key, value in pairs(meta) do
@@ -254,7 +257,7 @@ function Meta(meta)
254 257
255 meta = meta.tree 258 meta = meta.tree
256 259
257 local parts = utils.stringify(build.namespace):split('/'):skip(1) 260 local parts = build.namespace:split('/'):skip(1)
258 for i = 1, #parts do 261 for i = 1, #parts do
259 local part = parts[i] 262 local part = parts[i]
260 local p = meta.pages.by_id[part] 263 local p = meta.pages.by_id[part]
diff --git a/templates/partials/related.html b/templates/partials/related.html
index b0d3f61..7d29c7a 100644
--- a/templates/partials/related.html
+++ b/templates/partials/related.html
@@ -14,19 +14,25 @@ $if(related)$
14 </header> 14 </header>
15 <div class="l-card-list__cards l-card-list__cards--$related.layout.id$"> 15 <div class="l-card-list__cards l-card-list__cards--$related.layout.id$">
16 $if(related.layout.is_grid-2)$ 16 $if(related.layout.is_grid-2)$
17 $related.items:partials/grid_card()$ 17 $related.prev:partials/grid_card()$
18 $related.next:partials/grid_card()$
18 $elseif(related.layout.is_grid-3)$ 19 $elseif(related.layout.is_grid-3)$
19 $related.items:partials/grid_card()$ 20 $related.prev:partials/grid_card()$
21 $related.next:partials/grid_card()$
20 $elseif(related.layout.is_gallery-2)$ 22 $elseif(related.layout.is_gallery-2)$
21 $related.items:partials/gallery_card()$ 23 $related.prev:partials/gallery_card()$
24 $related.next:partials/gallery_card()$
22 $elseif(related.layout.is_gallery-3)$ 25 $elseif(related.layout.is_gallery-3)$
23 $related.items:partials/gallery_card()$ 26 $related.prev:partials/gallery_card()$
27 $related.next:partials/gallery_card()$
24 $else$ 28 $else$
25 <div class="u-d-contents u-d-none@sm-lo"> 29 <div class="u-d-contents u-d-none@sm-lo">
26 $related.items:partials/list_card()$ 30 $related.prev:partials/list_card()$
31 $related.next:partials/list_card()$
27 </div> 32 </div>
28 <div class="u-d-contents u-d-none@sm-hi"> 33 <div class="u-d-contents u-d-none@sm-hi">
29 $related.items:partials/grid_card()$ 34 $related.prev:partials/grid_card()$
35 $related.next:partials/grid_card()$
30 </div> 36 </div>
31 $endif$ 37 $endif$
32 </div> 38 </div>