Skip to content

Commit

Permalink
[chore] cache code snippets (sveltejs#7592)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored Nov 10, 2022
1 parent 8c480f6 commit 5655c4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion sites/kit.svelte.dev/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
.env
/.svelte-kit/
/.snippets
/build/
/functions/
/functions/
26 changes: 20 additions & 6 deletions sites/kit.svelte.dev/src/lib/docs/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import { render_modules } from './modules.js';
import { parse_route_id } from '../../../../../../packages/kit/src/utils/routing.js';
import ts from 'typescript';
import MagicString from 'magic-string';
import { fileURLToPath } from 'url';
import { createHash } from 'crypto';

const snippet_cache = fileURLToPath(new URL('../../../../.snippets', import.meta.url));
if (!fs.existsSync(snippet_cache)) {
fs.mkdirSync(snippet_cache, { recursive: true });
}

const languages = {
bash: 'bash',
Expand Down Expand Up @@ -54,8 +61,6 @@ export async function read_file(file) {
const match = /\d{2}-(.+)\.md/.exec(file.split(path.sep).pop());
if (!match) return null;

const slug = match[1];

const markdown = fs
.readFileSync(`${base}/${file}`, 'utf-8')
.replace('**TYPES**', () => render_modules('types'))
Expand All @@ -68,8 +73,15 @@ export async function read_file(file) {
const { content, sections } = parse({
body: generate_ts_from_js(body),
file,
slug,
code: (source, language, current) => {
const hash = createHash('sha256');
hash.update(source + language + current);
const digest = hash.digest().toString('base64').replace(/\//g, '-');

if (fs.existsSync(`${snippet_cache}/${digest}.html`)) {
return fs.readFileSync(`${snippet_cache}/${digest}.html`, 'utf-8');
}

/** @type {Record<string, string>} */
const options = {};

Expand Down Expand Up @@ -213,7 +225,7 @@ export async function read_file(file) {

type_regex.lastIndex = 0;

return html
html = html
.replace(type_regex, (match, prefix, name) => {
if (options.link === 'false' || name === current) {
// we don't want e.g. RequestHandler to link to RequestHandler
Expand All @@ -240,6 +252,9 @@ export async function read_file(file) {
.join('');
}
);

fs.writeFileSync(`${snippet_cache}/${digest}.html`, html);
return html;
},
codespan: (text) => {
return (
Expand All @@ -266,12 +281,11 @@ export async function read_file(file) {
* @param {{
* body: string;
* file: string;
* slug: string;
* code: (source: string, language: string, current: string) => string;
* codespan: (source: string) => string;
* }} opts
*/
function parse({ body, file, slug, code, codespan }) {
function parse({ body, file, code, codespan }) {
const headings = [];

/** @type {import('./types').Section[]} */
Expand Down

0 comments on commit 5655c4c

Please sign in to comment.