Skip to content

Commit f6f1b25

Browse files
authored
Add comprehensive localization to learningmap web components with language settings
1 parent b7a8c09 commit f6f1b25

62 files changed

Lines changed: 4779 additions & 2731 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/lemon-geckos-run.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@hyperbook/web-component-learningmap": minor
3+
"@hyperbook/markdown": minor
4+
"@hyperbook/types": minor
5+
"@hyperbook/fs": minor
6+
"hyperbook": minor
7+
"hyperbook-studio": minor
8+
---
9+
10+
Vastly improved learningmap element

packages/fs/src/hyperbook.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export const getPagesAndSections = async (
177177
}
178178
const page: HyperbookPage = {
179179
...data,
180+
path: file.path,
180181
};
181182
const repo = makeRepoLink(hyperbook.repo, file);
182183
if (repo) {

packages/fs/tests/__snapshots__/hyperbook.test.ts.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,37 @@ exports[`hyperbook > should get navigation 1`] = `
55
"current": {
66
"href": "/paradigms",
77
"name": "Paradigms",
8+
"path": {
9+
"absolute": "single-hyperbook/book/paradigms.md",
10+
"directory": "",
11+
"href": "/paradigms",
12+
"permalink": null,
13+
"relative": "paradigms.md",
14+
},
815
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/paradigms.md",
916
},
1017
"next": {
1118
"href": "/hyperbook-test",
1219
"name": "hyperbook-test",
20+
"path": {
21+
"absolute": "single-hyperbook/book/hyperbook-test.md",
22+
"directory": "",
23+
"href": "/hyperbook-test",
24+
"permalink": null,
25+
"relative": "hyperbook-test.md",
26+
},
1327
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/hyperbook-test.md",
1428
},
1529
"previous": {
1630
"href": "/",
1731
"name": "Home",
32+
"path": {
33+
"absolute": "single-hyperbook/book/index.md",
34+
"directory": "",
35+
"href": "/",
36+
"permalink": null,
37+
"relative": "index.md",
38+
},
1839
"repo": "https://github.com/openpatch/hyperbook/edit/main/website/en/book/index.md",
1940
},
2041
}

packages/fs/tests/hyperbook.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import path from "path";
22
import { describe, it, expect } from "vitest";
33
import { hyperbook, vfile } from "../src";
4+
import { HyperbookPage } from "@hyperbook/types/dist";
45

56
describe("hyperbook", () => {
7+
const relative = (s: string) =>
8+
path.relative(path.join(__dirname, "fixtures"), s);
9+
const makeFileRelative = (p: HyperbookPage) => {
10+
return {
11+
...p,
12+
path: { ...p.path, absolute: relative(p.path?.absolute || "") },
13+
} as HyperbookPage;
14+
};
615
it("should get navigation", async () => {
716
let hyperbookPath = path.join(__dirname, "fixtures", "single-hyperbook");
817
let files = await vfile.list(hyperbookPath);
@@ -17,7 +26,10 @@ describe("hyperbook", () => {
1726
pagesAndSections.sections,
1827
pagesAndSections.pages,
1928
);
20-
const navigation = await hyperbook.getNavigationForFile(pageList, current);
29+
const navigation = await hyperbook.getNavigationForFile(
30+
pageList.map(makeFileRelative),
31+
current,
32+
);
2133
expect(navigation).toMatchSnapshot();
2234
});
2335
});

packages/markdown/assets/directive-learningmap/client.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,14 @@ hyperbook.learningmap = (function () {
66
const map = elem.getElementsByTagName("hyperbook-learningmap")[0];
77
if (map) {
88
const result = await store.learningmap.get(elem.id);
9-
if (result && result.nodeState) {
10-
map.nodeState = result.nodeState;
11-
map.x = result.x || 0;
12-
map.y = result.y || 0;
13-
map.zoom = result.zoom || 1;
9+
if (result) {
10+
map.initialState = result;
1411
}
1512
map.addEventListener("change", function (event) {
1613
store.learningmap
1714
.update(elem.id, {
18-
nodeState: event.detail,
19-
})
20-
.then((updated) => {
21-
if (updated == 0) {
22-
store.learningmap.put({
23-
id: elem.id,
24-
nodeState: event.detail,
25-
});
26-
}
27-
});
28-
});
29-
map.addEventListener("viewport-change", function (event) {
30-
store.learningmap
31-
.update(elem.id, {
15+
id: elem.id,
16+
nodes: event.detail.nodes,
3217
x: event.detail.x,
3318
y: event.detail.y,
3419
zoom: event.detail.zoom,
@@ -37,6 +22,7 @@ hyperbook.learningmap = (function () {
3722
if (updated == 0) {
3823
store.learningmap.put({
3924
id: elem.id,
25+
nodes: event.detail.nodes,
4026
x: event.detail.x,
4127
y: event.detail.y,
4228
zoom: event.detail.zoom,

packages/markdown/assets/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ store.version(1).stores({
2626
webide: `id,html,css,js`,
2727
h5p: `id,userData`,
2828
geogebra: `id,state`,
29-
learningmap: `id,nodeState,x,y,zoom`,
29+
learningmap: `id,nodes,x,y,zoom`,
3030
});
3131
var sqlIdeDB = new Dexie("SQL-IDE");
3232
sqlIdeDB.version(0.1).stores({

packages/markdown/src/helper.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { HyperbookContext } from "@hyperbook/types";
2+
import path from "path";
3+
import fs from "fs";
4+
5+
export const readFile = (src: string, ctx: HyperbookContext) => {
6+
let srcFile = null;
7+
try {
8+
srcFile = fs.readFileSync(path.join(ctx.root, "public", src), "utf-8");
9+
} catch (e) {
10+
try {
11+
srcFile = fs.readFileSync(path.join(ctx.root, "book", src), "utf-8");
12+
} catch (e) {
13+
srcFile = fs.readFileSync(
14+
path.join(
15+
ctx.root,
16+
"book",
17+
ctx.navigation.current?.path?.directory || "",
18+
src,
19+
),
20+
"utf-8",
21+
);
22+
}
23+
}
24+
return srcFile;
25+
};

packages/markdown/src/rehypeDirectiveP5.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//
44
import { HyperbookContext } from "@hyperbook/types";
55
import { Root } from "mdast";
6-
import fs from "fs";
76
import path from "path";
87
import { visit } from "unist-util-visit";
98
import { VFile } from "vfile";
@@ -16,6 +15,7 @@ import {
1615
import { toText } from "./mdastUtilToText";
1716
import hash from "./objectHash";
1817
import { i18n } from "./i18n";
18+
import { readFile } from "./helper";
1919

2020
interface CodeBundle {
2121
js?: string;
@@ -94,10 +94,7 @@ ${(code.scripts ? [cdnLibraryUrl, ...code.scripts] : []).map((src) => `<script t
9494
let srcFile = "";
9595

9696
if (src) {
97-
srcFile = fs.readFileSync(
98-
path.join(ctx.root, "public", String(src)),
99-
"utf8",
100-
);
97+
srcFile = readFile(src, ctx);
10198
} else if (node.children?.length > 0) {
10299
srcFile = toText(node.children);
103100
}

packages/markdown/src/remarkDirectiveLearningmap.ts

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import {
1010
isDirective,
1111
registerDirective,
1212
} from "./remarkHelper";
13-
import { toText } from "./mdastUtilToText";
1413
import hash from "./objectHash";
15-
import * as yaml from "js-yaml";
14+
import { readFile } from "./helper";
1615

1716
export default (ctx: HyperbookContext) => () => {
1817
const name = "learningmap";
@@ -32,52 +31,27 @@ export default (ctx: HyperbookContext) => () => {
3231
[],
3332
);
3433

35-
const { height = "calc(100vh - 80px)", id = hash(node) } =
36-
node.attributes || {};
34+
const {
35+
height = "calc(100vh - 80px)",
36+
id = hash(node),
37+
src = "",
38+
} = node.attributes || {};
3739

38-
const roadmapData = toText(
39-
node.children.find(
40-
(c) =>
41-
c.type === "code" && (c.lang === "yaml" || c.lang === "json"),
42-
),
43-
);
44-
45-
let parsedRoadmapData: {
46-
background?: {
47-
image?: { src: string };
48-
};
49-
nodes?: {
50-
video?: string;
51-
resources: { url: string }[];
52-
}[];
53-
};
54-
55-
try {
56-
parsedRoadmapData = JSON.parse(roadmapData) as any;
57-
} catch {
58-
try {
59-
parsedRoadmapData = yaml.load(roadmapData) as any;
60-
} catch (err) {
61-
console.error("Failed to parse roadmap data:", err);
62-
return SKIP;
63-
}
64-
}
40+
if (!src) return SKIP;
6541

66-
if (parsedRoadmapData.background?.image?.src) {
67-
const url = ctx.makeUrl(
68-
parsedRoadmapData.background.image.src,
69-
"public",
70-
ctx.navigation.current || undefined,
71-
);
72-
parsedRoadmapData.background.image.src = url;
42+
let srcFile = readFile(src, ctx);
43+
if (!srcFile) {
44+
file.message(`File not found: ${src}`, node);
45+
return SKIP;
7346
}
7447

75-
parsedRoadmapData.nodes?.forEach((node) => {
48+
let parsedRoadmapData = JSON.parse(srcFile) as any;
49+
parsedRoadmapData.nodes?.forEach((node: any) => {
7650
if (node.video) {
7751
node.video = ctx.makeUrl(node.video, "public");
7852
}
7953
if (node.resources) {
80-
node.resources = node.resources.map((res) => ({
54+
node.resources = node.resources.map((res: any) => ({
8155
...res,
8256
url: ctx.makeUrl(res.url, "public"),
8357
}));

packages/markdown/src/remarkDirectivePyide.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//
44
import { HyperbookContext } from "@hyperbook/types";
55
import { Code, Root } from "mdast";
6-
import fs from "fs";
7-
import path from "path";
86
import { visit } from "unist-util-visit";
97
import { VFile } from "vfile";
108
import {
@@ -18,6 +16,7 @@ import { toText } from "./mdastUtilToText";
1816
import hash from "./objectHash";
1917
import { ElementContent } from "hast";
2018
import { i18n } from "./i18n";
19+
import { readFile } from "./helper";
2120

2221
function htmlEntities(str: string) {
2322
return String(str)
@@ -53,10 +52,7 @@ export default (ctx: HyperbookContext) => () => {
5352
let input = "";
5453

5554
if (src) {
56-
srcFile = fs.readFileSync(
57-
path.join(ctx.root, "public", String(src)),
58-
"utf8",
59-
);
55+
srcFile = readFile(src, ctx);
6056
} else if (node.children?.length > 0) {
6157
tests = node.children
6258
.filter((c) => c.type === "code")

0 commit comments

Comments
 (0)