Skip to content

Commit 8f2d8de

Browse files
committed
feat: make hash link work without disable clobbering
1 parent 67bed99 commit 8f2d8de

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

packages/bytemd/src/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ export interface ViewerProps {
102102
/**
103103
* Customize the default sanitize schema
104104
*
105-
* Defaults to GitHub style sanitation except:
106-
*
107-
* 1. `className` is allowed
108-
* 2. `id` is kept as is without clobbering
105+
* Defaults to GitHub style sanitation except the `className` property is allowed
109106
*
110107
* https://github.com/syntax-tree/hast-util-sanitize/blob/main/lib/github.json
111108
*/

packages/bytemd/src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export function getProcessor({
2727

2828
let schema = JSON.parse(schemaStr) as Schema;
2929
schema.attributes!['*'].push('className'); // Add className
30-
schema.clobber = schema.clobber!.filter((vs) => vs !== 'id'); // Keep id as is
3130
if (sanitize) schema = sanitize(schema);
3231

3332
p = p.use(rehypeSanitize, schema);

packages/bytemd/src/viewer.svelte

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import type { BytemdPlugin, ViewerProps } from './types';
3-
import { tick, onDestroy } from 'svelte';
3+
import { tick, onDestroy, onMount } from 'svelte';
44
import { getProcessor } from './utils';
55
66
export let value: ViewerProps['value'] = '';
@@ -17,6 +17,18 @@
1717
cbs.forEach((cb) => cb && cb());
1818
}
1919
20+
onMount(() => {
21+
el.addEventListener('click', (e) => {
22+
const $ = e.target as HTMLElement;
23+
if ($.tagName !== 'A') return;
24+
25+
const href = $.getAttribute('href');
26+
if (!href?.startsWith('#')) return;
27+
28+
el.querySelector('#user-content-' + href.slice(1))?.scrollIntoView();
29+
});
30+
});
31+
2032
onDestroy(off);
2133
$: result = getProcessor({ plugins, sanitize }).processSync(value);
2234
$: if (result && plugins) {

0 commit comments

Comments
 (0)