Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group globals by helper #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exports.tableHeadHtml = tableHeadHtml
exports.tableRow = tableRow
exports.deprecated = deprecated
exports.groupBy = groupBy
exports.groupGlobalsBy = groupGlobalsBy
exports._groupBy = _groupBy
exports._addGroup = _addGroup
exports.add = add
Expand All @@ -40,16 +41,18 @@ function escape (input) {
}

/**
replaces {@link} tags with markdown links in the suppied input text
replaces {@link}, {@linkplain}, and {@linkcode} tags with markdown links in the supplied input text
*/
function inlineLinks (text, options) {
if (text) {
const links = ddata.parseLink(text)
const dmdOptions = options.data.root.options
const links = ddata.parseLink(text, dmdOptions)
links.forEach(function (link) {
const captionFmt = link.format === 'code' ? '`' : ''
const linked = ddata._link(link.url, options)
if (link.caption === link.url) link.caption = linked.name
if (linked.url) link.url = linked.url
text = text.replace(link.original, '[' + link.caption + '](' + link.url + ')')
text = text.replace(link.original, '[' + captionFmt + link.caption + captionFmt + '](' + link.url + ')')
})
}
return text
Expand Down Expand Up @@ -170,6 +173,11 @@ function groupBy (groupByFields, options) {
return handlebars.helpers.each(_groupChildren.call(this, groupByFields, options), options)
}

function groupGlobalsBy (groupByFields, options) {
groupByFields = arrayify(groupByFields)
return handlebars.helpers.each(_groupBy(ddata._globals(options), groupByFields), options)
}

function _addGroup (identifiers, groupByFields) {
return identifiers.map(function (identifier) {
identifier._group = groupByFields.map(function (field) {
Expand Down
38 changes: 38 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"partials"
],
"scripts": {
"test": "test-runner test/*.js test/ddata/*.js"
"test": "test-runner test/*.js test/ddata/*.js test/helpers/*.js"
},
"dependencies": {
"array-back": "^6.2.2",
Expand All @@ -40,6 +40,7 @@
},
"devDependencies": {
"dmd-plugin-example": "^0.1.0",
"mock-require": "^3.0.3",
"test-runner": "^0.10.1"
},
"standard": {
Expand Down
48 changes: 48 additions & 0 deletions test/helpers/group-globals-by.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const Tom = require('test-runner').Tom
console.error('mocking...')
const mock = require('mock-require')
mock('handlebars', {
helpers: {
each: function (arg) { return arg }
}
})

const helpers = mock.reRequire('../../helpers/helpers')
const DmdOptions = require('../../lib/dmd-options')
const assert = require('assert').strict

const tom = module.exports = new Tom('groupGlobalsBy')

function makeOptions (data) {
return {
data: { root: data },
hash: {},
inverse: function() {},
fn: function() {}
}
}

tom.test('sorts globals according to sort fields', function () {
const options = makeOptions([
{ id: '1', category: 'foo', scope: 'global' },
{ id: '2', category: 'foo', scope: 'global' },
{ id: '3', category: 'bar', scope: 'global' },
{ id: '4', category: 'bar', scope: 'global' },
{ id: '5', scope: 'global' },
{ id: 'not global' }
])
const dmdOptions = new DmdOptions(options)
const groupByFields = dmdOptions['group-by']
const result = helpers.groupGlobalsBy(groupByFields, options)
assert.deepEqual(result, [
{ _title: 'foo', level: 0 },
{ id: '1', category: 'foo', scope: 'global', level: 1 },
{ id: '2', category: 'foo', scope: 'global', level: 1 },
{ _title: 'bar', level: 0 },
{ id: '3', category: 'bar', scope: 'global', level: 1 },
{ id: '4', category: 'bar', scope: 'global', level: 1 },
{ id: '5', level: 0, scope: 'global' }
])
})

mock.stopAll()