Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dpikt committed Mar 26, 2018
1 parent 6140824 commit b35fdb8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function MyXMPComponent () {

### Usage

To load your code with this loader, simply add it to your existing JS loaders in your webpack config. It's also possible to use a tag ther than `<xmp>` by passing in a loader option.
To load your code with this loader, simply add it to your existing JS loaders in your webpack config. It's also possible to use a tag other than `<xmp>` by passing in a loader option.

```jsx
// webpack.config.js
Expand Down Expand Up @@ -57,7 +57,7 @@ To load your code with this loader, simply add it to your existing JS loaders in
...
],

// with custom escape
// with custom escape function
...
rules: [
{
Expand Down
17 changes: 5 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,17 @@ function getDelimiters (tag) {
}
}

// Escape inner text between delimiters
function escapeTextInDelimiters (str, start, end) {
var middleText = str.slice(start.length, end.length * -1)
return start + escapeHtml(middleText) + end
}

// Create block loader with given tag delimiter
function loaderWithTag (tag) {
function loaderWithTag (tag, escape) {
var delimiters = getDelimiters(tag)
var start = delimiters.start
var end = delimiters.end
return blockLoader({
start: start,
end: end,
process: function (str) {
return escapeTextInDelimiters(str, start, end)
var middleText = str.slice(start.length, end.length * -1)
return start + escape(middleText) + end
}
})
}
Expand All @@ -38,10 +33,8 @@ function loaderWithTag (tag) {
function load (data) {
var options = getOptions(this) || {}
var tag = options.tag || 'xmp'
if (options.escape) {
escapeHtml = options.escape
}
var loader = loaderWithTag(tag)
var escape = options.escape || escapeHtml
var loader = loaderWithTag(tag, escape)
return loader(data)
}

Expand Down
3 changes: 2 additions & 1 deletion test/xmp-escape-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ test('works on multiline inputs', () => {

test('can receive custom escape', () => {
const input = '<xmp><div> Test </div></xmp>'
const customLoader = xmpEscapeLoader.bind({ query: { escape: string => escapeHtml(string).toLowerCase() } })
const escape = (string) => escapeHtml(string).toLowerCase()
const customLoader = xmpEscapeLoader.bind({ query: { escape } })
const output = customLoader(input)
expect(output).toEqual('<xmp>&lt;div&gt; test &lt;/div&gt;</xmp>')
})

0 comments on commit b35fdb8

Please sign in to comment.