sveltekit-data-plugin 1.2.3 → 1.3.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/dist/plugin.js CHANGED
@@ -37,14 +37,20 @@
37
37
  throw new Error(`Failed parsing string to object: ${str}`);
38
38
  }
39
39
  }
40
- async function convert(opts, obj, base, depth = 1) {
40
+ async function convert(opts, obj, { base, relative, depth = 1 } = {}) {
41
41
  if (typeof obj !== "object") {
42
42
  if (typeof obj === "string" && REGEX.IMAGE.test(obj)) {
43
- const img = await opts.imagetools_plugin.load.call(
44
- opts.plugin_context,
45
- stampURL(obj, base)
46
- );
47
- return parseObject(img.slice("export default".length, -1));
43
+ try {
44
+ const img = await opts.imagetools_plugin.load.call(
45
+ opts.plugin_context,
46
+ stampURL(obj, obj.startsWith("./") && relative ? relative : base)
47
+ );
48
+ return parseObject(img.slice("export default".length, -1));
49
+ } catch (e) {
50
+ return {
51
+ src: null
52
+ };
53
+ }
48
54
  }
49
55
  if (typeof obj === "string" && REGEX.YOUTUBE.test(obj)) {
50
56
  const oembedData = await oembed("youtube", obj);
@@ -52,14 +58,15 @@
52
58
  }
53
59
  if (typeof obj === "string" && /^\S*(.md)$/.test(obj) && depth === 1) {
54
60
  try {
55
- const contents = await fs.readFile(path.resolve(base, obj), "utf-8");
61
+ const markdownFile = path.resolve(base, obj);
62
+ const relativeFolder = path.basename(markdownFile);
63
+ const contents = await fs.readFile(markdownFile, "utf-8");
56
64
  const data = fm(contents);
57
- const attributes = await convert(
58
- opts,
59
- data.attributes,
65
+ const attributes = await convert(opts, data.attributes, {
60
66
  base,
61
- depth + 1
62
- );
67
+ relative: relativeFolder,
68
+ depth: depth + 1
69
+ });
63
70
  return {
64
71
  attributes: {
65
72
  ...attributes,
@@ -77,11 +84,11 @@
77
84
  Object.entries(obj).map(async ([key, value]) => {
78
85
  if (Array.isArray(value)) {
79
86
  const values = await Promise.all(
80
- value.map((item) => convert(opts, item, base))
87
+ value.map((item) => convert(opts, item, { base, depth, relative }))
81
88
  );
82
89
  return [key, values];
83
90
  }
84
- const v = await convert(opts, value, base);
91
+ const v = await convert(opts, value, { base, depth, relative });
85
92
  return [key, v];
86
93
  })
87
94
  );
@@ -115,7 +122,7 @@
115
122
  if (/\.(md)$/.test(id)) {
116
123
  const base2 = path.resolve(((_a = opts.vite_config) == null ? void 0 : _a.root) ?? "", opts.base);
117
124
  const d = fm(src);
118
- const attributes = await convert(opts, d.attributes, base2);
125
+ const attributes = await convert(opts, d.attributes, { base: base2 });
119
126
  return `
120
127
  ${Object.entries(attributes).map(([key, value]) => `export const ${key} = ${JSON.stringify(value)};`).join("\n")}
121
128
  export const body = ${JSON.stringify(d.body)};`;
package/dist/plugin.mjs CHANGED
@@ -36,14 +36,20 @@ function parseObject(str) {
36
36
  throw new Error(`Failed parsing string to object: ${str}`);
37
37
  }
38
38
  }
39
- async function convert(opts, obj, base, depth = 1) {
39
+ async function convert(opts, obj, { base, relative, depth = 1 } = {}) {
40
40
  if (typeof obj !== "object") {
41
41
  if (typeof obj === "string" && REGEX.IMAGE.test(obj)) {
42
- const img = await opts.imagetools_plugin.load.call(
43
- opts.plugin_context,
44
- stampURL(obj, base)
45
- );
46
- return parseObject(img.slice("export default".length, -1));
42
+ try {
43
+ const img = await opts.imagetools_plugin.load.call(
44
+ opts.plugin_context,
45
+ stampURL(obj, obj.startsWith("./") && relative ? relative : base)
46
+ );
47
+ return parseObject(img.slice("export default".length, -1));
48
+ } catch (e) {
49
+ return {
50
+ src: null
51
+ };
52
+ }
47
53
  }
48
54
  if (typeof obj === "string" && REGEX.YOUTUBE.test(obj)) {
49
55
  const oembedData = await oembed("youtube", obj);
@@ -51,14 +57,15 @@ async function convert(opts, obj, base, depth = 1) {
51
57
  }
52
58
  if (typeof obj === "string" && /^\S*(.md)$/.test(obj) && depth === 1) {
53
59
  try {
54
- const contents = await fs.readFile(path.resolve(base, obj), "utf-8");
60
+ const markdownFile = path.resolve(base, obj);
61
+ const relativeFolder = path.basename(markdownFile);
62
+ const contents = await fs.readFile(markdownFile, "utf-8");
55
63
  const data = fm(contents);
56
- const attributes = await convert(
57
- opts,
58
- data.attributes,
64
+ const attributes = await convert(opts, data.attributes, {
59
65
  base,
60
- depth + 1
61
- );
66
+ relative: relativeFolder,
67
+ depth: depth + 1
68
+ });
62
69
  return {
63
70
  attributes: {
64
71
  ...attributes,
@@ -76,11 +83,11 @@ async function convert(opts, obj, base, depth = 1) {
76
83
  Object.entries(obj).map(async ([key, value]) => {
77
84
  if (Array.isArray(value)) {
78
85
  const values = await Promise.all(
79
- value.map((item) => convert(opts, item, base))
86
+ value.map((item) => convert(opts, item, { base, depth, relative }))
80
87
  );
81
88
  return [key, values];
82
89
  }
83
- const v = await convert(opts, value, base);
90
+ const v = await convert(opts, value, { base, depth, relative });
84
91
  return [key, v];
85
92
  })
86
93
  );
@@ -114,7 +121,7 @@ function importDataPlugin(imagetools_plugin, { base = "./src/data" } = {}) {
114
121
  if (/\.(md)$/.test(id)) {
115
122
  const base2 = path.resolve(((_a = opts.vite_config) == null ? void 0 : _a.root) ?? "", opts.base);
116
123
  const d = fm(src);
117
- const attributes = await convert(opts, d.attributes, base2);
124
+ const attributes = await convert(opts, d.attributes, { base: base2 });
118
125
  return `
119
126
  ${Object.entries(attributes).map(([key, value]) => `export const ${key} = ${JSON.stringify(value)};`).join("\n")}
120
127
  export const body = ${JSON.stringify(d.body)};`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sveltekit-data-plugin",
3
- "version": "1.2.3",
3
+ "version": "1.3.1",
4
4
  "author": {
5
5
  "name": "Jonny Thaw",
6
6
  "url": "https://jthaw.me"