Skip to content

Commit b21accc

Browse files
authored
DEV: migratre to objects setting, fix mobile CSS (#56)
1 parent c8397d1 commit b21accc

6 files changed

Lines changed: 231 additions & 28 deletions

File tree

common/common.scss

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
@use "lib/viewport";
2+
13
@if $Hide_dropdowns == "true" {
24
ol.category-breadcrumb {
35
display: none;
46
}
57
}
68

79
@if $Hide_default_links == "true" {
8-
#navigation-bar {
9-
li:not(.navigation-toggle, [class*="custom_"]) {
10+
.list-control-toggle-link-content li:not([class*="custom_"]) {
11+
display: none;
12+
}
13+
14+
@include viewport.from(sm) {
15+
#navigation-bar li:not([class*="custom_"]) {
1016
display: none;
1117
}
1218
}

javascripts/discourse/initializers/init-nav-bar-additions.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@ export default {
55

66
initialize() {
77
withPluginApi((api) => {
8-
const itemsSetting = settings.Nav_links;
9-
const items = itemsSetting
10-
.split("|")
11-
.map((item) => item.trim())
12-
.filter(Boolean);
13-
14-
for (const item of items) {
15-
const splitSec = item.split(";").map((section) => section.trim());
16-
const filter = splitSec[0];
17-
const filterDasherized = splitSec[0].replace(/\s+/g, "-").toLowerCase();
18-
const title = splitSec[1];
19-
const location = splitSec[2];
20-
8+
for (const {
9+
display_name: displayName,
10+
title,
11+
url,
12+
} of settings.nav_links) {
2113
api.addNavigationBarItem({
22-
name: `custom_${filterDasherized}`,
23-
displayName: filter,
14+
name: `custom_${displayName.replace(/\s+/g, "-").toLowerCase()}`,
15+
displayName,
2416
title,
25-
href: location,
17+
href: url,
2618
forceActive: (category, args, router) =>
27-
router.currentURL.includes(location),
19+
router.currentURL?.split("?")[0] === url,
2820
});
2921
}
3022
});

locales/en.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
en:
2+
theme_metadata:
3+
settings:
4+
nav_links:
5+
description: Custom links to display in the topic list navigation
6+
schema:
7+
properties:
8+
display_name:
9+
label: Display name
10+
description: The text that shows on the menu
11+
title:
12+
label: Title
13+
description: The text that shows on hover
14+
url:
15+
label: URL
16+
description: The link (e.g., /latest or https://discourse.org)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export default function migrate(settings) {
2+
if (!settings.has("Nav_links")) {
3+
return settings;
4+
}
5+
6+
const oldValue = settings.get("Nav_links");
7+
settings.delete("Nav_links");
8+
9+
if (Array.isArray(oldValue)) {
10+
settings.set("nav_links", oldValue);
11+
return settings;
12+
}
13+
14+
if (typeof oldValue === "string") {
15+
const newLinks = [];
16+
17+
oldValue.split("|").forEach((link) => {
18+
const [displayName, title, url] = link
19+
.split(";")
20+
.map((section) => section.trim());
21+
22+
if (displayName && url) {
23+
const newLink = { display_name: displayName, url };
24+
25+
if (title) {
26+
newLink.title = title;
27+
}
28+
29+
newLinks.push(newLink);
30+
}
31+
});
32+
33+
settings.set("nav_links", newLinks);
34+
}
35+
36+
return settings;
37+
}

settings.yml

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1-
Nav_links:
2-
type: list
3-
default: "Latest;topics with recent posts;/latest|Categories;all topics grouped by category;/categories|Top;the most active topics in the last year, month, week or day;/top"
4-
description:
5-
en: |
6-
Semicolon delimited in this order: <strong>display name;description;URL</strong>
7-
<br/><strong>Display name:</strong> text that shows on the menu
8-
<br/><strong>Description:</strong> text that shows on hover
9-
<br/><strong>URL:</strong> the link (e.g., <code>/latest</code> or <code>https://discourse.org</code>)
1+
nav_links:
2+
type: objects
3+
default:
4+
- display_name: Latest
5+
title: topics with recent posts
6+
url: /latest
7+
- display_name: Categories
8+
title: all topics grouped by category
9+
url: /categories
10+
- display_name: Top
11+
title: the most active topics in the last year, month, week or day
12+
url: /top
13+
schema:
14+
name: link
15+
identifier: display_name
16+
properties:
17+
display_name:
18+
type: string
19+
required: true
20+
validations:
21+
min_length: 1
22+
max_length: 100
23+
title:
24+
type: string
25+
validations:
26+
min_length: 1
27+
max_length: 1000
28+
url:
29+
type: string
30+
required: true
31+
validations:
32+
min_length: 1
33+
max_length: 2048
34+
url: true
1035
Hide_dropdowns:
1136
default: false
1237
description:
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { module, test } from "qunit";
2+
import migrate from "../../../../migrations/settings/0001-migrate-nav-links-to-objects";
3+
4+
module(
5+
"Custom Top Navigation Links | Migrations | Settings | 0001-migrate-nav-links-to-objects",
6+
function () {
7+
test("does nothing when the old setting is not present", function (assert) {
8+
const settings = new Map(Object.entries({ Hide_dropdowns: true }));
9+
10+
const result = migrate(settings);
11+
12+
assert.deepEqual(
13+
Object.fromEntries(result.entries()),
14+
Object.fromEntries(new Map(Object.entries({ Hide_dropdowns: true })))
15+
);
16+
});
17+
18+
test("migrate when value of setting is already an array", function (assert) {
19+
const links = [
20+
{
21+
display_name: "Latest",
22+
title: "topics with recent posts",
23+
url: "/latest",
24+
},
25+
];
26+
const settings = new Map(Object.entries({ Nav_links: links }));
27+
28+
const result = migrate(settings);
29+
30+
assert.deepEqual(
31+
Object.fromEntries(result.entries()),
32+
Object.fromEntries(new Map(Object.entries({ nav_links: links })))
33+
);
34+
});
35+
36+
test("migrate when old setting value is an empty string", function (assert) {
37+
const settings = new Map(Object.entries({ Nav_links: "" }));
38+
39+
const result = migrate(settings);
40+
41+
assert.deepEqual(
42+
Object.fromEntries(result.entries()),
43+
Object.fromEntries(new Map(Object.entries({ nav_links: [] })))
44+
);
45+
});
46+
47+
test("migrate when old setting value is invalid", function (assert) {
48+
const settings = new Map(
49+
Object.entries({
50+
Nav_links: "Missing URL|Missing URL 2;some title|;;/no-name",
51+
})
52+
);
53+
54+
const result = migrate(settings);
55+
56+
assert.deepEqual(
57+
Object.fromEntries(result.entries()),
58+
Object.fromEntries(new Map(Object.entries({ nav_links: [] })))
59+
);
60+
});
61+
62+
test("migrate when title is not provided", function (assert) {
63+
const settings = new Map(
64+
Object.entries({
65+
Nav_links: "External link;;https://meta.discourse.org",
66+
})
67+
);
68+
69+
const result = migrate(settings);
70+
71+
assert.deepEqual(
72+
Object.fromEntries(result.entries()),
73+
Object.fromEntries(
74+
new Map(
75+
Object.entries({
76+
nav_links: [
77+
{
78+
display_name: "External link",
79+
url: "https://meta.discourse.org",
80+
},
81+
],
82+
})
83+
)
84+
)
85+
);
86+
});
87+
88+
test("migrate", function (assert) {
89+
const settings = new Map(
90+
Object.entries({
91+
Nav_links:
92+
"Latest;topics with recent posts;/latest|Categories;all topics grouped by category;/categories|Top;the most active topics in the last year, month, week or day;/top",
93+
})
94+
);
95+
96+
const result = migrate(settings);
97+
98+
assert.deepEqual(
99+
Object.fromEntries(result.entries()),
100+
Object.fromEntries(
101+
new Map(
102+
Object.entries({
103+
nav_links: [
104+
{
105+
display_name: "Latest",
106+
title: "topics with recent posts",
107+
url: "/latest",
108+
},
109+
{
110+
display_name: "Categories",
111+
title: "all topics grouped by category",
112+
url: "/categories",
113+
},
114+
{
115+
display_name: "Top",
116+
title:
117+
"the most active topics in the last year, month, week or day",
118+
url: "/top",
119+
},
120+
],
121+
})
122+
)
123+
)
124+
);
125+
});
126+
}
127+
);

0 commit comments

Comments
 (0)