@adobe/helix-html-pipeline 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/package.json +1 -1
- package/src/PipelineResponse.js +1 -1
- package/src/html-pipe.js +7 -1
- package/src/index.js +5 -0
- package/src/steps/extract-metadata.js +8 -50
- package/src/steps/set-custom-response-headers.js +2 -13
- package/src/steps/unwrap-sole-images.js +38 -0
- package/src/utils/metadata.js +66 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
## [1.0.5](https://github.com/adobe/helix-html-pipeline/compare/v1.0.4...v1.0.5) (2022-03-08)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* re-add lost image unwrapper ([#10](https://github.com/adobe/helix-html-pipeline/issues/10)) ([0f2b66e](https://github.com/adobe/helix-html-pipeline/commit/0f2b66eed2157717d0edc321fbd3430d4ce4b42c))
|
|
7
|
+
|
|
8
|
+
## [1.0.4](https://github.com/adobe/helix-html-pipeline/compare/v1.0.3...v1.0.4) (2022-03-08)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* no special headers init for PipelineResponse ([#9](https://github.com/adobe/helix-html-pipeline/issues/9)) ([7288677](https://github.com/adobe/helix-html-pipeline/commit/72886779f6ba0f8d07ea14757a8097034262e215))
|
|
14
|
+
|
|
15
|
+
## [1.0.3](https://github.com/adobe/helix-html-pipeline/compare/v1.0.2...v1.0.3) (2022-03-08)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* fix canonical url and 404.html response, clean up meta ([5f3e999](https://github.com/adobe/helix-html-pipeline/commit/5f3e999a6e305fb6ecf7d2fefe3a38274a135433))
|
|
21
|
+
|
|
1
22
|
## [1.0.2](https://github.com/adobe/helix-html-pipeline/compare/v1.0.1...v1.0.2) (2022-03-08)
|
|
2
23
|
|
|
3
24
|
|
package/package.json
CHANGED
package/src/PipelineResponse.js
CHANGED
|
@@ -19,7 +19,7 @@ export class PipelineResponse {
|
|
|
19
19
|
* Creates the pipeline response
|
|
20
20
|
*/
|
|
21
21
|
constructor(body = undefined, init = {}) {
|
|
22
|
-
let headers = init.headers ?? new Map(
|
|
22
|
+
let headers = init.headers ?? new Map();
|
|
23
23
|
if (typeof headers.get !== 'function') {
|
|
24
24
|
headers = new Map(Object.entries(init.headers));
|
|
25
25
|
}
|
package/src/html-pipe.js
CHANGED
|
@@ -30,6 +30,7 @@ import rewriteIcons from './steps/rewrite-icons.js';
|
|
|
30
30
|
import setXSurrogateKeyHeader from './steps/set-x-surrogate-key-header.js';
|
|
31
31
|
import setCustomResponseHeaders from './steps/set-custom-response-headers.js';
|
|
32
32
|
import splitSections from './steps/split-sections.js';
|
|
33
|
+
import unwrapSoleImages from './steps/unwrap-sole-images.js';
|
|
33
34
|
import tohtml from './steps/stringify-response.js';
|
|
34
35
|
import { PipelineStatusError } from './PipelineStatusError.js';
|
|
35
36
|
import { PipelineResponse } from './PipelineResponse.js';
|
|
@@ -54,7 +55,11 @@ export async function htmlPipe(state, req) {
|
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
/** @type PipelineResponse */
|
|
57
|
-
const res = new PipelineResponse(
|
|
58
|
+
const res = new PipelineResponse('', {
|
|
59
|
+
headers: {
|
|
60
|
+
'content-type': 'text/html; charset=utf-8',
|
|
61
|
+
},
|
|
62
|
+
});
|
|
58
63
|
|
|
59
64
|
try { // fetch config first, since we need to compute the content-bus-id from the fstab ...
|
|
60
65
|
await fetchConfig(state, req, res);
|
|
@@ -80,6 +85,7 @@ export async function htmlPipe(state, req) {
|
|
|
80
85
|
await parseMarkdown(state);
|
|
81
86
|
await splitSections(state);
|
|
82
87
|
await getMetadata(state); // this one extracts the metadata from the mdast
|
|
88
|
+
await unwrapSoleImages(state);
|
|
83
89
|
await html(state);
|
|
84
90
|
await rewriteBlobImages(state);
|
|
85
91
|
await rewriteIcons(state);
|
package/src/index.js
CHANGED
|
@@ -16,3 +16,8 @@ export * from './PipelineRequest.js';
|
|
|
16
16
|
export * from './PipelineResponse.js';
|
|
17
17
|
export * from './PipelineState.js';
|
|
18
18
|
export * from './PipelineStatusError.js';
|
|
19
|
+
|
|
20
|
+
export { default as fetchMetadata } from './steps/fetch-metadata.js';
|
|
21
|
+
export { default as fetchConfig } from './steps/fetch-config.js';
|
|
22
|
+
export { default as setCustomResponseHeaders } from './steps/set-custom-response-headers.js';
|
|
23
|
+
export { getOriginalHost } from './steps/utils.js';
|
|
@@ -11,17 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { resolve } from 'url';
|
|
13
13
|
import { getAbsoluteUrl, makeCanonicalHtmlUrl, optimizeImageURL } from './utils.js';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Converts all non-valid characters to `-`.
|
|
17
|
-
* @param {string} text input text
|
|
18
|
-
* @returns {string} the meta name
|
|
19
|
-
*/
|
|
20
|
-
function toMetaName(text) {
|
|
21
|
-
return text
|
|
22
|
-
.toLowerCase()
|
|
23
|
-
.replace(/[^0-9a-z:_]/gi, '-');
|
|
24
|
-
}
|
|
14
|
+
import { filterGlobalMetadata, toMetaName, ALLOWED_RESPONSE_HEADERS } from '../utils/metadata.js';
|
|
25
15
|
|
|
26
16
|
/**
|
|
27
17
|
* Cleans up comma-separated string lists and returns an array.
|
|
@@ -98,40 +88,6 @@ function readBlockConfig($block) {
|
|
|
98
88
|
return config;
|
|
99
89
|
}
|
|
100
90
|
|
|
101
|
-
function applyMetaRule(target, obj) {
|
|
102
|
-
Object.keys(obj).forEach((key) => {
|
|
103
|
-
const metaKey = toMetaName(key);
|
|
104
|
-
if (metaKey !== 'url' && obj[key]) {
|
|
105
|
-
target[metaKey] = obj[key];
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function globToRegExp(glob) {
|
|
111
|
-
const reString = glob
|
|
112
|
-
.replace(/\*\*/g, '_')
|
|
113
|
-
.replace(/\*/g, '[0-9a-z-.]*')
|
|
114
|
-
.replace(/_/g, '.*');
|
|
115
|
-
return new RegExp(`^${reString}$`);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function filterGlobalMetadata(metaRules, path) {
|
|
119
|
-
const metaConfig = {};
|
|
120
|
-
metaRules.forEach((rule) => {
|
|
121
|
-
const glob = rule.url || rule.URL || rule.Url;
|
|
122
|
-
if (glob && typeof glob === 'string' && /[0-9a-z-/*]/.test(glob)) {
|
|
123
|
-
if (glob.indexOf('*') >= 0) {
|
|
124
|
-
if (globToRegExp(glob).test(path)) {
|
|
125
|
-
applyMetaRule(metaConfig, rule);
|
|
126
|
-
}
|
|
127
|
-
} else if (glob === path) {
|
|
128
|
-
applyMetaRule(metaConfig, rule);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
return metaConfig;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
91
|
/**
|
|
136
92
|
* Looks for metadata in the document.
|
|
137
93
|
* @param {HTMLDocument} document The document
|
|
@@ -196,11 +152,13 @@ export default function extractMetaData(state, req) {
|
|
|
196
152
|
});
|
|
197
153
|
if (Object.keys(metaConfig).length > 0) {
|
|
198
154
|
// add rest to meta.custom
|
|
199
|
-
meta.custom = Object.
|
|
200
|
-
name
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
155
|
+
meta.custom = Object.entries(metaConfig)
|
|
156
|
+
.filter(([name]) => !ALLOWED_RESPONSE_HEADERS.includes(name))
|
|
157
|
+
.map(([name, value]) => ({
|
|
158
|
+
name,
|
|
159
|
+
value,
|
|
160
|
+
property: name.includes(':'),
|
|
161
|
+
}));
|
|
204
162
|
}
|
|
205
163
|
|
|
206
164
|
if (meta.keywords) {
|
|
@@ -10,18 +10,7 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
import { cleanupHeaderValue } from '@adobe/helix-shared-utils';
|
|
13
|
-
import { filterGlobalMetadata } from '
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Array of headers allowed in the metadata.json file.
|
|
17
|
-
*/
|
|
18
|
-
const allowList = [
|
|
19
|
-
'content-security-policy',
|
|
20
|
-
'content-security-policy-report-only',
|
|
21
|
-
'access-control-allow-origin',
|
|
22
|
-
'access-control-allow-methods',
|
|
23
|
-
'link',
|
|
24
|
-
];
|
|
13
|
+
import { filterGlobalMetadata, ALLOWED_RESPONSE_HEADERS } from '../utils/metadata.js';
|
|
25
14
|
|
|
26
15
|
/**
|
|
27
16
|
* Decorates the pipeline response object with the headers defined in metadata.json.
|
|
@@ -34,7 +23,7 @@ const allowList = [
|
|
|
34
23
|
export default function setCustomResponseHeaders(state, req, res) {
|
|
35
24
|
const meta = filterGlobalMetadata(state.metadata, state.info.path);
|
|
36
25
|
Object.entries(meta).forEach(([name, value]) => {
|
|
37
|
-
if (
|
|
26
|
+
if (ALLOWED_RESPONSE_HEADERS.includes(name)) {
|
|
38
27
|
res.headers.set(name, cleanupHeaderValue(value));
|
|
39
28
|
}
|
|
40
29
|
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2019 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
import { map } from 'unist-util-map';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Unwraps hero images to avoid the unnecessary paragraph.
|
|
16
|
+
*
|
|
17
|
+
* @param {object} request The content request
|
|
18
|
+
*/
|
|
19
|
+
export default function unwrap({ content }) {
|
|
20
|
+
let sections = content.mdast.children.filter((node) => node.type === 'section');
|
|
21
|
+
if (!sections.length) {
|
|
22
|
+
sections = [content.mdast];
|
|
23
|
+
}
|
|
24
|
+
sections.forEach((section) => {
|
|
25
|
+
map(section, (node, index, parent) => {
|
|
26
|
+
if (node.type === 'paragraph' // If we have a paragraph
|
|
27
|
+
&& (parent.type === 'root' // … in the document root
|
|
28
|
+
|| parent.type === 'section') // … or in a section
|
|
29
|
+
&& parent.meta.types.includes('has-only-image') // … that only has images
|
|
30
|
+
&& parent.meta.types.includes('nb-image-1')) { // … and actually only 1 of them
|
|
31
|
+
// … then consider it a hero image, and unwrap from the paragraph
|
|
32
|
+
const position = parent.children.indexOf(node);
|
|
33
|
+
const [img] = parent.children[position].children;
|
|
34
|
+
parent.children[position] = img;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Converts all non-valid characters to `-`.
|
|
14
|
+
* @param {string} text input text
|
|
15
|
+
* @returns {string} the meta name
|
|
16
|
+
*/
|
|
17
|
+
export function toMetaName(text) {
|
|
18
|
+
return text
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/[^0-9a-z:_]/gi, '-');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function applyMetaRule(target, obj) {
|
|
24
|
+
Object.keys(obj).forEach((key) => {
|
|
25
|
+
const metaKey = toMetaName(key);
|
|
26
|
+
if (metaKey !== 'url' && obj[key]) {
|
|
27
|
+
target[metaKey] = obj[key];
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function globToRegExp(glob) {
|
|
33
|
+
const reString = glob
|
|
34
|
+
.replace(/\*\*/g, '_')
|
|
35
|
+
.replace(/\*/g, '[0-9a-z-.]*')
|
|
36
|
+
.replace(/_/g, '.*');
|
|
37
|
+
return new RegExp(`^${reString}$`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function filterGlobalMetadata(metaRules, path) {
|
|
41
|
+
const metaConfig = {};
|
|
42
|
+
metaRules.forEach((rule) => {
|
|
43
|
+
const glob = rule.url || rule.URL || rule.Url;
|
|
44
|
+
if (glob && typeof glob === 'string' && /[0-9a-z-/*]/.test(glob)) {
|
|
45
|
+
if (glob.indexOf('*') >= 0) {
|
|
46
|
+
if (globToRegExp(glob).test(path)) {
|
|
47
|
+
applyMetaRule(metaConfig, rule);
|
|
48
|
+
}
|
|
49
|
+
} else if (glob === path) {
|
|
50
|
+
applyMetaRule(metaConfig, rule);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return metaConfig;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Array of headers allowed in the metadata.json file.
|
|
59
|
+
*/
|
|
60
|
+
export const ALLOWED_RESPONSE_HEADERS = [
|
|
61
|
+
'content-security-policy',
|
|
62
|
+
'content-security-policy-report-only',
|
|
63
|
+
'access-control-allow-origin',
|
|
64
|
+
'access-control-allow-methods',
|
|
65
|
+
'link',
|
|
66
|
+
];
|