summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/posthtml-urls/lib/index.js
blob: f9a49f0aa355767257967a418f293248b83fa327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use strict";
const DEFAULT_OPTIONS = require("./defaultOptions");
const evaluateValue = require("evaluate-value");
const list2Array = require("list-to-array");
const parseMetaRefresh = require("http-equiv-refresh");
const parseSrcset = require("parse-srcset");

const CONTENT_ATTR = "content";
const PING_ATTR = "ping";
const SRCSET_ATTR = "srcset";

const DELIMITER = ",";
const EMPTY_STRING = "";
const EMPTY_TAG_GROUP = Object.freeze({});
const FUNCTION_TYPE = "function";
const PRETTY_DELIMITER = ", ";



const plugin = options =>
{
	const {eachURL, filter} = Object.assign({}, DEFAULT_OPTIONS, options);

	if (typeof eachURL !== FUNCTION_TYPE)
	{
		throw new TypeError("eachURL option must be a function");
	}

	// const tagMatchers = Object.keys(filter).map(tagName => ({ tag: tagName }));

	// Called by PostHTML
	return tree =>
	{
		const promises = [];

		tree.walk(node =>
		// tree.match(tagMatchers, (node) =>
		{
			if (node.attrs === undefined) {
				return node;
			}

			const tagGroup = filter[node.tag] || EMPTY_TAG_GROUP;

			Object.keys(node.attrs).forEach(attrName =>
			{
				const isAcceptedTagAttr = attrName in tagGroup && evaluateValue(tagGroup[attrName], node, attrName);

				if (isAcceptedTagAttr)
				{
					switch (attrName)
					{
						case CONTENT_ATTR:
						{
							promises.push( transformMetaRefresh(node, attrName, eachURL) );
							break;
						}
						case PING_ATTR:
						{
							promises.push( transformCommaSeparated(node, attrName, eachURL) );
							break;
						}
						case SRCSET_ATTR:
						{
							promises.push( transformSrcset(node, attrName, eachURL) );
							break;
						}
						default:
						{
							promises.push( transformDefault(node, attrName, eachURL) );
						}
					}
				}
			});

			return node;
		});

		return Promise.all(promises).then(() => tree);
	};
};



const transformCommaSeparated = ({attrs, tag}, attrName, transformer) =>
{
	const urls = list2Array( attrs[attrName], DELIMITER );

	if (urls.length > 0)
	{
		const promises = urls.map(value => Promise.resolve( transformer(value, attrName, tag) ));

		return Promise.all(promises).then(newUrls => attrs[attrName] = newUrls.join(PRETTY_DELIMITER));
	}
};



const transformDefault = ({attrs, tag}, attrName, transformer) =>
{
	return Promise.resolve( transformer( attrs[attrName], attrName, tag ) )
	.then(newUrl => attrs[attrName] = newUrl);
};



const transformMetaRefresh = ({attrs, tag}, attrName, transformer) =>
{
	const {timeout, url} = parseMetaRefresh( attrs[attrName] );

	if (timeout !== null)
	{
		return Promise.resolve( transformer(url || "", attrName, tag) )
		.then(newUrl => attrs[attrName] = `${timeout}; url=${newUrl}`);
	}
};



const transformSrcset = ({attrs, tag}, attrName, transformer) =>
{
	const values = parseSrcset( attrs[attrName] );

	if (values.length > 0)
	{
		const promises = values.map(({d, h, url, w}) => Promise.resolve( transformer(url, attrName, tag) )
		.then(newUrl =>
		{
			d = d !== undefined ? ` ${d}x` : EMPTY_STRING;
			h = h !== undefined ? ` ${h}h` : EMPTY_STRING;
			w = w !== undefined ? ` ${w}w` : EMPTY_STRING;

			return `${newUrl}${w}${h}${d}`;
		}));

		return Promise.all(promises).then(newValues => attrs[attrName] = newValues.join(PRETTY_DELIMITER));
	}
};



module.exports = plugin;