sveltekit-data-plugin 1.2.2 → 1.3.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.
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
  );
@@ -110,19 +117,15 @@
110
117
  buildStart() {
111
118
  opts.plugin_context = this;
112
119
  },
113
- async transformFile(src) {
114
- var _a;
115
- const base2 = path.resolve(((_a = opts.vite_config) == null ? void 0 : _a.root) ?? "", opts.base);
116
- const d = fm(src);
117
- const attributes = await convert(opts, d.attributes, base2);
118
- return { attributes, body: d.body };
119
- },
120
120
  async transform(src, id) {
121
+ var _a;
121
122
  if (/\.(md)$/.test(id)) {
122
- const { attributes, body } = await this.transformFile(src);
123
+ const base2 = path.resolve(((_a = opts.vite_config) == null ? void 0 : _a.root) ?? "", opts.base);
124
+ const d = fm(src);
125
+ const attributes = await convert(opts, d.attributes, base2);
123
126
  return `
124
127
  ${Object.entries(attributes).map(([key, value]) => `export const ${key} = ${JSON.stringify(value)};`).join("\n")}
125
- export const body = ${JSON.stringify(body)};`;
128
+ export const body = ${JSON.stringify(d.body)};`;
126
129
  }
127
130
  }
128
131
  };
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
  );
@@ -109,19 +116,15 @@ function importDataPlugin(imagetools_plugin, { base = "./src/data" } = {}) {
109
116
  buildStart() {
110
117
  opts.plugin_context = this;
111
118
  },
112
- async transformFile(src) {
113
- var _a;
114
- const base2 = path.resolve(((_a = opts.vite_config) == null ? void 0 : _a.root) ?? "", opts.base);
115
- const d = fm(src);
116
- const attributes = await convert(opts, d.attributes, base2);
117
- return { attributes, body: d.body };
118
- },
119
119
  async transform(src, id) {
120
+ var _a;
120
121
  if (/\.(md)$/.test(id)) {
121
- const { attributes, body } = await this.transformFile(src);
122
+ const base2 = path.resolve(((_a = opts.vite_config) == null ? void 0 : _a.root) ?? "", opts.base);
123
+ const d = fm(src);
124
+ const attributes = await convert(opts, d.attributes, base2);
122
125
  return `
123
126
  ${Object.entries(attributes).map(([key, value]) => `export const ${key} = ${JSON.stringify(value)};`).join("\n")}
124
- export const body = ${JSON.stringify(body)};`;
127
+ export const body = ${JSON.stringify(d.body)};`;
125
128
  }
126
129
  }
127
130
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sveltekit-data-plugin",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "author": {
5
5
  "name": "Jonny Thaw",
6
6
  "url": "https://jthaw.me"