This commit is contained in:
Timothy Jaeryang Baek committed 2026-02-19 16:09:54 -06:00
1 parent 12bea8cd88
commit 6ac593209c
1 file changed
+41 -4
+41 -4
View File
@@ -13,6 +13,7 @@
getSkillById, getSkillById,
getSkillItems, getSkillItems,
exportSkills, exportSkills,
createNewSkill,
deleteSkillById, deleteSkillById,
toggleSkillById toggleSkillById
} from '$lib/apis/skills'; } from '$lib/apis/skills';
@@ -198,21 +199,54 @@
bind:this={importInputElement} bind:this={importInputElement}
bind:files={importFiles} bind:files={importFiles}
type="file" type="file"
accept=".md" accept=".md,.json"
hidden hidden
on:change={() => { on:change={() => {
if (importFiles && importFiles.length > 0) { if (importFiles && importFiles.length > 0) {
const file = importFiles[0];
const ext = file.name.split('.').pop()?.toLowerCase();
if (ext === 'json') {
// JSON import: create skills via API
const reader = new FileReader();
reader.onload = async (event) => {
try {
const content = event.target?.result;
if (typeof content !== 'string') return;
const parsedSkills = JSON.parse(content);
const items = Array.isArray(parsedSkills) ? parsedSkills : [parsedSkills];
for (const skill of items) {
await createNewSkill(localStorage.token, skill).catch(
(error) => {
toast.error(`${error}`);
}
);
}
toast.success($i18n.t('Skill imported successfully'));
page = 1;
loadSkillItems();
_skills.set(await getSkills(localStorage.token));
} catch (e) {
toast.error($i18n.t('Invalid JSON file'));
}
};
reader.readAsText(file);
} else {
// Markdown import: parse frontmatter and open in editor
const reader = new FileReader(); const reader = new FileReader();
reader.onload = (event) => { reader.onload = (event) => {
const mdContent = event.target?.result; const mdContent = event.target?.result;
if (typeof mdContent === 'string') { if (typeof mdContent === 'string') {
const fm = parseFrontmatter(mdContent); const fm = parseFrontmatter(mdContent);
const fileName = importFiles[0].name.replace(/\.md$/, ''); const fileName = file.name.replace(/\.md$/, '');
const rawName = fm.name || fileName; const rawName = fm.name || fileName;
const displayName = formatSkillName(rawName); const displayName = formatSkillName(rawName);
sessionStorage.skill = JSON.stringify({ sessionStorage.skill = JSON.stringify({
name: displayName, name: displayName,
id: fm.name || '', // Use raw frontmatter name as ID if available id: fm.name || '',
description: fm.description || '', description: fm.description || '',
content: mdContent, content: mdContent,
is_active: true, is_active: true,
@@ -221,7 +255,10 @@
goto('/workspace/skills/create'); goto('/workspace/skills/create');
} }
}; };
reader.readAsText(importFiles[0]); reader.readAsText(file);
}
importInputElement.value = '';
} }
}} }}
/> />