Skip to content

Commit

Permalink
41 aliases not working (#42)
Browse files Browse the repository at this point in the history
Fix #41. Aliases not working.
  • Loading branch information
about-code committed Dec 4, 2019
1 parent 3a6b411 commit 271cf70
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [2.1.1](https://github.com/about-code/glossarify-md/compare/v2.1.0...v2.1.1) (2019-12-04)

### Bug Fixes

* In cases where the alias began with the term as a substring, then ocurrences of the alias were no longer linked. For example if the term is *Cat* and the alias is *Cats*, then occurrences of *Cats* were no longer linked to the glossary term *Cat*. ([#41](https://github.com/about-code/glossarify-md/issues/41)) ([4ace562](https://github.com/about-code/glossarify-md/commit/4ace562))

## [2.1.0](https://github.com/about-code/glossarify-md/compare/v2.0.0...v2.1.0) (2019-10-04)

### Features
Expand Down
12 changes: 10 additions & 2 deletions lib/term.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ class Term {

function updateRegExp(term) {

const termAndAliases = [...term.aliases, term.term];
let flags = "u";
let regExp = `(${term.term}`;
term.aliases.forEach(alias => regExp += "|" + alias);
let regExp = `(`;

// A shorter term may be a substring of a longer term which causes issues
// when the regExp is used to split a text block (see #41). Thus we sort
// sort by length descending to create a regExp which tests for the longest
// term first.
termAndAliases
.sort((term1, term2) => term2.length - term1.length)
.forEach((alias, idx) => regExp += (idx > 0 ? "|" : "") + alias);
regExp += `)`;

if (term.ignoreCase) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "glossarify-md",
"version": "2.0.1",
"version": "2.1.1",
"description": "Scans markdown files for glossary terms and replaces each occurrence with a link to a glossary file.",
"main": "lib/glossarify.js",
"repository": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ GIVEN there is the term's alias "T4 Alias1" THEN it MUST be replaced with a link
GIVEN there is the term's alias "T4-Alias2" THEN it MUST be replaced with a link to the term's definition.
GIVEN there is the term's alias "T4.Alias3" THEN it MUST be replaced with a link to the term's definition.

## Alias substring behavior

GIVEN a term "Issue"
AND an alias "Issues" WHERE *the alias* begins with the term
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "OUR"
AND an alias "FLOURISH" WHERE *the alias* includes the term
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "FIELD"
AND an alias "GREENFIELD" WHERE *the alias* ends with the term
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "Hopefully"
AND an alias "Hope" WHERE *the term* begins with the alias
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "Flower"
AND an alias "lower" WHERE *the term* ends with the alias
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "Friendship"
AND an alias "end" WHERE *the term* includes the alias
THEN the alias MUST be linked to the term, correctly.

## Unicode behavior

GIVEN a term "China"
AND alias 中国zhōngguó
AND alias zhōngguó中国
Expand Down
38 changes: 38 additions & 0 deletions test/input/config-shared/aliases_and_synonyms/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,44 @@ AND the comment ends with a trailing comma
THEN this MUST NOT result in an infinite loop or out-of-memory error
as has been reported in #26

## Alias substring behavior

### Issue
<!-- Aliases: Issues -->
GIVEN a term "Issue"
AND an alias "Issues" WHERE *the alias* begins with the term
THEN the alias MUST still be linked to the term, correctly.

### OUR
<!-- Aliases: FLOURISH -->
GIVEN a term "OUR"
AND an alias "FLOURISH" WHERE *the alias* includes the term
THEN the alias MUST still be linked to the term, correctly.

### FIELD
<!-- Aliases: GREENFIELD -->
GIVEN a term "FIELD"
AND an alias "GREENFIELD" WHERE *the alias* ends with the term
THEN the alias MUST still be linked to the term, correctly.

### Hopefully
<!-- Aliases: Hope -->
GIVEN a term "Hopefully"
AND an alias "Hope" WHERE *the term* begins with the alias
THEN the alias MUST still be linked to the term, correctly.

### Flower
<!-- Aliases: lower -->
GIVEN a term "Flower"
AND an alias "lower" WHERE *the term* ends with the alias
THEN the alias MUST still be linked to the term, correctly.

### Friendship
<!-- Aliases: end -->
GIVEN a term "Friendship"
AND an alias "end" WHERE *the term* includes the alias
THEN the alias MUST be linked to the term, correctly.


## Unicode Support

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,39 @@ GIVEN there is the term's alias "[T4 Alias1][4]" THEN it MUST be replaced with a
GIVEN there is the term's alias "[T4-Alias2][4]" THEN it MUST be replaced with a link to the term's definition.
GIVEN there is the term's alias "[T4.Alias3][4]" THEN it MUST be replaced with a link to the term's definition.

GIVEN a term "[China][5]"
AND alias [中国zhōngguó][5]
AND alias [zhōngguó中国][5]
AND alias [中zhōngguó国][5]
AND alias [zhōng中国guó][5]
## Alias substring behavior

GIVEN a term "[Issue][5]"
AND an alias "[Issues][5]" WHERE _the alias_ begins with the term
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "[OUR][6]"
AND an alias "[FLOURISH][6]" WHERE _the alias_ includes the term
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "[FIELD][7]"
AND an alias "[GREENFIELD][7]" WHERE _the alias_ ends with the term
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "[Hopefully][8]"
AND an alias "[Hope][8]" WHERE _the term_ begins with the alias
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "[Flower][9]"
AND an alias "[lower][9]" WHERE _the term_ ends with the alias
THEN the alias MUST still be linked to the term, correctly.

GIVEN a term "[Friendship][10]"
AND an alias "[end][10]" WHERE _the term_ includes the alias
THEN the alias MUST be linked to the term, correctly.

## Unicode behavior

GIVEN a term "[China][11]"
AND alias [中国zhōngguó][11]
AND alias [zhōngguó中国][11]
AND alias [中zhōngguó国][11]
AND alias [zhōng中国guó][11]
WITH aliases containing non-ASCII unicode characters
THEN all aliases MUST still be separated and linked correctly

Expand All @@ -39,6 +67,30 @@ subsequent comma-separated lines of words MUST be detected as the term's aliases
line THEN the subsequent comma-separated lines of words MUST be detected as the
term's aliases."

[5]: glossary.md#china "GIVEN there is an HTML-single-line-comment beginning with 'Aliases:'
[5]: glossary.md#issue 'GIVEN a term "Issue"
AND an alias "Issues" WHERE the alias begins with the term
THEN the alias MUST still be linked to the term, correctly.'

[6]: glossary.md#our 'GIVEN a term "OUR"
AND an alias "FLOURISH" WHERE the alias includes the term
THEN the alias MUST still be linked to the term, correctly.'

[7]: glossary.md#field 'GIVEN a term "FIELD"
AND an alias "GREENFIELD" WHERE the alias ends with the term
THEN the alias MUST still be linked to the term, correctly.'

[8]: glossary.md#hopefully 'GIVEN a term "Hopefully"
AND an alias "Hope" WHERE the term begins with the alias
THEN the alias MUST still be linked to the term, correctly.'

[9]: glossary.md#flower 'GIVEN a term "Flower"
AND an alias "lower" WHERE the term ends with the alias
THEN the alias MUST still be linked to the term, correctly.'

[10]: glossary.md#friendship 'GIVEN a term "Friendship"
AND an alias "end" WHERE the term includes the alias
THEN the alias MUST be linked to the term, correctly.'

[11]: glossary.md#china "GIVEN there is an HTML-single-line-comment beginning with 'Aliases:'
AND aliases contain unicode word characters
THEN they MUST still be separated correctly"
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,56 @@ AND the comment ends with a trailing comma
THEN this MUST NOT result in an infinite loop or out-of-memory error
as has been reported in #26

## [Alias substring behavior](#alias-substring-behavior)

### [Issue](#issue)

<!-- Aliases: Issues -->

GIVEN a term "Issue"
AND an alias "Issues" WHERE _the alias_ begins with the term
THEN the alias MUST still be linked to the term, correctly.

### [OUR](#our)

<!-- Aliases: FLOURISH -->

GIVEN a term "OUR"
AND an alias "FLOURISH" WHERE _the alias_ includes the term
THEN the alias MUST still be linked to the term, correctly.

### [FIELD](#field)

<!-- Aliases: GREENFIELD -->

GIVEN a term "FIELD"
AND an alias "GREENFIELD" WHERE _the alias_ ends with the term
THEN the alias MUST still be linked to the term, correctly.

### [Hopefully](#hopefully)

<!-- Aliases: Hope -->

GIVEN a term "Hopefully"
AND an alias "Hope" WHERE _the term_ begins with the alias
THEN the alias MUST still be linked to the term, correctly.

### [Flower](#flower)

<!-- Aliases: lower -->

GIVEN a term "Flower"
AND an alias "lower" WHERE _the term_ ends with the alias
THEN the alias MUST still be linked to the term, correctly.

### [Friendship](#friendship)

<!-- Aliases: end -->

GIVEN a term "Friendship"
AND an alias "end" WHERE _the term_ includes the alias
THEN the alias MUST be linked to the term, correctly.

## [Unicode Support](#unicode-support)

### [China](#china)
Expand Down
Loading

0 comments on commit 271cf70

Please sign in to comment.