Skip to content

Commit 0ea716f

Browse files
Bugfix/spaces in headings (#6)
* Fix README typos * Add failing test for code-block text replacement behaviour * Change implementation to make test pass * Update the example to demonstrate the bug fix * Add screenshot to the readme
1 parent 05b59da commit 0ea716f

File tree

8 files changed

+118
-25
lines changed

8 files changed

+118
-25
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Simple Glossary for Docsify that replaces occurrences of words in text with link
66
Forked from [TheGreenToaster/docsify-glossary](https://github.com/TheGreenToaster/docsify-glossary) as the original
77
project was unmaintained for over 3 years, to address a couple of usability issues with the original script.
88

9-
An example usage can be found here [./example](./example), it is deployed automatically to this [github page](https://stijn-dejongh.github.io/docsify-glossary/#/).
9+
An example usage can be found here [./example](./example), it is deployed automatically to this [GitHub page](https://stijn-dejongh.github.io/docsify-glossary/#/).
10+
11+
![Example of the glossary replacements](./example_output.png)
1012

1113
## Installation
1214

@@ -19,9 +21,9 @@ An example usage can be found here [./example](./example), it is deployed automa
1921
1. Create a `_glossary.md` file in the root directory
2022
2. Populate the `_glossary.md` file with terms.
2123

22-
## Plugim Usage
24+
## Plugin Usage
2325

24-
* Terms must be predicated with a consistent markdown heading to get recognized by the glossary (see configuration)
26+
* Terms must be predicated with a consistent Markdown heading to get recognized by the glossary (see configuration)
2527
* Terms are replaced with links in the order that they appear in the glossary file.
2628
* This is especially relevant for nested terminology ( e.g. _API_ and _API Usage_)
2729

@@ -41,10 +43,13 @@ We recommend using [Node Version Manager](https://npm.github.io/installation-set
4143

4244
Once the code has been built, you can launch the example website illustrating the use of the glossary.
4345
In order to do so:
44-
3. Go to [http://localhost:3000/]()
46+
47+
1. Go to [http://localhost:3000/]()
48+
2. copy the latest version of the code into the example website: `cp ./dist/@stijn-dejongh/docsify-glossary* ./example`
49+
3. Run `docsify serve example`
4550

4651
## Changelog
47-
Simple Glossary for Docsify
52+
4853
An overview of all the changes made to this codebase can be found in the [CHANGELOG](./CHANGELOG.md) file included in this repository.
4954

5055
## TODO list
@@ -54,9 +59,8 @@ An overview of all the changes made to this codebase can be found in the [CHANGE
5459
* [x] make glossary file name/location configurable, see [feature request #1](https://github.com/TheGreenToaster/docsify-glossary/issues/1)
5560
* [x] make terminology heading depth configurable, see [feature request #1](https://github.com/TheGreenToaster/docsify-glossary/issues/1)
5661
* [x] fix issue with terminology replacements in page headers/titles, see: [bug report #6](https://github.com/TheGreenToaster/docsify-glossary/issues/6)
57-
* [ ] fix issue with terminology replacements in code blocks, see: [bug report #4](https://github.com/TheGreenToaster/docsify-glossary/issues/4)
62+
* [x] fix issue with terminology replacements in code blocks, see: [bug report #4](https://github.com/TheGreenToaster/docsify-glossary/issues/4)
5863
* [x] fix issue with multiple word terms, see: [bug report #13]([bug report #13](https://github.com/TheGreenToaster/docsify-glossary/issues/13))
5964

6065

61-
1. copy the latest version of the code into the example website: `cp ./dist/@stijn-dejongh/docsify-glossary* ./example`
62-
2. Run `docsify serve example`
66+

example/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ Mizzen galleon aye Pirate Round capstan grapple jolly boat American Main tackle
55
Yellow Jack case shot Nelsons folly ye interloper gangway Sink me heave to capstan lanyard. Starboard bucko long boat chase spike gunwalls piracy log aft brigantine. Skysail piracy fathom sheet gangway transom boom Spanish Main tackle pressgang.
66

77
Crack Jennys tea cup Buccaneer broadside Cat o'nine tails weigh anchor bilge water scurvy jolly boat crimp haul wind. Grog take a caulk brigantine spanker haul wind knave Yellow Jack bounty poop deck red ensign. Hogshead scourge of the seven seas interloper Admiral of the Black draught pillage black spot trysail hang the jib marooned.
8-
9-
8+
9+
Here is some code to help the English pirate hunters find the notorious pirate Jack Rackham:
10+
```java
11+
public void findJackRakham() {
12+
for(Pirate pirate : islandPopulation.getPirates()) {
13+
// The infamous pirate admiral Jack Rackham? Oh look, there he is!
14+
return pirate.hasFirstName("Jack") && pirate.hasLastName("Rackham");
15+
}
16+
}
17+
```
18+
19+
1020
Yellow Jack was an Admiral, Admiral, Admiral. (This Admiral line was added to demonstrate multiple replacements, as mentioned in [issue nr 13](https://github.com/TheGreenToaster/docsify-glossary/issues/13))

example/_glossary.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66

77
A commander of a fleet or naval squadron, or a naval officer of very high rank.
88

9-
## B - X
9+
## B - I
1010

1111
Nothing here....
1212

13+
## J
14+
15+
### Jack Rackham
16+
17+
Jack Rackham was a gentleman pirate with flamboyant fashion sense and a way with female pirates who were dressed like men– notably, Anne Bonny and Mary Read.
18+
19+
20+
## K - X
21+
1322
## Y
1423

1524
### Yellow Jack

example/docsify-glossary.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ this["@stijn-dejongh/docsify-glossary"] = this["@stijn-dejongh/docsify-glossary"
99

1010
this["@stijn-dejongh/docsify-glossary"].js = function(exports) {
1111
"use strict";
12-
function replaceTermInLine(term, contentLine, linkId) {
12+
function replaceTermInLine(term, contentLine, linkId, config) {
13+
if (isTitle(contentLine) && !config.replaceTitleTerms) {
14+
return contentLine;
15+
}
1316
var re = new RegExp("\\s(".concat(term, ")[\\s$]"), "ig");
1417
var reFullStop = new RegExp("\\s(".concat(term, ")."), "ig");
1518
var reComma = new RegExp("\\s(".concat(term, "),"), "ig");
@@ -20,11 +23,17 @@ this["@stijn-dejongh/docsify-glossary"].js = function(exports) {
2023
function isTitle(line) {
2124
return line.trim().startsWith("#");
2225
}
23-
function replaceTerm(content, term, linkId) {
24-
var contentLines = content.split("\n");
26+
function replaceTerm(content, term, linkId, config) {
2527
var processedText = "";
26-
contentLines.forEach((function(line, _index) {
27-
var replacedLine = line.trim().length > 0 ? replaceTermInLine(term, line + " ", linkId).trimEnd() : line;
28+
var codeBlockContext = false;
29+
content.split("\n").forEach((function(line, _index) {
30+
if (line.trim().startsWith("```")) {
31+
codeBlockContext = !codeBlockContext;
32+
}
33+
var replacedLine = line;
34+
if (line.trim().length > 0 && !codeBlockContext) {
35+
replacedLine = replaceTermInLine(term, line + " ", linkId, config).trimEnd();
36+
}
2837
processedText += replacedLine + "\n";
2938
}));
3039
return processedText;
@@ -35,7 +44,7 @@ this["@stijn-dejongh/docsify-glossary"].js = function(exports) {
3544
console.log("Adding links for terminology: ".concat(terms));
3645
}
3746
for (var term in terms) {
38-
textWithReplacements = replaceTerm(textWithReplacements, term, terms[term]);
47+
textWithReplacements = replaceTerm(textWithReplacements, term, terms[term], config);
3948
}
4049
return textWithReplacements;
4150
}
@@ -117,6 +126,7 @@ this["@stijn-dejongh/docsify-glossary"].js = function(exports) {
117126
_defineProperty(this, "terminologyHeading", "");
118127
_defineProperty(this, "glossaryLocation", "");
119128
_defineProperty(this, "debug", false);
129+
_defineProperty(this, "replaceTitleTerms", true);
120130
this.terminologyHeading = DEFAULT_TERM_HEADING;
121131
this.glossaryLocation = DEFAULT_GLOSSARY_FILE_NAME;
122132
}
@@ -138,6 +148,12 @@ this["@stijn-dejongh/docsify-glossary"].js = function(exports) {
138148
this.debug = enableDebug;
139149
return this;
140150
}
151+
}, {
152+
key: "withTitleTermReplacement",
153+
value: function withTitleTermReplacement(enableTitleTermReplacement) {
154+
this.replaceTitleTerms = enableTitleTermReplacement;
155+
return this;
156+
}
141157
}, {
142158
key: "build",
143159
value: function build() {
@@ -147,8 +163,8 @@ this["@stijn-dejongh/docsify-glossary"].js = function(exports) {
147163
return GlossaryConfigurationBuilder;
148164
}();
149165
function configFromYaml(configurationYaml) {
150-
var _configurationYaml$te, _configurationYaml$gl, _configurationYaml$de;
151-
return (new GlossaryConfigurationBuilder).withTermHeading((_configurationYaml$te = configurationYaml.terminologyHeading) !== null && _configurationYaml$te !== void 0 ? _configurationYaml$te : DEFAULT_TERM_HEADING).withGlossaryLocation((_configurationYaml$gl = configurationYaml.glossaryLocation) !== null && _configurationYaml$gl !== void 0 ? _configurationYaml$gl : DEFAULT_GLOSSARY_FILE_NAME).withDebugEnabled((_configurationYaml$de = configurationYaml.debug) !== null && _configurationYaml$de !== void 0 ? _configurationYaml$de : false).build();
166+
var _configurationYaml$te, _configurationYaml$gl, _configurationYaml$de, _configurationYaml$re;
167+
return (new GlossaryConfigurationBuilder).withTermHeading((_configurationYaml$te = configurationYaml.terminologyHeading) !== null && _configurationYaml$te !== void 0 ? _configurationYaml$te : DEFAULT_TERM_HEADING).withGlossaryLocation((_configurationYaml$gl = configurationYaml.glossaryLocation) !== null && _configurationYaml$gl !== void 0 ? _configurationYaml$gl : DEFAULT_GLOSSARY_FILE_NAME).withDebugEnabled((_configurationYaml$de = configurationYaml.debug) !== null && _configurationYaml$de !== void 0 ? _configurationYaml$de : false).withTitleTermReplacement((_configurationYaml$re = configurationYaml.replaceTitleTerms) !== null && _configurationYaml$re !== void 0 ? _configurationYaml$re : true).build();
152168
}
153169
function defaultGlossifyConfig() {
154170
return (new GlossaryConfigurationBuilder).build();

example/docsify-glossary.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_output.png

110 KB
Loading

src/main/js/glossary.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function replaceTermInLine(term, contentLine, linkId, config) {
22
if(isTitle(contentLine) && !config.replaceTitleTerms) {
3-
// Intentially not combined in the return statement, to avoid superfluous calculations
3+
// Intentionally not combined in the return statement, to avoid superfluous calculations
44
return contentLine;
55
}
66

@@ -23,13 +23,21 @@ function isTitle(line) {
2323
}
2424

2525
export function replaceTerm(content, term, linkId, config) {
26-
let contentLines = content.split('\n');
2726
let processedText = '';
2827

29-
contentLines.forEach( (line, _index) => {
30-
let replacedLine = line.trim().length > 0 ? replaceTermInLine(term, line + ' ', linkId, config).trimEnd() : line;
28+
let codeBlockContext = false;
29+
content.split('\n').forEach( (line, _index) => {
30+
if(line.trim().startsWith('```')) {
31+
codeBlockContext = !codeBlockContext;
32+
}
33+
34+
let replacedLine = line;
35+
if(line.trim().length > 0 && !codeBlockContext) {
36+
replacedLine = replaceTermInLine(term, line + ' ', linkId, config).trimEnd();
37+
}
3138
processedText += replacedLine + '\n';
3239
});
40+
3341
return processedText;
3442
}
3543

0 commit comments

Comments
 (0)