@adobe/helix-html-pipeline 1.0.4 → 1.1.1

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 CHANGED
@@ -1,3 +1,25 @@
1
+ ## [1.1.1](https://github.com/adobe/helix-html-pipeline/compare/v1.1.0...v1.1.1) (2022-03-10)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * adapt to breaking change (computeSurrogateKey is async) ([9c8830d](https://github.com/adobe/helix-html-pipeline/commit/9c8830dc8db3007921dd85f27537c030d80c0cbf))
7
+ * **deps:** update dependency @adobe/helix-shared-utils to v2.0.4 ([b5cd927](https://github.com/adobe/helix-html-pipeline/commit/b5cd92783f03c47f7f26fbaa6c9438ea11c1e05b))
8
+
9
+ # [1.1.0](https://github.com/adobe/helix-html-pipeline/compare/v1.0.5...v1.1.0) (2022-03-10)
10
+
11
+
12
+ ### Features
13
+
14
+ * avoid node built-ins (path and url) ([9e315df](https://github.com/adobe/helix-html-pipeline/commit/9e315df46fc7f473e437feb2e2d3dfdf9c6ddfc2))
15
+
16
+ ## [1.0.5](https://github.com/adobe/helix-html-pipeline/compare/v1.0.4...v1.0.5) (2022-03-08)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+ * 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))
22
+
1
23
  ## [1.0.4](https://github.com/adobe/helix-html-pipeline/compare/v1.0.3...v1.0.4) (2022-03-08)
2
24
 
3
25
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-html-pipeline",
3
- "version": "1.0.4",
3
+ "version": "1.1.1",
4
4
  "description": "Helix HTML Pipeline",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@adobe/helix-markdown-support": "3.1.2",
36
- "@adobe/helix-shared-utils": "2.0.3",
36
+ "@adobe/helix-shared-utils": "2.0.4",
37
37
  "github-slugger": "1.4.0",
38
38
  "hast-util-to-html": "8.0.3",
39
39
  "jsdom": "19.0.0",
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';
@@ -84,6 +85,7 @@ export async function htmlPipe(state, req) {
84
85
  await parseMarkdown(state);
85
86
  await splitSections(state);
86
87
  await getMetadata(state); // this one extracts the metadata from the mdast
88
+ await unwrapSoleImages(state);
87
89
  await html(state);
88
90
  await rewriteBlobImages(state);
89
91
  await rewriteIcons(state);
@@ -9,8 +9,9 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- import { resolve } from 'url';
13
- import { getAbsoluteUrl, makeCanonicalHtmlUrl, optimizeImageURL } from './utils.js';
12
+ import {
13
+ getAbsoluteUrl, makeCanonicalHtmlUrl, optimizeImageURL, resolveUrl,
14
+ } from './utils.js';
14
15
  import { filterGlobalMetadata, toMetaName, ALLOWED_RESPONSE_HEADERS } from '../utils/metadata.js';
15
16
 
16
17
  /**
@@ -110,7 +111,7 @@ function getLocalMetadata(document) {
110
111
  * @returns The optimized image URL
111
112
  */
112
113
  function optimizeMetaImage(pagePath, imgUrl) {
113
- const src = resolve(pagePath, imgUrl);
114
+ const src = resolveUrl(pagePath, imgUrl);
114
115
  if (src.startsWith('/')) {
115
116
  return optimizeImageURL(src, 1200, 'pjpg');
116
117
  }
@@ -9,8 +9,7 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- import { extname } from 'path';
13
- import { getPathInfo } from '../utils/path.js';
12
+ import { getPathInfo, getExtension } from '../utils/path.js';
14
13
 
15
14
  /**
16
15
  * Finds the mapping from path to folders in fstab
@@ -49,7 +48,7 @@ export default function folderMapping(state) {
49
48
  const mapped = mapPath(folders, path);
50
49
  if (mapped) {
51
50
  state.info = getPathInfo(mapped);
52
- if (extname(mapped)) {
51
+ if (getExtension(mapped)) {
53
52
  // special case: use code-bus
54
53
  state.content.sourceBus = 'code';
55
54
  state.info.resourcePath = mapped;
@@ -18,14 +18,14 @@ import { computeSurrogateKey } from '@adobe/helix-shared-utils';
18
18
  * @param {PipelineResponse} res
19
19
  * @returns {Promise<void>}
20
20
  */
21
- export default function setXSurrogateKeyHeader(state, req, res) {
21
+ export default async function setXSurrogateKeyHeader(state, req, res) {
22
22
  const {
23
23
  content, contentBusId, info, owner, repo, ref,
24
24
  } = state;
25
25
 
26
26
  const keys = [];
27
27
  if (content.sourceLocation) {
28
- keys.push(computeSurrogateKey(content.sourceLocation));
28
+ keys.push(await computeSurrogateKey(content.sourceLocation));
29
29
  }
30
30
  if (info.selector !== 'plain') {
31
31
  keys.push(`${contentBusId}_metadata`);
@@ -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
+ }
@@ -9,7 +9,6 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- import { parse, resolve } from 'url';
13
12
 
14
13
  /**
15
14
  * Returns the original host name from the request to the outer CDN.
@@ -25,21 +24,7 @@ export function getOriginalHost(headers) {
25
24
  }
26
25
 
27
26
  /**
28
- * Turns a relative into an absolute URL.
29
- * @param {object} headers The request headers
30
- * @param {string} url The relative or absolute URL
31
- * @returns {string} The absolute URL or <code>null</code>
32
- * if <code>url</code> is not a string
33
- */
34
- export function getAbsoluteUrl(headers, url) {
35
- if (typeof url !== 'string') {
36
- return null;
37
- }
38
- return resolve(`https://${getOriginalHost(headers)}/`, url);
39
- }
40
-
41
- /**
42
- * Returns the canonical HTML url for the give one by
27
+ * Returns the canonical HTML url for the given one by
43
28
  *
44
29
  * - removing .html extension
45
30
  * - removing index
@@ -95,13 +80,66 @@ export function toClassName(text) {
95
80
  * @returns {string}
96
81
  */
97
82
  export function optimizeImageURL(src, width, format = 'webply', optimize = 'medium') {
98
- // use deprecated api to avoid complexity with non absolute paths
99
- const url = parse(src, true);
83
+ if (typeof src !== 'string') {
84
+ throw new TypeError(`Parameter 'url' must be a string, not ${typeof src}`);
85
+ }
86
+
87
+ const simplePath = (uri) => uri.startsWith('/') || uri.startsWith('./');
88
+
89
+ const uri = src.trim();
90
+
91
+ let url;
92
+ if (simplePath(uri)) {
93
+ url = new URL(`https://dummy${uri[0] !== '/' ? '/' : ''}${uri}`);
94
+ } else {
95
+ url = new URL(uri);
96
+ }
100
97
  delete url.search;
101
98
  if (width) {
102
- url.query.width = String(width);
99
+ url.searchParams.set('width', String(width));
100
+ }
101
+ url.searchParams.set('format', format);
102
+ url.searchParams.set('optimize', optimize);
103
+
104
+ if (simplePath(uri)) {
105
+ // preserve original path over url.pathname
106
+ const pos = uri.search(/[?#]/g);
107
+ if (pos > -1) {
108
+ return `${uri.substring(0, pos)}${url.search}${url.hash}`;
109
+ } else {
110
+ return `${uri}${url.search}${url.hash}`;
111
+ }
112
+ }
113
+ return url.toString();
114
+ }
115
+
116
+ /**
117
+ * Resolves a target URL relative to a base URL in a manner similar
118
+ * to that of a web browser resolving an anchor tag.
119
+ * @param {string} from
120
+ * @param {string} to
121
+ * @returns {string} resolved url
122
+ */
123
+ export function resolveUrl(from, to) {
124
+ const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
125
+ if (resolvedUrl.protocol === 'resolve:') {
126
+ // `from` is a relative URL.
127
+ const { pathname, search, hash } = resolvedUrl;
128
+ return pathname + search + hash;
129
+ }
130
+ return resolvedUrl.toString();
131
+ }
132
+
133
+ /**
134
+ * Turns a relative into an absolute URL.
135
+ * @param {object} headers The request headers
136
+ * @param {string} url The relative or absolute URL
137
+ * @returns {string} The absolute URL or <code>null</code>
138
+ * if <code>url</code> is not a string
139
+ */
140
+ export function getAbsoluteUrl(headers, url) {
141
+ if (typeof url !== 'string') {
142
+ return null;
103
143
  }
104
- url.query.format = format;
105
- url.query.optimize = optimize;
106
- return url.format();
144
+ return resolveUrl(`https://${getOriginalHost(headers)}/`, url);
107
145
  }
package/src/utils/path.js CHANGED
@@ -101,3 +101,14 @@ export function validatePathInfo(info) {
101
101
  }
102
102
  return true;
103
103
  }
104
+
105
+ /**
106
+ * Returns the file extension (e.g. '.html') of the given parh
107
+ * @param {string} path the path to get the extension from
108
+ * @return {string} the file extension or '' if there's no extension
109
+ */
110
+ export function getExtension(path) {
111
+ const basename = path.split('/').pop();
112
+ const pos = basename.lastIndexOf('.');
113
+ return (basename === '' || pos < 1) ? '' : basename.slice(pos);
114
+ }