-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-import-instagram.js
More file actions
51 lines (47 loc) · 1.6 KB
/
Copy pathauto-import-instagram.js
File metadata and controls
51 lines (47 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
export const autoImportInstagram = {
name: 'auto-import-instagram',
markup({ content, filename }) {
if (!filename) return;
const isMarkdown = /(\.md|\.svx|\.svelte\.md)$/.test(filename);
if (!isMarkdown) return;
if (!content.includes('<Instagram')) return;
const scriptRegex = /<script((?:\s+[^>]*?)?)>([\s\S]*?)<\/script>/gi;
let instanceScriptMatch = null;
let match;
// Find the first instance script (not context="module")
while ((match = scriptRegex.exec(content)) !== null) {
const attrs = match[1];
if (!attrs.includes('context="module"')) {
instanceScriptMatch = match;
break;
}
}
if (instanceScriptMatch) {
const [fullMatch, attrs, innerContent] = instanceScriptMatch;
// Inject import at the top of the existing script
const newScript = `<script${attrs}>\n\timport { Instagram } from '$lib/components';${innerContent}</script>`;
return {
code: content.replace(fullMatch, newScript)
};
} else {
// No instance script found, create one
// Check for frontmatter
const frontmatterRegex = /^---\r?\n([\s\S]+?)\r?\n---/;
const match = content.match(frontmatterRegex);
if (match) {
// Insert after frontmatter
const frontmatterEnd = match[0].length;
const before = content.slice(0, frontmatterEnd);
const after = content.slice(frontmatterEnd);
return {
code: `${before}\n<script>\n\timport { Instagram } from '$lib/components';\n</script>\n${after}`
};
} else {
// No frontmatter, insert at top
return {
code: `<script>\n\timport { Instagram } from '$lib/components';\n</script>\n${content}`
};
}
}
}
};