Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sanitize paths removing trailing slash #262

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/components/rich-text.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
<script lang="ts" context="module">
// Monkey patch RichTextSchema.marks.link to add `sanitizeSlug`
// TODO: Find a better alternative and be careful updating storyblok dependency
// Patched as of https://github.com/storyblok/storyblok-js-client/blob/069d51afa8003f804ef8af5fc5b5823d2ce4a658/src/schema.ts#L133C1-L161
RichTextSchema.marks.link = (node) => {
/* eslint-disable @typescript-eslint/no-unused-vars */
const { story, uuid, linktype = 'url', ...attrs } = node.attrs;

if (linktype === 'email') {
attrs.href = `mailto:${attrs.href}`;
}
else if(attrs.href){
attrs.href = sanitizeSlug(attrs.href)
}

if (attrs.anchor) {
attrs.href = `${attrs.href}#${attrs.anchor}`
delete attrs.anchor
}

if (attrs.custom) {
for (const key in attrs.custom) {
attrs[key] = attrs.custom[key]
}
delete attrs.custom
}

return {
tag: [
{
tag: 'a',
attrs: attrs,
},
],
}
};

export const resolver = new RichTextResolver(RichTextSchema);
</script>

<script lang="ts">
import { clsx } from 'clsx';
import { RichTextResolver, type ISbRichtext } from '@storyblok/js';
import { RichTextResolver, type ISbRichtext, RichTextSchema } from '@storyblok/js';
import DynamicBlock from './blocks/dynamic-block.svelte';
import type { HTMLAttributes } from 'svelte/elements';
import { sanitizeSlug } from '$lib/utils/paths';

type Section = NonNullable<ISbRichtext['content']>[number];
type Block = NonNullable<Section['attrs']['body']>[number];
Expand All @@ -18,8 +59,6 @@
export let doc: $$Props['doc'];
export let getAttributes: $$Props['getAttributes'] = () => ({});
export let as: $$Props['as'] = 'div';

const resolver = new RichTextResolver();
</script>

{#if doc.type === 'doc' && doc.content?.length}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const sanitizeSlug = (slug: string) => {
// nothing for now, but it's good to have a centralized function that we pass all slugs through
if (slug.startsWith('http')) return slug;

// always start with a slash and end eith no slash
// always start with a slash and end with no slash
return '/' + slug.replace(/^\/+/, '').replace(/\/+$/, '');
};

Expand Down