-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Generate a List of Figures with option `generateFiles.listOfFigures`. More see README.md. (#61)
- Loading branch information
1 parent
792253e
commit 2c57ace
Showing
49 changed files
with
1,312 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ api.getNodeText = function getNodeText(node) { | |
} else { | ||
return; | ||
} | ||
|
||
}; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const {root, paragraph, text, heading, brk, link, list, listItem } = require("mdast-builder"); | ||
|
||
const {getFileLinkUrl} = require("./path/tools"); | ||
const {getLinkUrl, getNodeText} = require("./ast/tools"); | ||
const {getNodeIndex, getDefinitionIndex, groupByHeading} = require("./indexer"); | ||
|
||
const api = {}; | ||
|
||
/** | ||
* @typedef { import('./model/context') } Context | ||
* @typedef { import('./indexer').IndexEntry } IndexEntry | ||
* @typedef { import('./indexer').Index } Index | ||
*/ | ||
|
||
/** | ||
* Returns the markdown abstract syntax tree that is to be written to the file | ||
* configured via 'generateFiles.indexFile' config. | ||
* | ||
* @param {Context} context | ||
* @returns {Node} mdast tree | ||
*/ | ||
api.getAST = function(context) { | ||
const {generateFiles, indexing} = context.opts; | ||
const {groupByHeadingDepth} = indexing; | ||
const {title} = generateFiles.listOfFigures; | ||
const figures = selectFiguresFromIndex(context); | ||
|
||
let tree = [ | ||
heading(1, text(title || "Figures")) | ||
]; | ||
if (! groupByHeadingDepth || groupByHeadingDepth < 0) { | ||
tree.push(getListOfFiguresAst(context, figures)); | ||
} else { | ||
tree.push(getFiguresBySectionAst(context, figures)); | ||
} | ||
return root(tree); | ||
}; | ||
|
||
/** | ||
* @param {Context} context | ||
* @param {IndexEntry[]} figures | ||
*/ | ||
function getFiguresBySectionAst(context, figures) { | ||
return paragraph( | ||
groupByHeading(figures).map((figures) => { | ||
const groupHeadingNode = figures[0].groupHeadingNode; | ||
return paragraph([ | ||
// add +1 to depth of headings referred to in order to keep | ||
// the title of the generated file the only depth-1 heading | ||
brk | ||
,heading(groupHeadingNode.depth + 1, text(getNodeText(groupHeadingNode))) | ||
,brk | ||
,brk | ||
,getListOfFiguresAst(context, figures) | ||
,brk | ||
]); | ||
}) | ||
); | ||
} | ||
|
||
/** | ||
* @param {Context} context | ||
* @param {IndexEntry[]} figures | ||
* @returns {Node} mdast tree | ||
*/ | ||
function getListOfFiguresAst(context, figures) { | ||
return list( | ||
"ordered" | ||
,figures | ||
.sort((entry1, entry2) => entry1.id - entry2.id) | ||
.map((indexEntry) => getListOfFiguresItemAst(context, indexEntry)) | ||
); | ||
} | ||
|
||
/** | ||
* @param {Context} context | ||
* @param {IndexEntry} indexEntry | ||
* @returns {Node} mdast tree | ||
*/ | ||
function getListOfFiguresItemAst(context, indexEntry) { | ||
const {listOfFigures} = context.opts.generateFiles; | ||
const {file: listOfFiguresFile} = listOfFigures; | ||
|
||
return listItem([ | ||
link( | ||
getFileLinkUrl(context, listOfFiguresFile, indexEntry.file, getLinkUrl(indexEntry.headingNode)) | ||
,indexEntry.node.title | ||
,text(indexEntry.node.alt) | ||
) | ||
]); | ||
} | ||
|
||
/** | ||
* @returns {IndexEntry[]} index entries for node types "image" and "imageReference" | ||
*/ | ||
function selectFiguresFromIndex() { | ||
const images = getNodeIndex("image"); | ||
const imageRefs = getNodeIndex("imageReference"); | ||
const defIndex = getDefinitionIndex(); | ||
let figures = [ | ||
...images | ||
,...imageRefs.map((indexEntry) => { | ||
// dereference | ||
const refFile = indexEntry.file; | ||
const refNode = indexEntry.node; | ||
const defId = `${refFile}#${refNode.identifier}`; | ||
const defNode = defIndex[defId].node; | ||
indexEntry.node = { | ||
...indexEntry.node | ||
, type: "image" | ||
, url: defNode.url | ||
, title : defNode.title | ||
}; | ||
return indexEntry; | ||
}) | ||
]; | ||
return figures; | ||
} | ||
|
||
module.exports = api; |
Oops, something went wrong.