Skip to content

Commit f431e8e

Browse files
feat: allow document assets (#1601)
Co-authored-by: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com>
1 parent a6354e3 commit f431e8e

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

apps/svelte.dev/src/lib/server/content.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const assets = import.meta.glob<string>(
1717
import: 'default'
1818
}
1919
);
20+
// we need a separate glob import for document assets because we need to use `read` so it needs the actual import, not `?url`
21+
const documents_assets = import.meta.glob<string>(['../../../content/docs/**/+assets/**'], {
22+
eager: true,
23+
import: 'default'
24+
});
2025

2126
const registry_docs = import.meta.glob<string>(
2227
'../../../src/lib/server/generated/registry/*.json',
@@ -28,7 +33,13 @@ const registry_docs = import.meta.glob<string>(
2833
);
2934

3035
// https://github.com/vitejs/vite/issues/17453
31-
export const index = await create_index(documents, assets, '../../../content', read);
36+
export const index = await create_index(
37+
documents,
38+
assets,
39+
documents_assets,
40+
'../../../content',
41+
read
42+
);
3243

3344
const months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
3445

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { read } from '$app/server';
2+
import { docs } from '$lib/server/content';
3+
import { error } from '@sveltejs/kit';
4+
5+
export async function GET({ params: { name, topic } }) {
6+
const assets = docs.topics[`docs/${topic}`].assets;
7+
if (!assets) {
8+
error(404, 'No assets found for this topic');
9+
}
10+
11+
return read(assets[name]);
12+
}

packages/site-kit/src/lib/components/Text.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
}
9292
}
9393
94+
img {
95+
max-width: 100%;
96+
object-fit: contain;
97+
}
98+
9499
h2 {
95100
margin-top: 7rem;
96101
}

packages/site-kit/src/lib/server/content/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Document } from '../../types';
44
export async function create_index(
55
documents: Record<string, string>,
66
assets: Record<string, string>,
7+
documents_assets: Record<string, string>,
78
base: string,
89
read: (asset: string) => Response
910
): Promise<Record<string, Document>> {
@@ -99,6 +100,15 @@ export async function create_index(
99100
(document.assets ??= {})[file] = assets[key];
100101
}
101102

103+
for (const key in documents_assets) {
104+
const path = key.slice(base.length + 1);
105+
const slug = path.slice(0, path.indexOf('+assets') - 1).replace(/(^|\/)\d+-/g, '$1');
106+
const file = path.slice(path.indexOf('+assets') + 8);
107+
const document = content[slug];
108+
109+
(document.assets ??= {})[file] = documents_assets[key];
110+
}
111+
102112
let prev: Document | null = null;
103113

104114
for (const document of roots) {

0 commit comments

Comments
 (0)