Skip to content

Commit 29e5fed

Browse files
authored
[BUGFIX]: Fix Help page linking error (openscd#1594)
1 parent 14e933e commit 29e5fed

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

packages/plugins/src/menu/Help.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { newWizardEvent, Wizard } from '@openscd/open-scd/src/foundation.js';
99
import { openSCDIcon } from '@openscd/open-scd/src/icons/icons.js';
1010
import { Directory } from '@openscd/open-scd/src/finder-list.js';
1111

12+
const GITHUB_WIKI_LINK_PATTERN = /https:\/\/github\.com\/openscd\/open-scd\/wiki\/([^)]*)/g;
13+
const MD_LINK_TITLE_PATTERN ='([^\\]]*)';
14+
const HYPHEN_PATTERN = /-/g;
15+
1216
function aboutBox(version: string) {
1317
return html`<div>
1418
<div style="display:flex">
@@ -43,19 +47,22 @@ async function getLinkedPages(path: string[]): Promise<Directory> {
4347
}
4448

4549
const page = path[path.length - 1].replace(/ /g, '-');
46-
const res = await fetch(`/public/md/${page}.md`);
50+
const res = await fetch(`/openscd/public/md/${page}.md`);
4751
const md = await res.text();
52+
const MD_LINK_REPLACEMENT = `<a style="cursor: help; color: var(--mdc-theme-primary)" href="https://github.com/openscd/open-scd/wiki/$2" target="_blank" >$1</a>`
4853
const unlinkedMd = md.replace(
49-
/\[([^\]]*)\]\(https:..github.com.openscd.open-scd.wiki.([^)]*)\)/g,
50-
`<a style="cursor: help;" onclick="Array.from(event.target.closest('section').lastElementChild.children).find(child => child.text === '$2'.replace(/-/g, ' ')).click()">$1</a>`
54+
new RegExp(`\\[${MD_LINK_TITLE_PATTERN}\\]\\(${GITHUB_WIKI_LINK_PATTERN.source}\\)`, 'g'),
55+
MD_LINK_REPLACEMENT
5156
);
57+
5258
const header = html`<div style="padding: 8px;">
5359
${page === 'Home' ? aboutBox(edition.version) : html``}
5460
${unsafeHTML(marked.parse(unlinkedMd))}
5561
</div>`;
5662
const entries = Array.from(
57-
md.matchAll(/\(https:..github.com.openscd.open-scd.wiki.([^)]*)\)/g)
58-
).map(([_, child]) => child.replace(/-/g, ' '));
63+
md.matchAll( new RegExp(`\\(${GITHUB_WIKI_LINK_PATTERN.source}\\)`, 'g'))
64+
65+
).map(([_, child]) => child.replace(HYPHEN_PATTERN, ' '));
5966

6067
return { path, header, entries };
6168
}

0 commit comments

Comments
 (0)