Skip to content

Commit

Permalink
Add support for anchors to links and clean up local links in transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Richard committed May 3, 2024
1 parent aa9549e commit 61082cf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion site/lib/transforms/link-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ function icon(id, modifier = false) {
export async function postHTMLLinkIcons(tree) {
tree.match({ tag: 'a' }, (node) => {
const classes = node?.attrs?.class?.split(' ') || [];
const href = node?.attrs?.href || '';
let href = node?.attrs?.href || '';

// Do a check for ChromeOS.dev links
if (RegExp('^https?://chromeos.dev').test(href)) {
href = href.replace(/^https?:\/\/chromeos\.dev/, '');
node.attrs.href = href;
}

const external = RegExp('^https?://').test(href);

const types = {
Expand Down
16 changes: 16 additions & 0 deletions site/src/components/portable-text/Link.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
type Mark = {
_type: string;
_key: string;
anchor?: string;
};
interface SourceMark extends SourceRef, Mark {}
Expand All @@ -45,6 +46,21 @@
} else if (mark?.href) {
link = mark.href;
}
let url;
// Build full URLs with anchors
try {
url = new URL(link);
} catch (e) {
url = new URL(`https://chromeos.dev${link}`);
}
if (mark.anchor && !url.hash) {
url.hash = mark.anchor;
}
link = url.toString();
</script>

{#if link === ''}
Expand Down

0 comments on commit 61082cf

Please sign in to comment.