vite-plugin-svelte-md 0.1.6 → 0.1.8
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/README.md +1 -1
- package/lib/index.d.mts +35 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +101 -47
- package/lib/index.mjs +93 -41
- package/package.json +24 -25
package/README.md
CHANGED
|
@@ -114,7 +114,7 @@ Add it to `vite.config.js`
|
|
|
114
114
|
```ts
|
|
115
115
|
// vite.config.js
|
|
116
116
|
import { defineConfig } from "vite";
|
|
117
|
-
import { sveltekit } from "@sveltejs/kit/vite"
|
|
117
|
+
import { sveltekit } from "@sveltejs/kit/vite";
|
|
118
118
|
import svelteMd from "vite-plugin-svelte-md";
|
|
119
119
|
|
|
120
120
|
export default defineConfig({
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import MarkdownIt from 'markdown-it';
|
|
3
|
+
import { FilterPattern } from '@rollup/pluginutils';
|
|
4
|
+
|
|
5
|
+
interface Options {
|
|
6
|
+
/**
|
|
7
|
+
* Enable head support
|
|
8
|
+
*
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
headEnabled?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Options passed to Markdown It
|
|
14
|
+
*/
|
|
15
|
+
markdownItOptions?: MarkdownIt.Options;
|
|
16
|
+
/**
|
|
17
|
+
* Plugins for Markdown It
|
|
18
|
+
*/
|
|
19
|
+
markdownItUses?: (MarkdownIt.PluginSimple | [MarkdownIt.PluginSimple | MarkdownIt.PluginWithOptions, any] | any)[];
|
|
20
|
+
/**
|
|
21
|
+
* Class names for wrapper div
|
|
22
|
+
*
|
|
23
|
+
* @default 'markdown-body'
|
|
24
|
+
*/
|
|
25
|
+
wrapperClasses?: string | string[];
|
|
26
|
+
include?: FilterPattern;
|
|
27
|
+
exclude?: FilterPattern;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Creates vite-plugin-svelte-md
|
|
32
|
+
*/
|
|
33
|
+
declare function export_default(options?: Options): Plugin;
|
|
34
|
+
|
|
35
|
+
export { type Options, export_default as default };
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
7
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
9
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
10
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
8
25
|
var __export = (target, all) => {
|
|
9
26
|
for (var name in all)
|
|
10
27
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -17,15 +34,42 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
34
|
}
|
|
18
35
|
return to;
|
|
19
36
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
37
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
38
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
39
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
40
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
41
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
42
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
43
|
+
mod
|
|
44
|
+
));
|
|
21
45
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
46
|
+
var __async = (__this, __arguments, generator) => {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
var fulfilled = (value) => {
|
|
49
|
+
try {
|
|
50
|
+
step(generator.next(value));
|
|
51
|
+
} catch (e) {
|
|
52
|
+
reject(e);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var rejected = (value) => {
|
|
56
|
+
try {
|
|
57
|
+
step(generator.throw(value));
|
|
58
|
+
} catch (e) {
|
|
59
|
+
reject(e);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
63
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
64
|
+
});
|
|
65
|
+
};
|
|
22
66
|
|
|
23
67
|
// src/index.ts
|
|
24
|
-
var
|
|
25
|
-
__export(
|
|
26
|
-
default: () =>
|
|
68
|
+
var index_exports = {};
|
|
69
|
+
__export(index_exports, {
|
|
70
|
+
default: () => index_default
|
|
27
71
|
});
|
|
28
|
-
module.exports = __toCommonJS(
|
|
72
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
73
|
var import_pluginutils = require("@rollup/pluginutils");
|
|
30
74
|
|
|
31
75
|
// src/markdown.ts
|
|
@@ -54,8 +98,7 @@ function plugin(md) {
|
|
|
54
98
|
|
|
55
99
|
// src/utils.ts
|
|
56
100
|
function toArray(n) {
|
|
57
|
-
if (!Array.isArray(n))
|
|
58
|
-
return [n];
|
|
101
|
+
if (!Array.isArray(n)) return [n];
|
|
59
102
|
return n;
|
|
60
103
|
}
|
|
61
104
|
function escapeBraces(test) {
|
|
@@ -90,8 +133,7 @@ var headProperties = [
|
|
|
90
133
|
"bodyAttrs"
|
|
91
134
|
];
|
|
92
135
|
function preprocessHead(frontmatter, options) {
|
|
93
|
-
if (!options.headEnabled)
|
|
94
|
-
return frontmatter;
|
|
136
|
+
if (!options.headEnabled) return frontmatter;
|
|
95
137
|
const head = frontmatter;
|
|
96
138
|
const meta = Array.isArray(head.meta) ? [...head.meta] : [];
|
|
97
139
|
if (head.title) {
|
|
@@ -118,8 +160,7 @@ function preprocessHead(frontmatter, options) {
|
|
|
118
160
|
}
|
|
119
161
|
const result = {};
|
|
120
162
|
for (const [key, value] of Object.entries(head)) {
|
|
121
|
-
if (headProperties.includes(key))
|
|
122
|
-
result[key] = value;
|
|
163
|
+
if (headProperties.includes(key)) result[key] = value;
|
|
123
164
|
}
|
|
124
165
|
if (meta.length > 0) {
|
|
125
166
|
result.meta = meta;
|
|
@@ -129,12 +170,11 @@ function preprocessHead(frontmatter, options) {
|
|
|
129
170
|
function headObjToTags(obj) {
|
|
130
171
|
const tags = [];
|
|
131
172
|
for (const key of Object.keys(obj)) {
|
|
132
|
-
if (obj[key] == null)
|
|
133
|
-
continue;
|
|
173
|
+
if (obj[key] == null) continue;
|
|
134
174
|
if (key === "title") {
|
|
135
175
|
tags.push(`<title>${obj[key]}</title>`);
|
|
136
176
|
} else if (key === "base") {
|
|
137
|
-
tags.push(`<base ${attrs({ key: "default",
|
|
177
|
+
tags.push(`<base ${attrs(__spreadValues({ key: "default" }, obj[key]))}>`);
|
|
138
178
|
} else if (headProperties.includes(key)) {
|
|
139
179
|
const value = obj[key];
|
|
140
180
|
if (Array.isArray(value)) {
|
|
@@ -166,6 +206,9 @@ var TagContent = class {
|
|
|
166
206
|
this.defaultAttrs = defaultAttrs;
|
|
167
207
|
}
|
|
168
208
|
toTag() {
|
|
209
|
+
if (this.contents.length === 0) {
|
|
210
|
+
return "";
|
|
211
|
+
}
|
|
169
212
|
return `${this.startTag || `<${this.tagName}${this.defaultAttrs ? ` ${this.defaultAttrs}` : ""}>`}
|
|
170
213
|
${this.contents.join("\n")}
|
|
171
214
|
</${this.tagName}>`;
|
|
@@ -187,16 +230,10 @@ function parseHtml(html) {
|
|
|
187
230
|
const moduleContext = new TagContent("script", 'context="module"');
|
|
188
231
|
const instanceScript = new TagContent("script");
|
|
189
232
|
const svelteTags = [];
|
|
190
|
-
let newHtml = html.replace(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
scriptContent.addTag(startTag, script);
|
|
196
|
-
return "";
|
|
197
|
-
});
|
|
198
|
-
newHtml = newHtml.replace(SVELTE_TAGS_RE2, (_, startTag, inner) => {
|
|
199
|
-
const tagName = GET_SVELTE_TAG_NAME_RE.exec(startTag.slice(1))[0].toLowerCase();
|
|
233
|
+
let newHtml = html.replace(SVELTE_TAGS_RE2, (_, startTag, inner) => {
|
|
234
|
+
const tagName = GET_SVELTE_TAG_NAME_RE.exec(
|
|
235
|
+
startTag.slice(1)
|
|
236
|
+
)[0].toLowerCase();
|
|
200
237
|
let svelteTag = svelteTags.find((tag) => tag.tagName === tagName);
|
|
201
238
|
if (!svelteTag) {
|
|
202
239
|
svelteTag = new TagContent(tagName);
|
|
@@ -205,15 +242,22 @@ function parseHtml(html) {
|
|
|
205
242
|
svelteTag.addTag(startTag, inner);
|
|
206
243
|
return "";
|
|
207
244
|
});
|
|
245
|
+
newHtml = newHtml.replace(SCRIPTS_RE, (_, startTag, script) => {
|
|
246
|
+
let scriptContent = instanceScript;
|
|
247
|
+
if (IS_MODULE_CONTEXT_RE.test(startTag)) {
|
|
248
|
+
scriptContent = moduleContext;
|
|
249
|
+
}
|
|
250
|
+
scriptContent.addTag(startTag, script);
|
|
251
|
+
return "";
|
|
252
|
+
});
|
|
208
253
|
return { html: newHtml, moduleContext, instanceScript, svelteTags };
|
|
209
254
|
}
|
|
210
255
|
function createMarkdownProcessor(options) {
|
|
211
|
-
const markdownIt = new import_markdown_it.default({
|
|
256
|
+
const markdownIt = new import_markdown_it.default(__spreadValues({
|
|
212
257
|
html: true,
|
|
213
258
|
linkify: true,
|
|
214
|
-
typographer: true
|
|
215
|
-
|
|
216
|
-
});
|
|
259
|
+
typographer: true
|
|
260
|
+
}, options.markdownItOptions));
|
|
217
261
|
markdownIt.linkify.set({ fuzzyLink: false });
|
|
218
262
|
const originalValidateLink = markdownIt.validateLink;
|
|
219
263
|
markdownIt.validateLink = (url) => {
|
|
@@ -228,19 +272,27 @@ function createMarkdownProcessor(options) {
|
|
|
228
272
|
markdownIt.use(plugin3, ...options2);
|
|
229
273
|
});
|
|
230
274
|
return (id, text) => {
|
|
275
|
+
var _a, _b;
|
|
231
276
|
const raw = text.trimEnd();
|
|
232
277
|
const { wrapperClasses, headEnabled } = options;
|
|
233
278
|
const parsedFrontmatter = (0, import_gray_matter.default)(raw);
|
|
234
|
-
const plainMarkdown = (parsedFrontmatter == null ? void 0 : parsedFrontmatter.content)
|
|
279
|
+
const plainMarkdown = (_a = parsedFrontmatter == null ? void 0 : parsedFrontmatter.content) != null ? _a : raw;
|
|
235
280
|
let html = markdownIt.render(plainMarkdown, { id });
|
|
236
281
|
if (wrapperClasses) {
|
|
237
282
|
html = `<div class="${wrapperClasses}">${html}</div>`;
|
|
238
283
|
}
|
|
239
284
|
const parsedHtml = parseHtml(html);
|
|
240
|
-
const { head, frontmatter } = frontmatterPreprocess(
|
|
241
|
-
|
|
285
|
+
const { head, frontmatter } = frontmatterPreprocess(
|
|
286
|
+
(_b = parsedFrontmatter == null ? void 0 : parsedFrontmatter.data) != null ? _b : {},
|
|
287
|
+
options
|
|
288
|
+
);
|
|
289
|
+
parsedHtml.moduleContext.prepend(
|
|
290
|
+
`export const frontmatter = ${JSON.stringify(frontmatter)}`
|
|
291
|
+
);
|
|
242
292
|
if (headEnabled && head) {
|
|
243
|
-
let svelteHead = parsedHtml.svelteTags.find(
|
|
293
|
+
let svelteHead = parsedHtml.svelteTags.find(
|
|
294
|
+
(tag) => tag.tagName === "svelte:head"
|
|
295
|
+
);
|
|
244
296
|
if (!svelteHead) {
|
|
245
297
|
svelteHead = new TagContent("svelte:head");
|
|
246
298
|
parsedHtml.svelteTags.push(svelteHead);
|
|
@@ -261,29 +313,32 @@ function frontmatterPreprocess(frontmatter, options) {
|
|
|
261
313
|
|
|
262
314
|
// src/options.ts
|
|
263
315
|
function resolveOptions(userOptions) {
|
|
264
|
-
|
|
316
|
+
var _a;
|
|
317
|
+
const options = __spreadProps(__spreadValues({
|
|
265
318
|
headEnabled: true,
|
|
266
319
|
markdownItOptions: {},
|
|
267
320
|
markdownItUses: [],
|
|
268
321
|
include: null,
|
|
269
|
-
exclude: null
|
|
270
|
-
|
|
271
|
-
wrapperClasses: toArray(userOptions.wrapperClasses
|
|
272
|
-
};
|
|
322
|
+
exclude: null
|
|
323
|
+
}, userOptions), {
|
|
324
|
+
wrapperClasses: toArray((_a = userOptions.wrapperClasses) != null ? _a : "markdown-body").filter((i) => i).join(" ")
|
|
325
|
+
});
|
|
273
326
|
return options;
|
|
274
327
|
}
|
|
275
328
|
|
|
276
329
|
// src/index.ts
|
|
277
|
-
function
|
|
330
|
+
function index_default(options = {}) {
|
|
278
331
|
const resolvedOptions = resolveOptions(options);
|
|
279
332
|
const mdToSvelte = createMarkdownProcessor(resolvedOptions);
|
|
280
|
-
const filter = (0, import_pluginutils.createFilter)(
|
|
333
|
+
const filter = (0, import_pluginutils.createFilter)(
|
|
334
|
+
resolvedOptions.include || /\.md$/,
|
|
335
|
+
resolvedOptions.exclude
|
|
336
|
+
);
|
|
281
337
|
return {
|
|
282
338
|
name: "vite-plugin-svelte-md",
|
|
283
339
|
enforce: "pre",
|
|
284
340
|
transform(raw, id) {
|
|
285
|
-
if (!filter(id))
|
|
286
|
-
return void 0;
|
|
341
|
+
if (!filter(id)) return void 0;
|
|
287
342
|
try {
|
|
288
343
|
return mdToSvelte(id, raw);
|
|
289
344
|
} catch (e) {
|
|
@@ -292,14 +347,13 @@ function src_default(options = {}) {
|
|
|
292
347
|
return void 0;
|
|
293
348
|
},
|
|
294
349
|
handleHotUpdate(ctx) {
|
|
295
|
-
if (!filter(ctx.file))
|
|
296
|
-
return;
|
|
350
|
+
if (!filter(ctx.file)) return;
|
|
297
351
|
const defaultRead = ctx.read;
|
|
298
|
-
ctx.read =
|
|
299
|
-
return
|
|
352
|
+
ctx.read = function() {
|
|
353
|
+
return __async(this, null, function* () {
|
|
354
|
+
return mdToSvelte(ctx.file, yield defaultRead());
|
|
355
|
+
});
|
|
300
356
|
};
|
|
301
357
|
}
|
|
302
358
|
};
|
|
303
359
|
}
|
|
304
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
305
|
-
0 && (module.exports = {});
|
package/lib/index.mjs
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
1
41
|
// src/index.ts
|
|
2
42
|
import { createFilter } from "@rollup/pluginutils";
|
|
3
43
|
|
|
@@ -27,8 +67,7 @@ function plugin(md) {
|
|
|
27
67
|
|
|
28
68
|
// src/utils.ts
|
|
29
69
|
function toArray(n) {
|
|
30
|
-
if (!Array.isArray(n))
|
|
31
|
-
return [n];
|
|
70
|
+
if (!Array.isArray(n)) return [n];
|
|
32
71
|
return n;
|
|
33
72
|
}
|
|
34
73
|
function escapeBraces(test) {
|
|
@@ -63,8 +102,7 @@ var headProperties = [
|
|
|
63
102
|
"bodyAttrs"
|
|
64
103
|
];
|
|
65
104
|
function preprocessHead(frontmatter, options) {
|
|
66
|
-
if (!options.headEnabled)
|
|
67
|
-
return frontmatter;
|
|
105
|
+
if (!options.headEnabled) return frontmatter;
|
|
68
106
|
const head = frontmatter;
|
|
69
107
|
const meta = Array.isArray(head.meta) ? [...head.meta] : [];
|
|
70
108
|
if (head.title) {
|
|
@@ -91,8 +129,7 @@ function preprocessHead(frontmatter, options) {
|
|
|
91
129
|
}
|
|
92
130
|
const result = {};
|
|
93
131
|
for (const [key, value] of Object.entries(head)) {
|
|
94
|
-
if (headProperties.includes(key))
|
|
95
|
-
result[key] = value;
|
|
132
|
+
if (headProperties.includes(key)) result[key] = value;
|
|
96
133
|
}
|
|
97
134
|
if (meta.length > 0) {
|
|
98
135
|
result.meta = meta;
|
|
@@ -102,12 +139,11 @@ function preprocessHead(frontmatter, options) {
|
|
|
102
139
|
function headObjToTags(obj) {
|
|
103
140
|
const tags = [];
|
|
104
141
|
for (const key of Object.keys(obj)) {
|
|
105
|
-
if (obj[key] == null)
|
|
106
|
-
continue;
|
|
142
|
+
if (obj[key] == null) continue;
|
|
107
143
|
if (key === "title") {
|
|
108
144
|
tags.push(`<title>${obj[key]}</title>`);
|
|
109
145
|
} else if (key === "base") {
|
|
110
|
-
tags.push(`<base ${attrs({ key: "default",
|
|
146
|
+
tags.push(`<base ${attrs(__spreadValues({ key: "default" }, obj[key]))}>`);
|
|
111
147
|
} else if (headProperties.includes(key)) {
|
|
112
148
|
const value = obj[key];
|
|
113
149
|
if (Array.isArray(value)) {
|
|
@@ -139,6 +175,9 @@ var TagContent = class {
|
|
|
139
175
|
this.defaultAttrs = defaultAttrs;
|
|
140
176
|
}
|
|
141
177
|
toTag() {
|
|
178
|
+
if (this.contents.length === 0) {
|
|
179
|
+
return "";
|
|
180
|
+
}
|
|
142
181
|
return `${this.startTag || `<${this.tagName}${this.defaultAttrs ? ` ${this.defaultAttrs}` : ""}>`}
|
|
143
182
|
${this.contents.join("\n")}
|
|
144
183
|
</${this.tagName}>`;
|
|
@@ -160,16 +199,10 @@ function parseHtml(html) {
|
|
|
160
199
|
const moduleContext = new TagContent("script", 'context="module"');
|
|
161
200
|
const instanceScript = new TagContent("script");
|
|
162
201
|
const svelteTags = [];
|
|
163
|
-
let newHtml = html.replace(
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
scriptContent.addTag(startTag, script);
|
|
169
|
-
return "";
|
|
170
|
-
});
|
|
171
|
-
newHtml = newHtml.replace(SVELTE_TAGS_RE2, (_, startTag, inner) => {
|
|
172
|
-
const tagName = GET_SVELTE_TAG_NAME_RE.exec(startTag.slice(1))[0].toLowerCase();
|
|
202
|
+
let newHtml = html.replace(SVELTE_TAGS_RE2, (_, startTag, inner) => {
|
|
203
|
+
const tagName = GET_SVELTE_TAG_NAME_RE.exec(
|
|
204
|
+
startTag.slice(1)
|
|
205
|
+
)[0].toLowerCase();
|
|
173
206
|
let svelteTag = svelteTags.find((tag) => tag.tagName === tagName);
|
|
174
207
|
if (!svelteTag) {
|
|
175
208
|
svelteTag = new TagContent(tagName);
|
|
@@ -178,15 +211,22 @@ function parseHtml(html) {
|
|
|
178
211
|
svelteTag.addTag(startTag, inner);
|
|
179
212
|
return "";
|
|
180
213
|
});
|
|
214
|
+
newHtml = newHtml.replace(SCRIPTS_RE, (_, startTag, script) => {
|
|
215
|
+
let scriptContent = instanceScript;
|
|
216
|
+
if (IS_MODULE_CONTEXT_RE.test(startTag)) {
|
|
217
|
+
scriptContent = moduleContext;
|
|
218
|
+
}
|
|
219
|
+
scriptContent.addTag(startTag, script);
|
|
220
|
+
return "";
|
|
221
|
+
});
|
|
181
222
|
return { html: newHtml, moduleContext, instanceScript, svelteTags };
|
|
182
223
|
}
|
|
183
224
|
function createMarkdownProcessor(options) {
|
|
184
|
-
const markdownIt = new MarkdownIt({
|
|
225
|
+
const markdownIt = new MarkdownIt(__spreadValues({
|
|
185
226
|
html: true,
|
|
186
227
|
linkify: true,
|
|
187
|
-
typographer: true
|
|
188
|
-
|
|
189
|
-
});
|
|
228
|
+
typographer: true
|
|
229
|
+
}, options.markdownItOptions));
|
|
190
230
|
markdownIt.linkify.set({ fuzzyLink: false });
|
|
191
231
|
const originalValidateLink = markdownIt.validateLink;
|
|
192
232
|
markdownIt.validateLink = (url) => {
|
|
@@ -201,19 +241,27 @@ function createMarkdownProcessor(options) {
|
|
|
201
241
|
markdownIt.use(plugin3, ...options2);
|
|
202
242
|
});
|
|
203
243
|
return (id, text) => {
|
|
244
|
+
var _a, _b;
|
|
204
245
|
const raw = text.trimEnd();
|
|
205
246
|
const { wrapperClasses, headEnabled } = options;
|
|
206
247
|
const parsedFrontmatter = grayMatter(raw);
|
|
207
|
-
const plainMarkdown = (parsedFrontmatter == null ? void 0 : parsedFrontmatter.content)
|
|
248
|
+
const plainMarkdown = (_a = parsedFrontmatter == null ? void 0 : parsedFrontmatter.content) != null ? _a : raw;
|
|
208
249
|
let html = markdownIt.render(plainMarkdown, { id });
|
|
209
250
|
if (wrapperClasses) {
|
|
210
251
|
html = `<div class="${wrapperClasses}">${html}</div>`;
|
|
211
252
|
}
|
|
212
253
|
const parsedHtml = parseHtml(html);
|
|
213
|
-
const { head, frontmatter } = frontmatterPreprocess(
|
|
214
|
-
|
|
254
|
+
const { head, frontmatter } = frontmatterPreprocess(
|
|
255
|
+
(_b = parsedFrontmatter == null ? void 0 : parsedFrontmatter.data) != null ? _b : {},
|
|
256
|
+
options
|
|
257
|
+
);
|
|
258
|
+
parsedHtml.moduleContext.prepend(
|
|
259
|
+
`export const frontmatter = ${JSON.stringify(frontmatter)}`
|
|
260
|
+
);
|
|
215
261
|
if (headEnabled && head) {
|
|
216
|
-
let svelteHead = parsedHtml.svelteTags.find(
|
|
262
|
+
let svelteHead = parsedHtml.svelteTags.find(
|
|
263
|
+
(tag) => tag.tagName === "svelte:head"
|
|
264
|
+
);
|
|
217
265
|
if (!svelteHead) {
|
|
218
266
|
svelteHead = new TagContent("svelte:head");
|
|
219
267
|
parsedHtml.svelteTags.push(svelteHead);
|
|
@@ -234,29 +282,32 @@ function frontmatterPreprocess(frontmatter, options) {
|
|
|
234
282
|
|
|
235
283
|
// src/options.ts
|
|
236
284
|
function resolveOptions(userOptions) {
|
|
237
|
-
|
|
285
|
+
var _a;
|
|
286
|
+
const options = __spreadProps(__spreadValues({
|
|
238
287
|
headEnabled: true,
|
|
239
288
|
markdownItOptions: {},
|
|
240
289
|
markdownItUses: [],
|
|
241
290
|
include: null,
|
|
242
|
-
exclude: null
|
|
243
|
-
|
|
244
|
-
wrapperClasses: toArray(userOptions.wrapperClasses
|
|
245
|
-
};
|
|
291
|
+
exclude: null
|
|
292
|
+
}, userOptions), {
|
|
293
|
+
wrapperClasses: toArray((_a = userOptions.wrapperClasses) != null ? _a : "markdown-body").filter((i) => i).join(" ")
|
|
294
|
+
});
|
|
246
295
|
return options;
|
|
247
296
|
}
|
|
248
297
|
|
|
249
298
|
// src/index.ts
|
|
250
|
-
function
|
|
299
|
+
function index_default(options = {}) {
|
|
251
300
|
const resolvedOptions = resolveOptions(options);
|
|
252
301
|
const mdToSvelte = createMarkdownProcessor(resolvedOptions);
|
|
253
|
-
const filter = createFilter(
|
|
302
|
+
const filter = createFilter(
|
|
303
|
+
resolvedOptions.include || /\.md$/,
|
|
304
|
+
resolvedOptions.exclude
|
|
305
|
+
);
|
|
254
306
|
return {
|
|
255
307
|
name: "vite-plugin-svelte-md",
|
|
256
308
|
enforce: "pre",
|
|
257
309
|
transform(raw, id) {
|
|
258
|
-
if (!filter(id))
|
|
259
|
-
return void 0;
|
|
310
|
+
if (!filter(id)) return void 0;
|
|
260
311
|
try {
|
|
261
312
|
return mdToSvelte(id, raw);
|
|
262
313
|
} catch (e) {
|
|
@@ -265,15 +316,16 @@ function src_default(options = {}) {
|
|
|
265
316
|
return void 0;
|
|
266
317
|
},
|
|
267
318
|
handleHotUpdate(ctx) {
|
|
268
|
-
if (!filter(ctx.file))
|
|
269
|
-
return;
|
|
319
|
+
if (!filter(ctx.file)) return;
|
|
270
320
|
const defaultRead = ctx.read;
|
|
271
|
-
ctx.read =
|
|
272
|
-
return
|
|
321
|
+
ctx.read = function() {
|
|
322
|
+
return __async(this, null, function* () {
|
|
323
|
+
return mdToSvelte(ctx.file, yield defaultRead());
|
|
324
|
+
});
|
|
273
325
|
};
|
|
274
326
|
}
|
|
275
327
|
};
|
|
276
328
|
}
|
|
277
329
|
export {
|
|
278
|
-
|
|
330
|
+
index_default as default
|
|
279
331
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-svelte-md",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Vite plugin to convert markdown to svelte template",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib"
|
|
@@ -46,47 +46,46 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/ota-meshi/vite-plugin-svelte-md#readme",
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"vite": "^2.0.0 || ^3.0.0"
|
|
49
|
+
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@rollup/pluginutils": "^
|
|
52
|
+
"@rollup/pluginutils": "^5.0.0",
|
|
53
53
|
"gray-matter": "^4.0.3",
|
|
54
54
|
"markdown-it": "^13.0.0"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@ota-meshi/eslint-plugin": "^0.
|
|
57
|
+
"@ota-meshi/eslint-plugin": "^0.15.0",
|
|
58
58
|
"@types/chai": "^4.2.22",
|
|
59
59
|
"@types/escape-html": "^1.0.1",
|
|
60
|
-
"@types/
|
|
61
|
-
"@types/
|
|
62
|
-
"@types/
|
|
63
|
-
"@types/node": "^16.11.11",
|
|
60
|
+
"@types/markdown-it": "^13.0.0",
|
|
61
|
+
"@types/mocha": "^10.0.0",
|
|
62
|
+
"@types/node": "^20.0.0",
|
|
64
63
|
"@types/prismjs": "^1.16.6",
|
|
65
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
66
|
-
"@typescript-eslint/parser": "^
|
|
64
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
65
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
67
66
|
"chai": "^4.3.4",
|
|
68
67
|
"escape-html": "^1.0.3",
|
|
69
68
|
"eslint": "^8.3.0",
|
|
70
|
-
"eslint-config-prettier": "^
|
|
69
|
+
"eslint-config-prettier": "^9.0.0",
|
|
71
70
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
72
|
-
"eslint-plugin-json-schema-validator": "^
|
|
71
|
+
"eslint-plugin-json-schema-validator": "^5.0.0",
|
|
73
72
|
"eslint-plugin-jsonc": "^2.0.0",
|
|
74
|
-
"eslint-plugin-
|
|
75
|
-
"eslint-plugin-node-dependencies": "^0.
|
|
76
|
-
"eslint-plugin-prettier": "^
|
|
77
|
-
"eslint-plugin-regexp": "^
|
|
73
|
+
"eslint-plugin-n": "^17.0.0",
|
|
74
|
+
"eslint-plugin-node-dependencies": "^0.12.0",
|
|
75
|
+
"eslint-plugin-prettier": "^5.0.0",
|
|
76
|
+
"eslint-plugin-regexp": "^2.0.0",
|
|
78
77
|
"mocha": "^10.0.0",
|
|
79
|
-
"mocha-chai-jest-snapshot": "
|
|
80
|
-
"nyc": "^
|
|
81
|
-
"prettier": "^
|
|
82
|
-
"prettier-plugin-svelte": "^
|
|
78
|
+
"mocha-chai-jest-snapshot": "1.1.3",
|
|
79
|
+
"nyc": "^17.0.0",
|
|
80
|
+
"prettier": "^3.0.0",
|
|
81
|
+
"prettier-plugin-svelte": "^3.0.2",
|
|
83
82
|
"prism-svelte": "^0.4.7",
|
|
84
83
|
"prismjs": "^1.25.0",
|
|
85
|
-
"rimraf": "^
|
|
86
|
-
"svelte": "^
|
|
84
|
+
"rimraf": "^6.0.0",
|
|
85
|
+
"svelte": "^4.0.0",
|
|
87
86
|
"ts-node": "^10.4.0",
|
|
88
|
-
"tsup": "^
|
|
89
|
-
"typescript": "
|
|
90
|
-
"vite": "^
|
|
87
|
+
"tsup": "^8.0.0",
|
|
88
|
+
"typescript": "~5.0.0",
|
|
89
|
+
"vite": "^4.0.0"
|
|
91
90
|
}
|
|
92
91
|
}
|