aboutsummaryrefslogtreecommitdiff
path: root/website/src/components/Head.astro
blob: dac83b818aadabaebd5604970be25a4e459adf2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
---
import { docsVersion } from '../docs-path';

const { head } = Astro.locals.starlightRoute;
const version = docsVersion(Astro.url.pathname);
---

{head.map(({ tag: Tag, attrs, content }) => <Tag {...attrs} set:html={content} />)}
<meta data-pagefind-filter="version[content]" content={version} />
{version !== 'stable' && <meta name="robots" content="noindex, nofollow" />}

<script>
  const labels = (() => {
    const lang = document.documentElement.lang.toLowerCase();
    if (lang.startsWith('ja')) return { copy: 'リンクをコピー', copied: 'コピーしました' };
    if (lang.startsWith('zh')) return { copy: '复制链接', copied: '已复制' };
    return { copy: 'Copy link', copied: 'Copied' };
  })();

  function labelHeadingLinks(root: ParentNode = document) {
    for (const link of root.querySelectorAll<HTMLAnchorElement>('.sl-anchor-link')) {
      link.title = labels.copy;
      const section = link.querySelector('.sr-only')?.textContent;
      if (section) link.setAttribute('aria-label', `${labels.copy}: ${section}`);
    }
  }

  labelHeadingLinks();
  document.addEventListener('astro:page-load', () => labelHeadingLinks());
  document.addEventListener('click', async (event) => {
    if (
      event instanceof MouseEvent &&
      (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
    ) {
      return;
    }

    const target = event.target;
    if (!(target instanceof Element)) return;
    const link = target.closest('.sl-anchor-link');
    if (!(link instanceof HTMLAnchorElement) || !navigator.clipboard?.writeText) return;

    try {
      await navigator.clipboard.writeText(link.href);
    } catch {
      return;
    }

    link.querySelector('.heading-link-copy-feedback')?.remove();
    const feedback = document.createElement('span');
    feedback.className = 'heading-link-copy-feedback';
    feedback.setAttribute('aria-hidden', 'true');
    feedback.textContent = labels.copied;
    link.append(feedback);
    window.setTimeout(() => feedback.remove(), 1400);

    let status = document.querySelector<HTMLElement>('[data-heading-link-copy-status]');
    if (!status) {
      status = document.createElement('span');
      status.className = 'sr-only';
      status.setAttribute('role', 'status');
      status.dataset.headingLinkCopyStatus = '';
      document.body.append(status);
    }
    status.textContent = labels.copied;
  });
</script>