Skip to content

Commit e868e43

Browse files
committed
doc: code comments in utils function
1 parent 1fe0eec commit e868e43

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

utils.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,22 @@ export function splitObjectByKeys(obj, keys) {
2121
}
2222

2323
export function processAttributesAndConfig(nodeUrl, config = {}) {
24-
// clone the config object to avoid side effects for other images
24+
25+
// Clone/deepcopy the config object to avoid side effects for other images
26+
// Due to the way we process the config, we need to make sure we don't modify the original object
27+
// => https://github.com/lzinga/mdsvex-enhanced-images/issues/9
2528
let configCopy = JSON.parse(JSON.stringify(config));
2629

2730
// create a standard url (to cleanly parse the query string)
2831
const url = new URL(nodeUrl, "http://localhost"); // base url is irrelevant but needed by URL constructor
2932

30-
/* CSS class names handling */
33+
/* Query string CSS class names handling */
3134
// get possible "class" entries from the query string
3235
const searchParams = new URLSearchParams(url.search);
3336
const classesInQuery = searchParams
3437
.getAll("class")
35-
.flatMap((e) => e.split(";"));
38+
.flatMap((e) => e.split(";")) // class names can be separated by ';' in the query string
39+
.map((c)=> c.trim());
3640

3741
// merge classes from the query string with the ones from the config (if any)
3842
// - normalize the possible classes from config
@@ -54,7 +58,8 @@ export function processAttributesAndConfig(nodeUrl, config = {}) {
5458
const combinedClassesAttrStr =
5559
allClasses.length > 0 ? `class="${allClasses.join(" ")}"` : "";
5660

57-
// classes processed: remove them from searchParams and config
61+
// Classes processed: remove them from searchParams and config
62+
// here, we mutate the configCopy object, but as it's a copy, so no side effects (see above)
5863
searchParams.delete("class");
5964
if (configCopy.attributes) {
6065
delete configCopy.attributes.class;
@@ -65,14 +70,13 @@ export function processAttributesAndConfig(nodeUrl, config = {}) {
6570
// get the rest of the query string as attributes
6671
const urlParamsAttributes = Object.fromEntries(searchParams);
6772

68-
// split the attributes from urlParamsAttributes into attributes and directives
73+
// split the attributes from urlParamsAttributes into attributes and image directives
6974
const [attributes, directives] = splitObjectByKeys(
7075
urlParamsAttributes,
7176
possibleAttributes
7277
);
7378

74-
// Combine config.attributes with attributes from URL, with URL parameters taking precedence
75-
79+
// Combine/merge config.attributes with attributes from URL, with URL parameters taking precedence
7680
const combinedAttributes = {
7781
...(configCopy?.attributes ?? {}),
7882
...attributes,
@@ -82,16 +86,17 @@ export function processAttributesAndConfig(nodeUrl, config = {}) {
8286
.map(([key, value]) => `${key}="${value}"`)
8387
.join(" ");
8488

85-
// Combine directives from config with directives from URL, with URL parameters taking precedence
89+
// Combine/merge image directives from config with image directives from URL, with URL parameters taking precedence
8690
const combinedDirectives = {
8791
...(configCopy?.imagetoolsDirectives ?? {}),
8892
...directives,
8993
};
9094

91-
// finally, format the combined directives as URL parameters
95+
// Finally, format the combined directives as URL parameters
9296
const combinedDirectivesStr = Object.entries(combinedDirectives)
9397
.map(([key, value]) => `${key}=${value}`)
9498
.join("&");
99+
95100
const combinedDirectivesUrlParams = combinedDirectivesStr
96101
? `&${combinedDirectivesStr}`
97102
: "";

0 commit comments

Comments
 (0)