Skip to content

Commit 48397a3

Browse files
Bugfix/relative glossary link (#9)
* BUGFIX: link behaviour with relative glossary path - Add failing test to illustrate the problem (RED) - Add configuration parameter to set replacement link prefixes - for use with custom set-ups (such as c4builder variant of docsify) * Implement bugfix [GREEN]
1 parent a1fe311 commit 48397a3

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stijn-dejongh/docsify-glossary",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A simple glossary plugin for docsify, forked from the original work by TheGreenToaster",
55
"main": "dist/docsify-glossary.js",
66
"unpkg": "dist/docsify-tabs.min.js",

src/main/js/configuration.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class GlossaryConfigurationBuilder {
66
glossaryLocation = '';
77
debug = false;
88
replaceTitleTerms = true;
9+
linkPrefix = '';
910
constructor() {
1011
this.terminologyHeading = DEFAULT_TERM_HEADING;
1112
this.glossaryLocation = DEFAULT_GLOSSARY_FILE_NAME;
@@ -21,6 +22,11 @@ export class GlossaryConfigurationBuilder {
2122
return this;
2223
}
2324

25+
withLinkPrefix(linkPrefix) {
26+
this.linkPrefix = linkPrefix;
27+
return this;
28+
}
29+
2430
withDebugEnabled(enableDebug) {
2531
this.debug = enableDebug;
2632
return this;
@@ -42,6 +48,7 @@ export function configFromYaml(configurationYaml) {
4248
.withGlossaryLocation(configurationYaml.glossaryLocation ?? DEFAULT_GLOSSARY_FILE_NAME)
4349
.withDebugEnabled(configurationYaml.debug ?? false)
4450
.withTitleTermReplacement(configurationYaml.replaceTitleTerms ?? true)
51+
.withLinkPrefix(configurationYaml.linkPrefix ?? '')
4552
.build();
4653
}
4754

src/main/js/glossary.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ function replaceTermInLine(term, contentLine, linkId, config) {
99
let reComma = new RegExp(`\\s(${term}),`, 'ig');
1010
let reFullStop = new RegExp(`\\s(${term})\\.`, 'ig');
1111

12-
let link = ` [$1](/${config.glossaryLocation.replace('.md', '')}?id=${linkId})`;
12+
let compiledLink = config.glossaryLocation
13+
.replace('./', `${config.linkPrefix}/`)
14+
.replace('.md', '');
15+
let link = ` [$1](/${compiledLink}?id=${linkId})`;
1316

1417
let replacement = contentLine.replace(reComma, link + ',')
1518
.replace(re, link + ' ')

src/test/js/glossary_test.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,23 @@ describe('Glossary location', () => {
219219

220220
expect([...result.matchAll(`${glossaryLocation}\\?id=api`)]).toHaveLength(2);
221221
});
222+
223+
// Issue with links referring to relative paths after compilation
224+
// example case:
225+
// glossaryLocation: ./X_Appendix/Glossary/HOME.md
226+
// resulting link: http://localhost:3000/#/./X_Appendix/Glossary/HOME?id=practices
227+
it(' can handle relative link configurations', () => {
228+
let glossaryLocation = './alternate/location/glossary/README';
229+
config = glossifyConfig()
230+
.withGlossaryLocation(glossaryLocation+'.md')
231+
.withLinkPrefix('#')
232+
.build();
233+
234+
dictionary = loadTerminology(sourceText, config);
235+
expect(dictionary['API']).toBeTruthy();
236+
237+
let result = addLinks(textWithTerminologyUsage, dictionary, config);
238+
expect([...result.matchAll('/#/alternate/location/glossary/README\\?id=api')]).toHaveLength(2);
239+
});
222240
});
223241

0 commit comments

Comments
 (0)