sveltekit-data-plugin 1.1.1 → 1.2.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 +31 -3
- package/dist/plugin.mjs +30 -1
- package/package.json +1 -1
package/dist/plugin.js
CHANGED
|
@@ -1,7 +1,31 @@
|
|
|
1
1
|
(function(global, factory) {
|
|
2
|
-
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory(require("front-matter"), require("path"), require("fs/promises")) : typeof define === "function" && define.amd ? define(["front-matter", "path", "fs/promises"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, global["Sveltekit Data Plugin"] = factory(global.fm, global.path, global.fs));
|
|
3
|
-
})(this, function(fm, path, fs) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory(require("front-matter"), require("path"), require("fs/promises"), require("node:fe")) : typeof define === "function" && define.amd ? define(["front-matter", "path", "fs/promises", "node:fe"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, global["Sveltekit Data Plugin"] = factory(global.fm, global.path, global.fs, global.fetch));
|
|
3
|
+
})(this, function(fm, path, fs, fetch) {
|
|
4
4
|
"use strict";
|
|
5
|
+
const REGEX = {
|
|
6
|
+
IMAGE: /^\S*(jpg|png|webp|avif|jpeg)$/,
|
|
7
|
+
YOUTUBE: /^(https?\:\/\/)?((www\.)?youtube\.com|youtu\.be)\/.+$/
|
|
8
|
+
};
|
|
9
|
+
function oembed(type, url) {
|
|
10
|
+
switch (type) {
|
|
11
|
+
case "youtube":
|
|
12
|
+
return fetch(`https://youtube.com/oembed?url=${url}&format=json`).then((resp) => resp.json()).catch(() => {
|
|
13
|
+
return {
|
|
14
|
+
src: url,
|
|
15
|
+
width: 1280,
|
|
16
|
+
height: 720,
|
|
17
|
+
type: "error"
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
default:
|
|
21
|
+
return Promise.resolve({
|
|
22
|
+
src: url,
|
|
23
|
+
width: 1280,
|
|
24
|
+
height: 720,
|
|
25
|
+
type: "error"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
5
29
|
function stampURL(src, base) {
|
|
6
30
|
return `${path.resolve(base, src)}?enhanced`;
|
|
7
31
|
}
|
|
@@ -15,13 +39,17 @@
|
|
|
15
39
|
}
|
|
16
40
|
async function convert(opts, obj, base, depth = 1) {
|
|
17
41
|
if (typeof obj !== "object") {
|
|
18
|
-
if (typeof obj === "string" &&
|
|
42
|
+
if (typeof obj === "string" && REGEX.IMAGE.test(obj)) {
|
|
19
43
|
const img = await opts.imagetools_plugin.load.call(
|
|
20
44
|
opts.plugin_context,
|
|
21
45
|
stampURL(obj, base)
|
|
22
46
|
);
|
|
23
47
|
return parseObject(img.slice("export default".length, -1));
|
|
24
48
|
}
|
|
49
|
+
if (typeof obj === "string" && REGEX.YOUTUBE.test(obj)) {
|
|
50
|
+
const oembedData = await oembed("youtube", obj);
|
|
51
|
+
return { ...oembedData };
|
|
52
|
+
}
|
|
25
53
|
if (typeof obj === "string" && /^\S*(.md)$/.test(obj) && depth === 1) {
|
|
26
54
|
try {
|
|
27
55
|
const contents = await fs.readFile(path.resolve(base, obj), "utf-8");
|
package/dist/plugin.mjs
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
import fm from "front-matter";
|
|
2
2
|
import path from "path";
|
|
3
3
|
import fs from "fs/promises";
|
|
4
|
+
import fetch from "node:fe";
|
|
5
|
+
const REGEX = {
|
|
6
|
+
IMAGE: /^\S*(jpg|png|webp|avif|jpeg)$/,
|
|
7
|
+
YOUTUBE: /^(https?\:\/\/)?((www\.)?youtube\.com|youtu\.be)\/.+$/
|
|
8
|
+
};
|
|
9
|
+
function oembed(type, url) {
|
|
10
|
+
switch (type) {
|
|
11
|
+
case "youtube":
|
|
12
|
+
return fetch(`https://youtube.com/oembed?url=${url}&format=json`).then((resp) => resp.json()).catch(() => {
|
|
13
|
+
return {
|
|
14
|
+
src: url,
|
|
15
|
+
width: 1280,
|
|
16
|
+
height: 720,
|
|
17
|
+
type: "error"
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
default:
|
|
21
|
+
return Promise.resolve({
|
|
22
|
+
src: url,
|
|
23
|
+
width: 1280,
|
|
24
|
+
height: 720,
|
|
25
|
+
type: "error"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
4
29
|
function stampURL(src, base) {
|
|
5
30
|
return `${path.resolve(base, src)}?enhanced`;
|
|
6
31
|
}
|
|
@@ -14,13 +39,17 @@ function parseObject(str) {
|
|
|
14
39
|
}
|
|
15
40
|
async function convert(opts, obj, base, depth = 1) {
|
|
16
41
|
if (typeof obj !== "object") {
|
|
17
|
-
if (typeof obj === "string" &&
|
|
42
|
+
if (typeof obj === "string" && REGEX.IMAGE.test(obj)) {
|
|
18
43
|
const img = await opts.imagetools_plugin.load.call(
|
|
19
44
|
opts.plugin_context,
|
|
20
45
|
stampURL(obj, base)
|
|
21
46
|
);
|
|
22
47
|
return parseObject(img.slice("export default".length, -1));
|
|
23
48
|
}
|
|
49
|
+
if (typeof obj === "string" && REGEX.YOUTUBE.test(obj)) {
|
|
50
|
+
const oembedData = await oembed("youtube", obj);
|
|
51
|
+
return { ...oembedData };
|
|
52
|
+
}
|
|
24
53
|
if (typeof obj === "string" && /^\S*(.md)$/.test(obj) && depth === 1) {
|
|
25
54
|
try {
|
|
26
55
|
const contents = await fs.readFile(path.resolve(base, obj), "utf-8");
|