@adobe/helix-html-pipeline 1.0.0

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.
Files changed (60) hide show
  1. package/.eslintrc.cjs +33 -0
  2. package/.husky/pre-commit +4 -0
  3. package/.mocha-multi.json +6 -0
  4. package/.nycrc.json +10 -0
  5. package/.releaserc.cjs +16 -0
  6. package/CHANGELOG.md +6 -0
  7. package/CODE_OF_CONDUCT.md +74 -0
  8. package/CONTRIBUTING.md +74 -0
  9. package/LICENSE.txt +264 -0
  10. package/README.md +45 -0
  11. package/docs/API.md +12 -0
  12. package/package.json +101 -0
  13. package/src/PipelineContent.d.ts +69 -0
  14. package/src/PipelineContent.js +26 -0
  15. package/src/PipelineRequest.d.ts +26 -0
  16. package/src/PipelineRequest.js +36 -0
  17. package/src/PipelineResponse.d.ts +32 -0
  18. package/src/PipelineResponse.js +44 -0
  19. package/src/PipelineState.d.ts +72 -0
  20. package/src/PipelineState.js +42 -0
  21. package/src/PipelineStatusError.d.ts +14 -0
  22. package/src/PipelineStatusError.js +17 -0
  23. package/src/html-pipe.js +100 -0
  24. package/src/index.d.ts +98 -0
  25. package/src/index.js +18 -0
  26. package/src/json-pipe.js +87 -0
  27. package/src/steps/add-heading-ids.js +32 -0
  28. package/src/steps/create-page-blocks.js +78 -0
  29. package/src/steps/create-pictures.js +35 -0
  30. package/src/steps/extract-metadata.js +257 -0
  31. package/src/steps/fetch-config.js +42 -0
  32. package/src/steps/fetch-content.js +83 -0
  33. package/src/steps/fetch-metadata.js +53 -0
  34. package/src/steps/fix-sections.js +36 -0
  35. package/src/steps/folder-mapping.js +61 -0
  36. package/src/steps/get-metadata.js +170 -0
  37. package/src/steps/make-html.js +34 -0
  38. package/src/steps/parse-markdown.js +42 -0
  39. package/src/steps/removeHlxProps.js +34 -0
  40. package/src/steps/render-code.js +25 -0
  41. package/src/steps/render.js +158 -0
  42. package/src/steps/rewrite-blob-images.js +44 -0
  43. package/src/steps/rewrite-icons.js +93 -0
  44. package/src/steps/set-custom-response-headers.js +41 -0
  45. package/src/steps/set-x-surrogate-key-header.js +35 -0
  46. package/src/steps/split-sections.js +57 -0
  47. package/src/steps/stringify-response.js +39 -0
  48. package/src/steps/utils.js +107 -0
  49. package/src/utils/hast-util-to-dom.js +190 -0
  50. package/src/utils/heading-handler.js +42 -0
  51. package/src/utils/icon-handler.js +40 -0
  52. package/src/utils/json-filter.js +143 -0
  53. package/src/utils/last-modified.js +48 -0
  54. package/src/utils/link-handler.js +25 -0
  55. package/src/utils/mdast-to-vdom.js +323 -0
  56. package/src/utils/mdast-util-gfm-nolink.js +93 -0
  57. package/src/utils/path.js +103 -0
  58. package/src/utils/remark-gfm-nolink.js +128 -0
  59. package/src/utils/section-handler.js +69 -0
  60. package/src/utils/table-handler.js +27 -0
@@ -0,0 +1,103 @@
1
+ /*
2
+ * Copyright 2021 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
+ /**
14
+ * Returns a path info for the given resource path
15
+ * @param {string} path request path
16
+ * @returns {PathInfo} the path info
17
+ */
18
+ export function getPathInfo(path) {
19
+ if (!path) {
20
+ // eslint-disable-next-line no-param-reassign
21
+ path = '/';
22
+ }
23
+ const segs = path.split(/\/+/);
24
+ segs.shift(); // remove _emptyness_ before first slash
25
+ if (segs.length < 1) {
26
+ return null;
27
+ }
28
+ if (segs.indexOf('..') >= 0 || segs.indexOf('.') >= 0) {
29
+ return null;
30
+ }
31
+ const info = {
32
+ selector: '',
33
+ extension: '.html',
34
+ originalExtension: '',
35
+ originalPath: path,
36
+ originalFilename: segs.pop(),
37
+ };
38
+
39
+ // path -> web path (no .html, no index)
40
+ // resourcePath -> content path (.md)
41
+ let fileName = info.originalFilename;
42
+ if (!fileName || fileName === 'index.html' || fileName === 'index.md' || fileName === 'index') {
43
+ // last segment empty or index
44
+ const lastDot = fileName.lastIndexOf('.');
45
+ if (lastDot >= 0) {
46
+ info.originalExtension = fileName.substring(lastDot);
47
+ }
48
+ segs.push('');
49
+ fileName = 'index.md';
50
+ } else if (fileName.indexOf('.') < 0) {
51
+ // if last segment has no extension, add `.md`
52
+ segs.push(fileName);
53
+ fileName = `${fileName}.md`;
54
+ } else {
55
+ // compute selector and extension
56
+ const firstDot = fileName.indexOf('.');
57
+ const lastDot = fileName.lastIndexOf('.');
58
+ info.extension = fileName.substring(lastDot);
59
+ info.originalExtension = info.extension;
60
+ const baseName = fileName.substring(0, firstDot);
61
+ let resExt = info.extension;
62
+ if (resExt === '.md') {
63
+ info.extension = '.html';
64
+ segs.push(baseName);
65
+ } else if (resExt === '.html') {
66
+ resExt = '.md';
67
+ segs.push(baseName);
68
+ } else {
69
+ segs.push(`${baseName}${resExt}`);
70
+ }
71
+
72
+ if (lastDot !== firstDot) {
73
+ info.selector = fileName.substring(firstDot + 1, lastDot);
74
+ segs[segs.length - 1] = `${baseName}.${info.selector}${info.extension}`;
75
+ }
76
+ fileName = `${baseName}${resExt}`;
77
+ }
78
+
79
+ info.path = `/${segs.join('/')}`;
80
+ segs[segs.length - 1] = fileName;
81
+ info.resourcePath = `/${segs.join('/')}`;
82
+ return info;
83
+ }
84
+
85
+ /**
86
+ * Validates the path info
87
+ * @param {PathInfo} info Info to valida
88
+ * @return {boolean} {@code true} if valid.
89
+ */
90
+ export function validatePathInfo(info) {
91
+ if (!info) {
92
+ return false;
93
+ }
94
+
95
+ // only support empty selector or plain with html
96
+ if (info.selector) {
97
+ if (info.selector !== 'plain') {
98
+ return false;
99
+ }
100
+ return info.extension === '.html';
101
+ }
102
+ return true;
103
+ }
@@ -0,0 +1,128 @@
1
+ /*
2
+ * Copyright 2021 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
+ /*
14
+ copied from
15
+ https://github.com/syntax-tree/mdast-util-gfm/blob/544cc04ef9980fe152090bf68338444b1ed0e7a4/index.js
16
+
17
+ (The MIT License)
18
+
19
+ Copyright (c) 2020 Titus Wormer <tituswormer@gmail.com>
20
+
21
+ Permission is hereby granted, free of charge, to any person obtaining
22
+ a copy of this software and associated documentation files (the
23
+ 'Software'), to deal in the Software without restriction, including
24
+ without limitation the rights to use, copy, modify, merge, publish,
25
+ distribute, sublicense, and/or sell copies of the Software, and to
26
+ permit persons to whom the Software is furnished to do so, subject to
27
+ the following conditions:
28
+
29
+ The above copyright notice and this permission notice shall be
30
+ included in all copies or substantial portions of the Software.
31
+
32
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
33
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
35
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
36
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
37
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
38
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39
+ */
40
+
41
+ /* eslint-disable no-unused-vars */
42
+
43
+ /**
44
+ * @typedef {import('micromark-util-types').Extension} Extension
45
+ * @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
46
+ * @typedef {import('micromark-extension-gfm-strikethrough').Options} Options
47
+ * @typedef {import('micromark-extension-gfm-footnote').HtmlOptions} HtmlOptions
48
+ */
49
+
50
+ import {
51
+ combineExtensions,
52
+ } from 'micromark-util-combine-extensions';
53
+ // import {
54
+ // gfmAutolinkLiteral,
55
+ // gfmAutolinkLiteralHtml
56
+ // } from 'micromark-extension-gfm-autolink-literal'
57
+ import { gfmFootnote, gfmFootnoteHtml } from 'micromark-extension-gfm-footnote';
58
+ import {
59
+ gfmStrikethrough,
60
+ gfmStrikethroughHtml,
61
+ } from 'micromark-extension-gfm-strikethrough';
62
+ import { gfmTable, gfmTableHtml } from 'micromark-extension-gfm-table';
63
+ import { gfmTagfilterHtml } from 'micromark-extension-gfm-tagfilter';
64
+ import {
65
+ gfmTaskListItem,
66
+ gfmTaskListItemHtml,
67
+ } from 'micromark-extension-gfm-task-list-item';
68
+ import { gfmFromMarkdown, gfmToMarkdown } from './mdast-util-gfm-nolink.js';
69
+
70
+ /**
71
+ * Support GFM or markdown on github.com.
72
+ *
73
+ * @param {Options} [options]
74
+ * @returns {Extension}
75
+ */
76
+ export function gfm(options) {
77
+ return combineExtensions([
78
+ // gfmAutolinkLiteral,
79
+ gfmFootnote(),
80
+ gfmStrikethrough(options),
81
+ gfmTable,
82
+ gfmTaskListItem,
83
+ ]);
84
+ }
85
+
86
+ /**
87
+ * Support to compile GFM to HTML.
88
+ *
89
+ * @param {HtmlOptions} [options]
90
+ * @returns {HtmlExtension}
91
+ */
92
+ // export function gfmHtml(options) {
93
+ // return combineHtmlExtensions([
94
+ // // gfmAutolinkLiteralHtml,
95
+ // gfmFootnoteHtml(options),
96
+ // gfmStrikethroughHtml,
97
+ // gfmTableHtml,
98
+ // gfmTagfilterHtml,
99
+ // gfmTaskListItemHtml,
100
+ // ]);
101
+ // }
102
+
103
+ /**
104
+ * Plugin to support GFM (autolink literals, footnotes, strikethrough, tables, tasklists).
105
+ *
106
+ * @type {import('unified').Plugin<[Options?]|void[], Root>}
107
+ */
108
+ export default function remarkGfm(options = {}) {
109
+ const data = this.data();
110
+
111
+ /**
112
+ * @param {string} field
113
+ * @param {unknown} value
114
+ */
115
+ function add(field, value) {
116
+ const list = /** @type {unknown[]} */ (
117
+ // Other extensions
118
+ /* c8 ignore next 2 */
119
+ data[field] ? data[field] : (data[field] = [])
120
+ );
121
+
122
+ list.push(value);
123
+ }
124
+
125
+ add('micromarkExtensions', gfm(options));
126
+ add('fromMarkdownExtensions', gfmFromMarkdown());
127
+ add('toMarkdownExtensions', gfmToMarkdown(options));
128
+ }
@@ -0,0 +1,69 @@
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 { all } from 'mdast-util-to-hast';
13
+ import { wrap } from 'mdast-util-to-hast/lib/wrap.js';
14
+
15
+ const HELIX_NAMESPACE = 'hlx-';
16
+ const DEFAULT_SECTION_TAG = 'div';
17
+ const DEFAULT_SECTION_CLASS = `${HELIX_NAMESPACE}section`;
18
+ const SYSTEM_META_PROPERTIES = ['tagname'];
19
+ const SYSTEM_HTML_ATTRIBUTES = ['types'];
20
+ const GLOBAL_HTML_ATTRIBUTES = [
21
+ 'accesskey', 'autocapitalize', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', 'hidden',
22
+ 'id', 'inputmode', 'is', 'itemid', 'itemprop', 'itemref', 'itemscope', 'itemtype', 'lang', 'slot', 'spellcheck',
23
+ 'style', 'tabindex', 'title', 'translate',
24
+ ];
25
+
26
+ /**
27
+ * Get the tag name for the specified section.
28
+ *
29
+ * @param {Node} section The MDAST section to get the tag name for
30
+ * @returns {string} The tag name for the section. Defaults to {@code div}.
31
+ */
32
+ function getTagName(section) {
33
+ return (section.meta && section.meta.tagname) || DEFAULT_SECTION_TAG;
34
+ }
35
+
36
+ function toHtmlAttribute(value) {
37
+ return Array.isArray(value) ? value.join(' ') : value;
38
+ }
39
+
40
+ function getAttributes(section) {
41
+ const attributeKeys = Object.keys(section.meta);
42
+ // Add system properties as data-hlx-*
43
+ const attributes = attributeKeys
44
+ .filter((k) => SYSTEM_HTML_ATTRIBUTES.indexOf(k) > -1)
45
+ .reduce((result, attr) => Object.assign(result, { [`data-${HELIX_NAMESPACE}${attr}`]: toHtmlAttribute(section.meta[attr]) }), {});
46
+ return attributeKeys
47
+ .filter((k) => [...SYSTEM_HTML_ATTRIBUTES, ...SYSTEM_META_PROPERTIES].indexOf(k) === -1)
48
+ .reduce((result, attr) => {
49
+ // Add invalid HTML attributes as data-*
50
+ if (GLOBAL_HTML_ATTRIBUTES.indexOf(attr) === -1 && !attr.startsWith('data-')) {
51
+ return Object.assign(result, { [`data-${attr}`]: toHtmlAttribute(section.meta[attr]) });
52
+ }
53
+ // Add valid HTML attributes
54
+ return Object.assign(result, { [attr]: toHtmlAttribute(section.meta[attr]) });
55
+ }, attributes);
56
+ }
57
+
58
+ export default function sectionHandler() {
59
+ return function handler(h, node) {
60
+ const n = { ...node };
61
+
62
+ const tagName = getTagName(n);
63
+ const props = getAttributes(n);
64
+ props.class = props.class ? `${DEFAULT_SECTION_CLASS} ${props.class}` : DEFAULT_SECTION_CLASS;
65
+ const children = wrap(all(h, n), true);
66
+
67
+ return h(node, tagName, props, children);
68
+ };
69
+ }
@@ -0,0 +1,27 @@
1
+ /*
2
+ * Copyright 2018 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 { table as fallback } from 'mdast-util-to-hast/lib/handlers/table.js';
13
+
14
+ export default function table() {
15
+ return function handler(h, node) {
16
+ // remove 'none' from align (doesn't occur anymore, maybe leftover from prior versions)
17
+ if (node.align) {
18
+ for (let i = 0; i < node.align.length; i += 1) {
19
+ /* c8 ignore next 3 */
20
+ if (node.align[i] === 'none') {
21
+ node.align[i] = null;
22
+ }
23
+ }
24
+ }
25
+ return fallback(h, node);
26
+ };
27
+ }