Skip to content

Commit

Permalink
feat: OnThisPage blog (sveltejs#9216)
Browse files Browse the repository at this point in the history
* feat: OnThisPage blog

### Before submitting the PR, please make sure you do the following

- [ ] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [ ] Prefix your PR title with `feat:`, `fix:`, `chore:`, or `docs:`.
- [ ] This message body should clearly illustrate what problems it solves.
- [ ] Ideally, include a test that fails without this PR but passes with it.

### Tests and linting

- [ ] Run the tests with `pnpm test` and lint the project with `pnpm lint`

* Bump site-kit

* Add content
  • Loading branch information
PuruVJ authored and kelvinsjk committed Oct 19, 2023
1 parent 720b86c commit c000841
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sites/svelte.dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@resvg/resvg-js": "^2.4.1",
"@sveltejs/adapter-vercel": "^3.0.3",
"@sveltejs/kit": "^1.24.1",
"@sveltejs/site-kit": "6.0.0-next.45",
"@sveltejs/site-kit": "6.0.0-next.46",
"@sveltejs/vite-plugin-svelte": "^2.4.6",
"@types/cookie": "^0.5.2",
"@types/node": "^20.5.9",
Expand Down
4 changes: 3 additions & 1 deletion sites/svelte.dev/src/lib/server/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { extractFrontmatter } from '@sveltejs/site-kit/markdown';
import { CONTENT_BASE_PATHS } from '../../../constants.js';
import { render_content } from '../renderer.js';
import { get_sections } from '../docs/index.js';

/**
* @param {import('./types').BlogData} blog_data
Expand Down Expand Up @@ -47,7 +48,8 @@ export async function get_blog_data(base = CONTENT_BASE_PATHS.BLOG) {
author: {
name: metadata.author,
url: metadata.authorURL
}
},
sections: await get_sections(body)
});
}

Expand Down
3 changes: 3 additions & 0 deletions sites/svelte.dev/src/lib/server/blog/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Section } from '../docs/types';

export interface BlogPost {
title: string;
description: string;
Expand All @@ -11,6 +13,7 @@ export interface BlogPost {
};
draft: boolean;
content: string;
sections: Section[];
}

export type BlogData = BlogPost[];
Expand Down
3 changes: 2 additions & 1 deletion sites/svelte.dev/src/lib/server/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ const titled = async (str) =>
.replace(/"/g, '"')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/, '&')
.replace(/<(\/)?(em|b|strong|code)>/g, '')
);

/** @param {string} markdown */
async function get_sections(markdown) {
export async function get_sections(markdown) {
const lines = markdown.split('\n');
const root = /** @type {import('./types').Section} */ ({
title: 'Root',
Expand Down
40 changes: 27 additions & 13 deletions sites/svelte.dev/src/routes/blog/[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { page } from '$app/stores';
import { copy_code_descendants } from '@sveltejs/site-kit/actions';
import { setupDocsHovers } from '@sveltejs/site-kit/docs';
import { DocsOnThisPage, setupDocsHovers } from '@sveltejs/site-kit/docs';
export let data;
Expand All @@ -20,17 +20,31 @@
<meta name="og:image" content="https://svelte.dev/blog/{$page.params.slug}/card.png" />
</svelte:head>

<article class="post listify text" use:copy_code_descendants>
<h1>{data.post.title}</h1>
<p class="standfirst">{data.post.description}</p>

<p class="byline">
<a href={data.post.author.url}>{data.post.author.name}</a>
<time datetime={data.post.date}>{data.post.date_formatted}</time>
</p>

{@html data.post.content}
</article>
<div class="content">
<article class="post listify text" use:copy_code_descendants>
<h1>{data.post.title}</h1>
<p class="standfirst">{data.post.description}</p>

<p class="byline">
<a href={data.post.author.url}>{data.post.author.name}</a>
<time datetime={data.post.date}>{data.post.date_formatted}</time>
</p>

<DocsOnThisPage
details={{
content: '',
file: '',
path: `/blog/${data.post.slug}`,
sections: data.post.sections,
slug: data.post.slug,
title: data.post.title
}}
orientation="inline"
/>

{@html data.post.content}
</article>
</div>

<!-- the crawler doesn't understand twitter:image etc, so we have to add this hack. TODO fix in sveltekit -->
<img hidden src="/blog/{$page.params.slug}/card.png" alt="Social card for {data.post.title}" />
Expand All @@ -54,7 +68,7 @@
}
.byline {
margin: 0 0 6rem 0;
margin: 0 0 1rem 0;
padding: 1.6rem 0 0 0;
border-top: var(--sk-thick-border-width) solid #6767785b;
font-size: var(--sk-text-xs);
Expand Down

0 comments on commit c000841

Please sign in to comment.