Skip to content

Commit

Permalink
fix: Option indexFiles: Incorrect path from index to section of occur…
Browse files Browse the repository at this point in the history
…rence. (#156)

* test: Updated test case.
* fix: Option indexFiles: Incorrect path from index to section of occurrence.
* test: New baseline.
* fix (upgrade): Add missing migration for indexFile (string to object);
  • Loading branch information
about-code authored Apr 17, 2021
1 parent 45717f7 commit 41e59ad
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 47 deletions.
8 changes: 7 additions & 1 deletion lib/cli/upgrade/v4to5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {liftSchemaVersion} = require("./version");
const api = {};

api.v4To5 = function(confData, confVersion) {
if (confVersion > 4) {
if (confVersion >= 5) {
return;
}
if (typeof confData.linking === "string") {
Expand All @@ -16,6 +16,12 @@ api.v4To5 = function(confData, confVersion) {
confData.linking = Object.assign({}, confData.linking, { baseUrl: confData.baseUrl });
delete confData.baseUrl;
}
if (confData.generateFiles) {
const {indexFile} = confData.generateFiles;
if (typeof indexFile === "string") {
confData.generateFiles.indexFile = { file: indexFile };
}
}
if (confData.$schema) {
if (/node_modules/.test(confData.$schema)) {
confData.$schema = "./node_modules/glossarify-md/conf.schema.json";
Expand Down
26 changes: 8 additions & 18 deletions lib/index/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ api.getAST = function (context, indexFileConf) {
indexEntries = getIndex(api.IDX_OCCURRENCES_BY_TEXT);
}

const indexFile = indexFileConf.file;
let title = "";
if (indexFileConf !== null && typeof indexFileConf === "object") {
title = indexFileConf.title;
Expand All @@ -98,7 +99,7 @@ api.getAST = function (context, indexFileConf) {
,...Object
.keys(indexEntries)
.sort((key1, key2) => collator.compare(key1, key2))
.map((key) => getIndexEntryAst(context, indexEntries[key]))
.map((key) => getIndexEntryAst(context, indexEntries[key], indexFile))
];
return root(tree);
};
Expand All @@ -110,28 +111,27 @@ api.getAST = function (context, indexFileConf) {
* @param {string} indexFilename
* @returns {Node} mdast tree
*/
function getIndexEntryAst(context, entriesForKey) {
function getIndexEntryAst(context, entriesForKey, indexFile) {
const key = getNodeText(entriesForKey[0].node);
return paragraph([
heading(2, text(key))
,brk
,brk
,...getEntryLinksAst(context, entriesForKey)
,...getEntryLinksAst(context, entriesForKey, indexFile)
]);
}

/**
*
* @param {Context} context
* @param {IndexEntry} indexEntry
* @param {string} indexFilename
* @param {string} indexFile
*/
function getEntryLinksAst(context, entriesForKey) {
const indexFilename = getIndexFilename(context);
function getEntryLinksAst(context, entriesForKey, indexFile) {
const byHeadings = group(entriesForKey, byGroupHeading);
const links = [
...getGlossaryLinksAst(context, entriesForKey, indexFilename)
,...getDocumentLinksAst(context, byHeadings, indexFilename)
...getGlossaryLinksAst(context, entriesForKey, indexFile)
,...getDocumentLinksAst(context, byHeadings, indexFile)
];
const linksSeparated = [];
for (let i = 0, len = links.length; i < len; i++) {
Expand Down Expand Up @@ -230,14 +230,4 @@ function byHeading(indexEntry) {
return `${indexEntry.file}#${pos}:${anchor}`;
}

/**
* @param {Context} context
*/
function getIndexFilename(context) {
const {indexFile} = context.conf.generateFiles;
if (indexFile && typeof indexFile === "object") {
return indexFile.file;
}
}

module.exports = api;
24 changes: 18 additions & 6 deletions test/input/config-indexFiles/154-deep-dir/document.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ AND a configuration
{
"generateFiles": {
"indexFiles": [
{ "title": "Index A", "file": "./index-a.md", "glossary": "./glossary-a.md" },
{ "title": "Index B", "file": "./index-b.md", "glossary": "./sub1/glossary-b.md" },
{ "title": "Index C", "file": "./index-c.md", "glossary": "./sub1/sub2/glossary-c.md" }
{
"title": "Index A", "file": "./index-a.md", "glossary": "./glossary-a.md"
},
{
"title": "Index B", "file": "./sub1-index/index-b.md", "glossary": "./sub1/glossary-b.md"
},
{
"title": "Index C", "file": "./sub1-index/sub2-index/index-c.md", "glossary": "./sub1/sub2/glossary-c.md"
}
]
},
"glossaries": [
{ "file": "./**/glossary-*.md" }
{
"file": "./**/glossary-*.md"
}
]
}
~~~
Expand All @@ -30,7 +38,11 @@ THEN the system MUST generate three files

- index-a.md
- WITH terms GlossaryA_Term1, GlossaryA_Term2, GlossaryABC_Term3, only
- index-b.md
- sub1-index/index-b.md
- WITH terms GlossaryB_Term1, GlossaryB_Term2, GlossaryABC_Term3, only
- index-c.md
- sub1-index/sub2-index/index-c.md
- WITH terms GlossaryC_Term1, GlossaryC_Term2, GlossaryABC_Term3, only

AND

- links from within indexes at each depth MUST resolve to the document's section of occurrence.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"title": "Index A", "file": "./index-a.md", "glossary": "./glossary-a.md"
},
{
"title": "Index B", "file": "./index-b.md", "glossary": "./sub1/glossary-b.md"
"title": "Index B", "file": "./sub1-index/index-b.md", "glossary": "./sub1/glossary-b.md"
},
{
"title": "Index C", "file": "./index-c.md", "glossary": "./sub1/sub2/glossary-c.md"
"title": "Index C", "file": "./sub1-index/sub2-index/index-c.md", "glossary": "./sub1/sub2/glossary-c.md"
}
]
},
Expand Down
24 changes: 18 additions & 6 deletions test/output-expected/config-indexFiles/154-deep-dir/document.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ AND a configuration
{
"generateFiles": {
"indexFiles": [
{ "title": "Index A", "file": "./index-a.md", "glossary": "./glossary-a.md" },
{ "title": "Index B", "file": "./index-b.md", "glossary": "./sub1/glossary-b.md" },
{ "title": "Index C", "file": "./index-c.md", "glossary": "./sub1/sub2/glossary-c.md" }
{
"title": "Index A", "file": "./index-a.md", "glossary": "./glossary-a.md"
},
{
"title": "Index B", "file": "./sub1-index/index-b.md", "glossary": "./sub1/glossary-b.md"
},
{
"title": "Index C", "file": "./sub1-index/sub2-index/index-c.md", "glossary": "./sub1/sub2/glossary-c.md"
}
]
},
"glossaries": [
{ "file": "./**/glossary-*.md" }
{
"file": "./**/glossary-*.md"
}
]
}
```
Expand All @@ -31,11 +39,15 @@ THEN the system MUST generate three files

* index-a.md
* WITH terms [GlossaryA_Term1][1], [GlossaryA_Term2][2], [GlossaryABC_Term3][7][<sup>2)</sup>][8][<sup> 3)</sup>][9], only
* index-b.md
* sub1-index/index-b.md
* WITH terms [GlossaryB_Term1][3], [GlossaryB_Term2][4], [GlossaryABC_Term3][7][<sup>2)</sup>][8][<sup> 3)</sup>][9], only
* index-c.md
* sub1-index/sub2-index/index-c.md
* WITH terms [GlossaryC_Term1][5], [GlossaryC_Term2][6], [GlossaryABC_Term3][7][<sup>2)</sup>][8][<sup> 3)</sup>][9], only

AND

* links from within indexes at each depth MUST resolve to the document's section of occurrence.

[1]: ./glossary-a.md#glossarya_term1

[2]: ./glossary-a.md#glossarya_term2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"title": "Index A", "file": "./index-a.md", "glossary": "./glossary-a.md"
},
{
"title": "Index B", "file": "./index-b.md", "glossary": "./sub1/glossary-b.md"
"title": "Index B", "file": "./sub1-index/index-b.md", "glossary": "./sub1/glossary-b.md"
},
{
"title": "Index C", "file": "./index-c.md", "glossary": "./sub1/sub2/glossary-c.md"
"title": "Index C", "file": "./sub1-index/sub2-index/index-c.md", "glossary": "./sub1/sub2/glossary-c.md"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

[Glossary B][6][Testing config indexFiles with multiple index files][4]

[1]: ./glossary-a.md#glossaryabc_term3
[1]: ../glossary-a.md#glossaryabc_term3

[2]: ./sub1/glossary-b.md#glossaryabc_term3
[2]: ../sub1/glossary-b.md#glossaryabc_term3

[3]: ./sub1/sub2/glossary-c.md#glossaryabc_term3
[3]: ../sub1/sub2/glossary-c.md#glossaryabc_term3

[4]: ./document.md#testing-config-indexfiles-with-multiple-index-files
[4]: ../document.md#testing-config-indexfiles-with-multiple-index-files

[5]: ./sub1/glossary-b.md#glossaryb_term1
[5]: ../sub1/glossary-b.md#glossaryb_term1

[6]: ./sub1/glossary-b.md#glossaryb_term2
[6]: ../sub1/glossary-b.md#glossaryb_term2
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

[Glossary C][6][Testing config indexFiles with multiple index files][4]

[1]: ./glossary-a.md#glossaryabc_term3
[1]: ../../glossary-a.md#glossaryabc_term3

[2]: ./sub1/glossary-b.md#glossaryabc_term3
[2]: ../../sub1/glossary-b.md#glossaryabc_term3

[3]: ./sub1/sub2/glossary-c.md#glossaryabc_term3
[3]: ../../sub1/sub2/glossary-c.md#glossaryabc_term3

[4]: ./document.md#testing-config-indexfiles-with-multiple-index-files
[4]: ../../document.md#testing-config-indexfiles-with-multiple-index-files

[5]: ./sub1/sub2/glossary-c.md#glossaryc_term1
[5]: ../../sub1/sub2/glossary-c.md#glossaryc_term1

[6]: ./sub1/sub2/glossary-c.md#glossaryc_term2
[6]: ../../sub1/sub2/glossary-c.md#glossaryc_term2

0 comments on commit 41e59ad

Please sign in to comment.