Skip to content

Commit

Permalink
fix: fix: wrap ecstatic for fixing jfhbrook/node-ecstatic#235
Browse files Browse the repository at this point in the history
  • Loading branch information
imcuttle committed Oct 27, 2018
1 parent a9adefd commit dc534f3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
22 changes: 21 additions & 1 deletion __tests__/express.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,34 @@ describe('main', function() {

it('should skip readme when index.html existed', function(done) {
request(app)
.get('/case-readme')
.get('/case-readme/')
.expect(function(res) {
expect(res.text).toBe('index\n')
})
.expect(200)
.end(done)
})

it('should redirect to directory', function(done) {
request(app)
.get('/case-readme')
.expect(302)
.end(done)
})

it("should ecstatic's link is correct when has baseUrl", function(done) {
request(app)
.get('/case-1/')
.expect(200)
.expect(function(res) {
expect(res.text).not.toContain('<a href="/abc/">')
expect(res.text).toContain('<a href="/case-1/abc/">')
expect(res.text).not.toContain('<a href="/haha.html">')
expect(res.text).toContain('<a href="/case-1/haha.html">')
})
.end(done)
})

it('should responses raw markdown text when `?raw`', function(done) {
request(app)
.get('/case-raw/README.md?raw=true')
Expand Down
59 changes: 34 additions & 25 deletions src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@ function renderMarkdown(
res,
filename,
next,
{
markdownTemplate = nps.join(__dirname, 'template.html'),
markdownTemplateString,
templateParameters = {}
} = {}
{ markdownTemplate = nps.join(__dirname, 'template.html'), markdownTemplateString, templateParameters = {} } = {}
) {
markhtml(filename)
.then(function({ output }) {
if (
typeof markdownTemplateString === 'string' &&
markdownTemplateString
) {
if (typeof markdownTemplateString === 'string' && markdownTemplateString) {
return { output, templateString: markdownTemplateString }
}
return pify(fs.readFile)(markdownTemplate, {
Expand All @@ -45,11 +38,13 @@ function renderMarkdown(
.then(({ templateString, output }) => {
res.type('html')
res.send(
template(templateString)(Object.assign({}, templateParameters, {
filename,
title: nps.basename(filename, nps.extname(filename)),
markdownHTML: output
}))
template(templateString)(
Object.assign({}, templateParameters, {
filename,
title: nps.basename(filename, nps.extname(filename)),
markdownHTML: output
})
)
)
})
.catch(next)
Expand All @@ -72,10 +67,7 @@ function markdown(options = {}) {
const filename = nps.join(root, url)
debug('filename', filename)

if (
fs.isFile(filename) &&
['.md', '.markdown'].includes(nps.extname(filename).toLowerCase())
) {
if (fs.isFile(filename) && ['.md', '.markdown'].includes(nps.extname(filename).toLowerCase())) {
renderMarkdown(res, filename, next, options)
} else if (
fs.isDirectory(filename) &&
Expand Down Expand Up @@ -122,18 +114,35 @@ const DEFAULT_OPTIONS = {
}
function githubSimilar(options = {}) {
if (typeof options.root !== 'string') {
throw new SpecError(
'expect `root` is belongs to string, but ' + typeof options.root + '.'
)
throw new SpecError('expect `root` is belongs to string, but ' + typeof options.root + '.')
}

options = Object.assign({}, DEFAULT_OPTIONS, options)
debug('options:', options)

return [
options.enableMarkdown && markdown(options),
options.enableStatic && ecstatic(options)
].filter(Boolean)
return [options.enableMarkdown && markdown(options), options.enableStatic && wrapStatic(options)].filter(Boolean)
}

// https://github.com/jfhbrook/node-ecstatic/issues/235
// Wrap ecstatic to solve it temporarily
function wrapStatic(opts = {}) {
return function(req, res, next) {
const baseDir = typeof opts.baseDir === 'string' ? opts.baseDir : req.baseUrl
req.url = req.originalUrl || req.url

let fn = ecstatic(
Object.assign({}, opts, {
baseDir
})
)

if (!Array.isArray(fn)) {
fn = [fn]
}
fn.forEach(handle => {
typeof handle === 'function' && handle.apply(this, arguments)
})
}
}

githubSimilar.markdown = markdown
Expand Down

0 comments on commit dc534f3

Please sign in to comment.