Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.

Commit 5d59e79

Browse files
authored
Merge pull request #16 from ericmorand/issue_15
Resolve issue #15
2 parents 9d9f72a + 7358cd1 commit 5d59e79

33 files changed

+2819
-8064
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ indent_style = space
1414
indent_size = 4
1515

1616
# Indentation override for all JS under lib directory
17-
[*.js]
17+
[*.ts]
1818
indent_size = 2
1919

2020
# Matches the exact files either package.json or .travis.yml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ npm-debug.log*
33
node_modules/
44
coverage
55
.nyc_output
6+
package-lock.json

.nycrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"branches": 100,
55
"functions": 100,
66
"extension": [
7-
".js"
7+
".ts"
88
],
99
"include": "src",
1010
"reporter": [

.travis.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
language: node_js
22
node_js:
3-
- 6
4-
- 7
5-
- 8
6-
- 9
73
- 10
4+
- 11
5+
- 12
86
jobs:
97
include:
108
- stage: cover
11-
node_js: 10
9+
node_js: 13
1210
script:
1311
- npm run cover
1412
- npm run coverage

README.md

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
Rebase your HTML assets relatively to the source file they were imported from.
66

7+
## Installation
8+
9+
```bash
10+
npm install html-source-map-rebase
11+
```
12+
713
## Example
814

915
Consider the following Twig sources:
@@ -34,93 +40,36 @@ By rebasing the assets relatively to the file they were imported from, the resul
3440
html-source-map-rebase uses the mapping provided by source maps to resolve the original file the assets where imported from. That's why it *needs* a source map to perform its magic. Any tool able to generate a source map from a source file is appropriate. Here is how one could use [Twing](https://www.npmjs.com/package/twing) and html-source-map-rebase together to render an HTML document and rebase its assets.
3541

3642
``` javascript
37-
const {TwingEnvironment, TwingLoaderFilesystem} = require('twing');
38-
const Readable = require('stream').Readable;
39-
const through = require('through2');
40-
const Rebaser = require('html-source-map-rebase');
43+
import {TwingEnvironment, TwingLoaderFilesystem} from "twing";
44+
import {createRebaser} from "html-source-map-rebase";
4145

42-
let loader = new TwingLoaderFilesystem('src');
43-
let twing = new TwingEnvironment(loader, {
46+
const loader = new TwingLoaderFilesystem('src');
47+
const environment = new TwingEnvironment(loader, {
4448
source_map: true
4549
});
4650

47-
let html = twing.render('index.twig');
48-
49-
let rebaser = new Rebaser({
50-
map: twing.getSourceMap()
51-
});
52-
53-
let data = '';
54-
let stream = new Readable({
55-
encoding: 'utf8'
56-
});
57-
58-
stream
59-
.pipe(rebaser)
60-
.pipe(through(function (chunk, enc, cb) {
61-
data += chunk;
62-
63-
cb();
64-
}))
65-
.on('finish', function () {
66-
console.log(data); // data contains the rendered HTML with rebased assets
67-
})
68-
;
69-
70-
stream.push(html);
71-
stream.push(null);
51+
return environment.render('index.twig')
52+
.then((html) => {
53+
const map = environment.getSourceMap();
54+
const rebaser = createRebaser(Buffer.from(map));
55+
56+
return rebaser.rebase(html);
57+
})
58+
.then((result) => {
59+
// result.data contains the HTML markup with rebased assets
60+
// result.map contains the source map
61+
});
7262
```
7363

7464
## API
7565

76-
`let Rebaser = require('html-source-map-rebase')`
77-
78-
### rebaser = new Rebaser(opts={})
79-
80-
Return an object transform stream `rebaser` that expects entry filenames.
81-
82-
Optionally pass in some opts:
83-
84-
* opts.map:
85-
86-
The belonging source map in the form of a JSON string. Defaults to `null`. Note that this module basically does nothing without a source map.
87-
88-
* opts.rebase:
89-
90-
Handles when the rebaser encounters an asset that may need rebasing.
91-
92-
* Type: `Function`
93-
* Default: `undefined`
94-
* Signature:
95-
* `url (Url)` - the [Url](https://nodejs.org/api/url.html) of the asset that may need rebasing.
96-
* `source (String)` - the path of the file the asset was imported from.
97-
* `done (Function)` - a callback function to invoke on completion. Accepts either `false`, `null`, `undefined` or an [Url](https://nodejs.org/api/url.html) as parameter. When called with `false`, the asset will not be rebased. When called with either `null` or `undefined`, the asset will be rebased using the [default rebasing logic](#rebasing-logic). When called with an Url, the asset will be rebased to that Url.
98-
99-
## <a name="rebasing-logic"></a>Rebasing logic
100-
101-
When html-source-map-rebase encounters an asset that may need rebasing, it first checks if it is a remote or a local asset. In the former case, the asset is not rebased at all. In the latter case, the asset is rebased be appending the asset path to the path of the source it's coming from.
102-
103-
For example, a `foo/bar.png` asset coming from `/lorem/ipsum/index.twig` would be rebased to `/lorem/ipsum/foo/bar.png`.
104-
105-
## Events
106-
107-
In addition to the usual events emitted by node.js streams, html-source-map-rebase emits the following events:
108-
109-
### rebaser.on('rebase', function(url) {})
110-
111-
Every time an asset is rebased, this event fires with the rebased [Url](https://nodejs.org/api/url.html).
112-
113-
## Installation
114-
115-
```bash
116-
npm install html-source-map-rebase
117-
```
66+
Read the [documentation](https://nightlycommit.github.io/html-source-map-rebase) for more information.
11867

11968
## Contributing
12069

12170
* Fork the main repository
12271
* Code
123-
* Implement tests using [node-tap](https://github.com/tapjs/node-tap)
72+
* Implement tests using [tape](https://www.npmjs.com/package/tape)
12473
* Issue a pull request keeping in mind that all pull requests must reference an issue in the issue queue
12574

12675
## License

docs/.nojekyll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.

docs/assets/highlight.css

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
:root {
2+
--light-hl-0: #800000;
3+
--dark-hl-0: #808080;
4+
--light-hl-1: #800000;
5+
--dark-hl-1: #569CD6;
6+
--light-hl-2: #000000;
7+
--dark-hl-2: #D4D4D4;
8+
--light-hl-3: #E50000;
9+
--dark-hl-3: #9CDCFE;
10+
--light-hl-4: #0000FF;
11+
--dark-hl-4: #CE9178;
12+
--light-hl-5: #0000FF;
13+
--dark-hl-5: #569CD6;
14+
--light-hl-6: #0070C1;
15+
--dark-hl-6: #4FC1FF;
16+
--light-hl-7: #795E26;
17+
--dark-hl-7: #DCDCAA;
18+
--light-hl-8: #A31515;
19+
--dark-hl-8: #CE9178;
20+
--light-hl-9: #001080;
21+
--dark-hl-9: #9CDCFE;
22+
--light-hl-10: #008000;
23+
--dark-hl-10: #6A9955;
24+
--light-code-background: #FFFFFF;
25+
--dark-code-background: #1E1E1E;
26+
}
27+
28+
@media (prefers-color-scheme: light) { :root {
29+
--hl-0: var(--light-hl-0);
30+
--hl-1: var(--light-hl-1);
31+
--hl-2: var(--light-hl-2);
32+
--hl-3: var(--light-hl-3);
33+
--hl-4: var(--light-hl-4);
34+
--hl-5: var(--light-hl-5);
35+
--hl-6: var(--light-hl-6);
36+
--hl-7: var(--light-hl-7);
37+
--hl-8: var(--light-hl-8);
38+
--hl-9: var(--light-hl-9);
39+
--hl-10: var(--light-hl-10);
40+
--code-background: var(--light-code-background);
41+
} }
42+
43+
@media (prefers-color-scheme: dark) { :root {
44+
--hl-0: var(--dark-hl-0);
45+
--hl-1: var(--dark-hl-1);
46+
--hl-2: var(--dark-hl-2);
47+
--hl-3: var(--dark-hl-3);
48+
--hl-4: var(--dark-hl-4);
49+
--hl-5: var(--dark-hl-5);
50+
--hl-6: var(--dark-hl-6);
51+
--hl-7: var(--dark-hl-7);
52+
--hl-8: var(--dark-hl-8);
53+
--hl-9: var(--dark-hl-9);
54+
--hl-10: var(--dark-hl-10);
55+
--code-background: var(--dark-code-background);
56+
} }
57+
58+
:root[data-theme='light'] {
59+
--hl-0: var(--light-hl-0);
60+
--hl-1: var(--light-hl-1);
61+
--hl-2: var(--light-hl-2);
62+
--hl-3: var(--light-hl-3);
63+
--hl-4: var(--light-hl-4);
64+
--hl-5: var(--light-hl-5);
65+
--hl-6: var(--light-hl-6);
66+
--hl-7: var(--light-hl-7);
67+
--hl-8: var(--light-hl-8);
68+
--hl-9: var(--light-hl-9);
69+
--hl-10: var(--light-hl-10);
70+
--code-background: var(--light-code-background);
71+
}
72+
73+
:root[data-theme='dark'] {
74+
--hl-0: var(--dark-hl-0);
75+
--hl-1: var(--dark-hl-1);
76+
--hl-2: var(--dark-hl-2);
77+
--hl-3: var(--dark-hl-3);
78+
--hl-4: var(--dark-hl-4);
79+
--hl-5: var(--dark-hl-5);
80+
--hl-6: var(--dark-hl-6);
81+
--hl-7: var(--dark-hl-7);
82+
--hl-8: var(--dark-hl-8);
83+
--hl-9: var(--dark-hl-9);
84+
--hl-10: var(--dark-hl-10);
85+
--code-background: var(--dark-code-background);
86+
}
87+
88+
.hl-0 { color: var(--hl-0); }
89+
.hl-1 { color: var(--hl-1); }
90+
.hl-2 { color: var(--hl-2); }
91+
.hl-3 { color: var(--hl-3); }
92+
.hl-4 { color: var(--hl-4); }
93+
.hl-5 { color: var(--hl-5); }
94+
.hl-6 { color: var(--hl-6); }
95+
.hl-7 { color: var(--hl-7); }
96+
.hl-8 { color: var(--hl-8); }
97+
.hl-9 { color: var(--hl-9); }
98+
.hl-10 { color: var(--hl-10); }
99+
pre, code { background: var(--code-background); }

docs/assets/main.js

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/navigation.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/search.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)