The code consists of two functions: resultMarkdown
which formats a single result in markdown, and displayCell
which formats an array of results in markdown, and exports displayCell
as a module.
npm run import -- "display interpreted results in markdown"
function resultMarkdown(res) {
return ('\n\n\n' + res.length + ' match'
+ (res.length !== 1 ? 'es' : '')
+ ' found: ' + res.join(' , ') + '\n\n\n'
+ (res.length > 0
? ('\n\n\n' + res.markdown.join('\n') + '\n\n\n'
+ '```\n\n\n' + res.code + '\n\n\n```\n\n\n')
: ''));
};
function displayCell(results) {
return (typeof results[0] !== 'undefined' && typeof results[0] !== 'string'
? results.reduce((str, res) => {
str += resultMarkdown(res);
return str;
}, '')
: resultMarkdown(results));
}
module.exports.displayCell = displayCell
```javascript
/**
* Formats the result of a search in markdown format.
*
* @param {Array} res The result of the search.
* @returns {string} The markdown formatted result.
*/
function resultMarkdown(res) {
const matchCount = res.length;
const matchesTitle = matchCount > 1?'matches' :'match';
// Join the results with a comma and a space, unless it's the last one,
// then just append it without a space.
const resultList = res.map((item, index) => index < res.length - 1
? item + ','
: item).join('');
const content = matchCount > 0
? (`\n\n\n ${matchCount} ${matchesTitle} found: ${resultList} \n\n\n
\n\n\n ${res.map(item => item.markdown).join('\n')} \n\n\n
``\n\n\n ${res.map(item => item.code).join('\n\n\n')} \n\n\n``\n\n\n`)
: '';
return content;
};
/**
* Formats the result of a search in markdown format and
* returns it as a string. If the first element of the result is
* a string, return the string as is.
*
* @param {Array} results The result of the search.
* @returns {string} The markdown formatted result.
*/
function displayCell(results) {
// Check if the first element of the result is a string.
if (typeof results[0] ==='string') {
return results[0];
}
// Check if the result is an array and if its first element is not undefined.
if (Array.isArray(results) && results[0]!== undefined) {
// Reduce the array to a string by calling resultMarkdown for each element.
return results.reduce((str, res) => str + resultMarkdown(res), '');
} else {
// If the result is not an array or its first element is undefined,
// return an empty string.
return '';
}
}
module.exports.displayCell = displayCell;
```
resultMarkdown
Functionres
as input and returns a formatted string in markdown.res
contains a markdown
property and a code
property, they are appended to the string with a formatted code block.displayCell
Functionresults
as input and returns a formatted string in markdown.resultMarkdown
on each element and concatenating the results.resultMarkdown
directly on the first element of the array.displayCell
function is exported as a module export, making it available for use in other parts of the application.