Move *.jst from / to /_lib to keep things tidy, implement /_lib/Problem.jst and ...
[ndcode_site.git] / _lib / breadcrumbs.jst
1 let assert = require('assert')
2
3 return async (env, _out) => {
4   let pathname = env.parsed_url.pathname
5   assert(pathname.slice(0, 1) === '/')
6
7   // find number of path components, their positions, and names
8   let components = [{index: 0, name: 'Home'}]
9   for (let i = 1, j; (j = pathname.indexOf('/', i)) !== -1; i = j + 1) {
10     let menu = await env.site.get_menu(`${pathname.slice(0, i)}_menu.json`)
11     let dir = pathname.slice(i, j)
12     components.push({index: j, name: menu.entries[menu.index[dir]].name})
13   }
14
15   // present components as breadcrumbs, except last one as text
16   h2.mt-3 {
17     for (let i = 0; i < components.length - 1; ++i) {
18       a.h4(
19         href=`${pathname.slice(0, components[i].index)}/index.html`
20       ) {`${components[i].name}`}
21       ' '
22       span.h5 {'>'}
23       ' '
24     }
25     `${components[components.length - 1].name}`
26   }
27 }