Skip to content

Commit

Permalink
feat(markdown): refactor controller/markdownToHtml.js module (pelias#…
Browse files Browse the repository at this point in the history
…1599)

* Added Missing HTML / BODY tag

* Updated Text var to use Template Literal plus Simplified controller function

* feat(markdown): refactor markdownToHtml controller

Co-authored-by: Dhananjay-JSR <[email protected]>
Co-authored-by: Dhananjay-JSR <[email protected]>
  • Loading branch information
3 people authored Feb 2, 2022
1 parent a5aabc2 commit 5c650df
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 43 deletions.
42 changes: 22 additions & 20 deletions controller/markdownToHtml.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
var markdown = require('markdown').markdown;
var fs = require('fs');
const markdown = require('markdown').markdown;
const fs = require('fs');

function setup(peliasConfig, markdownFile){
function setup(peliasConfig, markdownFile) {

var styleString = '<style>html{font-family:monospace}</style>';
var text = '# Pelias API\n';
text += '### Version: [' + peliasConfig.version + '](https://github.com/pelias/api/releases)\n';
text += fs.readFileSync( markdownFile, 'utf8');
var html = styleString + markdown.toHTML(text);
// read markdown
const md = fs.readFileSync(markdownFile, 'utf8').trim();

function controller( req, res ) {
if (req.accepts('html')) {
res.send(html);
return;
}
// default behaviour
res.json({
markdown: text,
html: html
});
}
// convert to HTML
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pelias Geocoder</title>
<style>html { font-family:monospace; }</style>
</head>
<body>
${markdown.toHTML(md)}
</body>
</html>`.trim();

return controller;
// send HTML
return function controller(req, res) {
res.send(html);
};
}

module.exports = setup;
3 changes: 3 additions & 0 deletions public/apiDoc.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Pelias API
### Version: [1.0](https://github.com/pelias/api/releases)

## [View our documentation on GitHub](https://github.com/pelias/documentation/)
3 changes: 3 additions & 0 deletions public/attribution.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Pelias API
### Version: [1.0](https://github.com/pelias/api/releases)

## Attribution
* Geocoding by [Pelias](https://pelias.io).
* Data from
Expand Down
27 changes: 4 additions & 23 deletions test/unit/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,11 @@ module.exports.tests.interface = function(test, common) {
});
};

module.exports.tests.info_json = function(test, common) {
test('returns server info in json', function(t) {
var controller = setup({}, './public/attribution.md');
var req = {
accepts: function (format) {
t.equal(format, 'html', 'check for Accepts:html');
return false;
}
};
var res = { json: function( json ){
t.equal(typeof json, 'object', 'returns json');
t.assert(json.hasOwnProperty('markdown'), 'return object contains markdown property');
t.assert(json.hasOwnProperty('html'), 'return object contains html property');
t.end();
}};
controller( req, res );
});
};

module.exports.tests.info_html = function(test, common) {
test('returns server info in html', function(t) {
var filePath = './foo.md';

var style = '<style>html{font-family:monospace}</style>';
var style = '<style>html { font-family:monospace; }</style>';
var mockText = 'this text should show up in the html content';
var fsMock = {
readFileSync: function (path, format) {
Expand All @@ -56,8 +37,8 @@ module.exports.tests.info_html = function(test, common) {
};
var res = { send: function( content ){
t.equal(typeof content, 'string', 'returns string');
t.assert(content.indexOf(style) === 0, 'style set');
t.assert(content.indexOf(mockText) !== -1, 'file content added');
t.assert(content.includes(style), 'style set');
t.assert(content.includes(mockText), 'file content added');
t.end();
}};
controller( req, res );
Expand All @@ -73,4 +54,4 @@ module.exports.all = function (tape, common) {
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
};

0 comments on commit 5c650df

Please sign in to comment.