Skip to content

Commit

Permalink
fix: replace module-path in examples with different quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Jun 7, 2020
1 parent 2537d96 commit a88eb0a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
39 changes: 27 additions & 12 deletions handlebars/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,40 @@ function includeRaw(filename) {
*/
function example(filename, options) {
return fs.readFile(filename, 'utf-8').then(function(contents) {
// Relative path to the current module (e.g. "../"). This path must be replaced
// by the module name in the
const modulePath = path.relative(path.dirname(filename), '.')
debug('example modulepath', modulePath)
const requireModuleRegex = new RegExp(regex`require\('${modulePath}/?(.*?)'\)`, 'g')
if (options && options.hash && options.hash.snippet) {
contents = contents.match(/---<snip>---.*\n([\S\s]*?)\n.*---<\/snip>---/)[1]
}

return util.format(
'```%s\n%s\n```',
path.extname(filename).substr(1),
contents.trim().replace(requireModuleRegex, function(match, suffix) {
return `require('${require(process.cwd() + '/package').name}${suffix ? '/' + suffix : ''}')`
})
)
contents = contents.trim()

// Relative path to the current module (e.g. "../"). This path must be replaced
// by the module name from package.json
const modulePath = path.relative(path.dirname(filename), '.')
debug('example modulepath', modulePath)
const packageName = require(process.cwd() + '/package').name
contents = replaceRequireModule(contents, modulePath, packageName, '"')
contents = replaceRequireModule(contents, modulePath, packageName, '`')
contents = replaceRequireModule(contents, modulePath, packageName, "'")

return util.format('```%s\n%s\n```', path.extname(filename).substr(1), contents)
})
}

function replaceRequireModule(contents, modulePath, packageName, quoteChar) {
const requireModuleSingleQuoteRegex = new RegExp(regex`require\(${quoteChar}${modulePath}(/.*?)?${quoteChar}\)`, 'g')

return contents.replace(requireModuleSingleQuoteRegex, function(match, suffix) {
return `require(${quoteChar}${combineRequirePath(packageName, suffix)}${quoteChar})`
})
}

function combineRequirePath(packageName, innerPath) {
if (innerPath == null || innerPath === '' || innerPath === '/') {
return packageName
}
return packageName + innerPath
}

/**
* Return true if a file exists
*
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/example-helper/examples/full.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
require('../')
require('..')
require('../file')

require("../")
require("..")
require("../file")

require(`../`)
require(`..`)
require(`../file`)
8 changes: 8 additions & 0 deletions test/fixtures/example-helper/examples/output.full.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@
require('example-helper')
require('example-helper')
require('example-helper/file')

require("example-helper")
require("example-helper")
require("example-helper/file")

require(`example-helper`)
require(`example-helper`)
require(`example-helper/file`)
```

0 comments on commit a88eb0a

Please sign in to comment.