From 61408248e0fa7ef47e73dcb7b4909204d62e4eed Mon Sep 17 00:00:00 2001 From: black-trooper Date: Sat, 24 Mar 2018 09:45:16 +0900 Subject: [PATCH 1/3] Add custom escape option --- README.md | 18 ++++++++++++++++++ index.js | 3 +++ test/xmp-escape-loader.test.js | 8 ++++++++ 3 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 67335d4..d2aff26 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,24 @@ To load your code with this loader, simply add it to your existing JS loaders in }, ... ], + +// with custom escape + ... + rules: [ + { + test: /\.js$/, + use: [ + 'babel-loader', + { + loader: 'xmp-escape-loader', + options: { + escape: string => escapeHtml(string).toLowerCase() + } + }, + ], + }, + ... + ], ``` ### How it works diff --git a/index.js b/index.js index e75ce96..cf8bea2 100644 --- a/index.js +++ b/index.js @@ -38,6 +38,9 @@ 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) return loader(data) } diff --git a/test/xmp-escape-loader.test.js b/test/xmp-escape-loader.test.js index 0397198..89b88b1 100644 --- a/test/xmp-escape-loader.test.js +++ b/test/xmp-escape-loader.test.js @@ -1,4 +1,5 @@ const xmpEscapeLoader = require('../index') +const escapeHtml = require('escape-html') test('defaults to xmp tag', () => { const input = '<div> Test </div>' @@ -18,3 +19,10 @@ test('works on multiline inputs', () => { const output = xmpEscapeLoader(input) expect(output).toEqual('\n&lt;div&gt; Test &lt;/div&gt;\n') }) + +test('can receive custom escape', () => { + const input = '<div> Test </div>' + const customLoader = xmpEscapeLoader.bind({ query: { escape: string => escapeHtml(string).toLowerCase() } }) + const output = customLoader(input) + expect(output).toEqual('&lt;div&gt; test &lt;/div&gt;') +}) \ No newline at end of file From b35fdb8bb850eb0075747d45351cc297b67f1a56 Mon Sep 17 00:00:00 2001 From: David Pickart Date: Mon, 26 Mar 2018 10:21:26 -0500 Subject: [PATCH 2/3] Refactor --- README.md | 4 ++-- index.js | 17 +++++------------ test/xmp-escape-loader.test.js | 3 ++- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d2aff26..7296f77 100644 --- a/README.md +++ b/README.md @@ -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 `` 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 @@ -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: [ { diff --git a/index.js b/index.js index cf8bea2..d671415 100644 --- a/index.js +++ b/index.js @@ -14,14 +14,8 @@ 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 @@ -29,7 +23,8 @@ function loaderWithTag (tag) { 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 } }) } @@ -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) } diff --git a/test/xmp-escape-loader.test.js b/test/xmp-escape-loader.test.js index 89b88b1..8dbe977 100644 --- a/test/xmp-escape-loader.test.js +++ b/test/xmp-escape-loader.test.js @@ -22,7 +22,8 @@ test('works on multiline inputs', () => { test('can receive custom escape', () => { const input = '<xmp><div> Test </div>' - 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('&lt;div&gt; test &lt;/div&gt;') }) \ No newline at end of file From 66a8334e8e851160118577d381e2d3047cc077f1 Mon Sep 17 00:00:00 2001 From: David Pickart Date: Mon, 26 Mar 2018 10:21:49 -0500 Subject: [PATCH 3/3] v2.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dee2e65..473a799 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xmp-escape-loader", - "version": "2.0.0", + "version": "2.1.0", "description": "", "main": "index.js", "scripts": {