@riebeckite/plugin-l10n 0.0.24 → 0.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import type { LocalizedContent } from "../src/l10n.js";
2
2
  export type LanguageSwitcherProps = {
3
- localization: LocalizedContent | null;
3
+ localization?: LocalizedContent | null;
4
4
  };
5
5
  declare const LanguageSwitcher: (props: Readonly<LanguageSwitcherProps>) => import("hono/jsx/jsx-dev-runtime").JSX.Element;
6
6
  export default LanguageSwitcher;
@@ -1,7 +1,62 @@
1
+ // components/language-switcher.tsx
2
+ import { useArticleContext, useSiteContext } from "@riebeckite/ui";
3
+
4
+ // src/l10n.ts
5
+ import {
6
+ attachErrorPath,
7
+ createStyleAsset,
8
+ definePlugin
9
+ } from "@riebeckite/core";
10
+ import { parse } from "yaml";
11
+ var TRANSLATION_METADATA_KEY = "l10n.translationId";
12
+ function getLocalization(view, slug) {
13
+ const entry = findLocalizedEntry(view, slug);
14
+ if (!entry) return null;
15
+ const lang = entry.publicLocation.language;
16
+ const translationId = entry.publicLocation.metadata?.[TRANSLATION_METADATA_KEY];
17
+ if (!lang || !translationId) return null;
18
+ const candidates = getLocalizedDiscoverableEntries(view).filter(
19
+ (candidate) => candidate.publicLocation.metadata?.[TRANSLATION_METADATA_KEY] === translationId
20
+ );
21
+ const translations = Object.fromEntries(
22
+ uniqueLanguageEntries(candidates).map((candidate) => [
23
+ candidate.publicLocation.language ?? "",
24
+ candidate.permalink
25
+ ])
26
+ );
27
+ return {
28
+ slug,
29
+ permalink: entry.permalink,
30
+ lang,
31
+ translationId,
32
+ availableLanguages: Object.keys(translations).sort(),
33
+ translations
34
+ };
35
+ }
36
+ function findLocalizedEntry(view, slug) {
37
+ return "findRoutableBySlug" in view ? view.findRoutableBySlug(slug) : view.bySlug.get(slug);
38
+ }
39
+ function getLocalizedDiscoverableEntries(view) {
40
+ return view.discoverableEntries;
41
+ }
42
+ function uniqueLanguageEntries(entries) {
43
+ const entriesByLanguage = /* @__PURE__ */ new Map();
44
+ for (const entry of entries) {
45
+ const language = entry.publicLocation.language;
46
+ if (!language) continue;
47
+ const candidates = entriesByLanguage.get(language) ?? [];
48
+ candidates.push(entry);
49
+ entriesByLanguage.set(language, candidates);
50
+ }
51
+ return [...entriesByLanguage.values()].filter((candidates) => candidates.length === 1).flatMap((candidates) => candidates[0] ? [candidates[0]] : []);
52
+ }
53
+
1
54
  // components/language-switcher.tsx
2
55
  import { jsx, jsxs } from "hono/jsx/jsx-runtime";
3
56
  var LanguageSwitcher = ((props) => {
4
- const localization = props.localization;
57
+ const site = useSiteContext();
58
+ const article = useArticleContext();
59
+ const localization = props.localization ?? (site && article?.entry ? getLocalization(site.view, article.entry.slug) : null);
5
60
  if (!localization || localization.availableLanguages.length < 2) return null;
6
61
  return /* @__PURE__ */ jsxs("nav", { class: "l10n-switcher", "aria-label": "Language", children: [
7
62
  /* @__PURE__ */ jsx("span", { class: "l10n-switcher__label", children: "Language" }),
package/dist/index.js CHANGED
@@ -1,28 +1,5 @@
1
1
  // components/language-switcher.tsx
2
- import { jsx, jsxs } from "hono/jsx/jsx-runtime";
3
- var LanguageSwitcher = ((props) => {
4
- const localization = props.localization;
5
- if (!localization || localization.availableLanguages.length < 2) return null;
6
- return /* @__PURE__ */ jsxs("nav", { class: "l10n-switcher", "aria-label": "Language", children: [
7
- /* @__PURE__ */ jsx("span", { class: "l10n-switcher__label", children: "Language" }),
8
- /* @__PURE__ */ jsx("ul", { class: "l10n-switcher__list", children: localization.availableLanguages.map((lang) => {
9
- const href = localization.translations[lang];
10
- if (!href) return null;
11
- return /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx(
12
- "a",
13
- {
14
- class: "l10n-switcher__link",
15
- href,
16
- hreflang: lang,
17
- lang,
18
- "aria-current": lang === localization.lang ? "page" : void 0,
19
- children: lang
20
- }
21
- ) }, lang);
22
- }) })
23
- ] });
24
- });
25
- var language_switcher_default = LanguageSwitcher;
2
+ import { useArticleContext, useSiteContext } from "@riebeckite/ui";
26
3
 
27
4
  // src/l10n.ts
28
5
  import {
@@ -512,6 +489,34 @@ function emptyState() {
512
489
  slugsByPermalink: /* @__PURE__ */ new Map()
513
490
  };
514
491
  }
492
+
493
+ // components/language-switcher.tsx
494
+ import { jsx, jsxs } from "hono/jsx/jsx-runtime";
495
+ var LanguageSwitcher = ((props) => {
496
+ const site = useSiteContext();
497
+ const article = useArticleContext();
498
+ const localization = props.localization ?? (site && article?.entry ? getLocalization(site.view, article.entry.slug) : null);
499
+ if (!localization || localization.availableLanguages.length < 2) return null;
500
+ return /* @__PURE__ */ jsxs("nav", { class: "l10n-switcher", "aria-label": "Language", children: [
501
+ /* @__PURE__ */ jsx("span", { class: "l10n-switcher__label", children: "Language" }),
502
+ /* @__PURE__ */ jsx("ul", { class: "l10n-switcher__list", children: localization.availableLanguages.map((lang) => {
503
+ const href = localization.translations[lang];
504
+ if (!href) return null;
505
+ return /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx(
506
+ "a",
507
+ {
508
+ class: "l10n-switcher__link",
509
+ href,
510
+ hreflang: lang,
511
+ lang,
512
+ "aria-current": lang === localization.lang ? "page" : void 0,
513
+ children: lang
514
+ }
515
+ ) }, lang);
516
+ }) })
517
+ ] });
518
+ });
519
+ var language_switcher_default = LanguageSwitcher;
515
520
  export {
516
521
  language_switcher_default as LanguageSwitcher,
517
522
  getLanguageFromPath,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riebeckite/plugin-l10n",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -23,11 +23,12 @@
23
23
  "dependencies": {
24
24
  "hono": "^4.12.25",
25
25
  "yaml": "^2.9.0",
26
- "@riebeckite/core": "0.0.24"
26
+ "@riebeckite/core": "0.0.25",
27
+ "@riebeckite/ui": "0.0.25"
27
28
  },
28
29
  "devDependencies": {
29
30
  "tsx": "^4.22.4",
30
- "@riebeckite/plugin-obsidian-markdown": "0.0.24"
31
+ "@riebeckite/plugin-obsidian-markdown": "0.0.25"
31
32
  },
32
33
  "publishConfig": {
33
34
  "access": "public"