· 2 min read · updated
An mdsvex layout for markdown links
Added MarkdownLayout.svelte, passed to mdsvex's compile as layout. It wraps every .md file (posts and Devlog entries) and only renders children.
What we did
- Added
MarkdownLayout.svelte, passed to mdsvex'scompileaslayout. It wraps every .md file (posts and Devlog entries) and only renderschildren. - The layout's module script exports
a=MarkdownLink.svelte, so every markdown link is rendered by that component instead of a plain<a>:/cvgets the base path throughresolve(), and renders ashref="../cv"in the build.https://…getsrel="external".- mdsvex's
rel="nofollow"is dropped: these are links I recommend.
- The
markdown()plugin also removes mdsvex's{...$$props}from the layout call, which runes mode doesn't allow.
What I learned
- Without the layout,
[CV](/cv)points at the domain root. On GitHub Pages the site lives under/portfolio, so that link would 404. - mdsvex replaces an element when the layout exports a component with that element's name. It imports the layout as
* as Components, so any other export (likeCodeBlock) can be used by name, asComponents.CodeBlock. layouthas to be a file path, not a URL: mdsvex reads the file to see which elements it replaces.resolve()'s types only take routes it knows, and a markdown link is just a string, so TypeScript can't check it. The prerender crawler checks instead: a link to a page that doesn't exist fails the build.
Problems & fixes
resolve('/cv')with astringdidn't type-check ($app/typeshas noPathnametype to cast to). For a path with no[param],resolve()only adds the base, so its type is widened once inMarkdownLink.
Links
- mdsvex: layouts
- mdsvex: custom components
- SvelteKit:
resolve - SvelteKit: prerender options
- Google: qualify outbound links (
nofollow)
Next
- Tag pages,
/blog/tags/[tag].