|
| 1 | +import { get_blog_data, get_blog_list } from '$lib/server/blog/index.js'; |
| 2 | +import { get_docs_data, get_docs_list } from '$lib/server/docs/index.js'; |
| 3 | +import { get_examples_data, get_examples_list } from '$lib/server/examples/index.js'; |
| 4 | +import { get_tutorial_data, get_tutorial_list } from '$lib/server/tutorial/index.js'; |
| 5 | + |
| 6 | +/** @param {URL} url */ |
| 7 | +function get_nav_title(url) { |
| 8 | + const list = new Map([ |
| 9 | + [/^docs/, 'Docs'], |
| 10 | + [/^repl/, 'REPL'], |
| 11 | + [/^blog/, 'Blog'], |
| 12 | + [/^faq/, 'FAQ'], |
| 13 | + [/^tutorial/, 'Tutorial'], |
| 14 | + [/^search/, 'Search'], |
| 15 | + [/^examples/, 'Examples'] |
| 16 | + ]); |
| 17 | + |
| 18 | + for (const [regex, title] of list) { |
| 19 | + if (regex.test(url.pathname.replace(/^\/(.+)/, '$1'))) { |
| 20 | + return title; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + return ''; |
| 25 | +} |
| 26 | + |
| 27 | +async function get_nav_context_list() { |
| 28 | + const docs_list = get_docs_list(get_docs_data()); |
| 29 | + const processed_docs_list = docs_list.map(({ title, pages }) => ({ |
| 30 | + title, |
| 31 | + sections: pages.map(({ title, path }) => ({ title, path })) |
| 32 | + })); |
| 33 | + |
| 34 | + const blog_list = get_blog_list(get_blog_data()); |
| 35 | + const processed_blog_list = [ |
| 36 | + { |
| 37 | + title: 'Blog', |
| 38 | + sections: blog_list.map(({ title, slug, date }) => ({ |
| 39 | + title, |
| 40 | + path: '/blog/' + slug, |
| 41 | + // Put a NEW badge on blog posts that are less than 14 days old |
| 42 | + badge: (+new Date() - +new Date(date)) / (1000 * 60 * 60 * 24) < 14 ? 'NEW' : undefined |
| 43 | + })) |
| 44 | + } |
| 45 | + ]; |
| 46 | + |
| 47 | + const tutorial_list = get_tutorial_list(get_tutorial_data()); |
| 48 | + const processed_tutorial_list = tutorial_list.map(({ title, tutorials }) => ({ |
| 49 | + title, |
| 50 | + sections: tutorials.map(({ title, slug }) => ({ title, path: '/tutorial/' + slug })) |
| 51 | + })); |
| 52 | + |
| 53 | + const examples_list = get_examples_list(get_examples_data()); |
| 54 | + const processed_examples_list = examples_list |
| 55 | + .map(({ title, examples }) => ({ |
| 56 | + title, |
| 57 | + sections: examples.map(({ title, slug }) => ({ title, path: '/examples/' + slug })) |
| 58 | + })) |
| 59 | + .filter(({ title }) => title !== 'Embeds'); |
| 60 | + |
| 61 | + return { |
| 62 | + docs: processed_docs_list, |
| 63 | + blog: processed_blog_list, |
| 64 | + tutorial: processed_tutorial_list, |
| 65 | + examples: processed_examples_list |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +export const load = async ({ url }) => { |
| 70 | + return { |
| 71 | + nav_title: get_nav_title(url), |
| 72 | + nav_context_list: get_nav_context_list() |
| 73 | + }; |
| 74 | +}; |
0 commit comments