From be441f8482f41b2f03831aff6f5202e3996d128e Mon Sep 17 00:00:00 2001 From: babayaga Date: Thu, 27 Mar 2025 14:58:37 +0100 Subject: [PATCH] howto md to_github --- .astro/collections/resources.schema.json | 2 +- src/model/howto.ts | 129 ++++++++++++++--------- 2 files changed, 81 insertions(+), 50 deletions(-) diff --git a/.astro/collections/resources.schema.json b/.astro/collections/resources.schema.json index b9b82bf..59341b2 100644 --- a/.astro/collections/resources.schema.json +++ b/.astro/collections/resources.schema.json @@ -23,7 +23,7 @@ "format": "unix-time" } ], - "default": "2025-03-27T13:50:50.493Z" + "default": "2025-03-27T13:56:51.679Z" }, "description": { "type": "string", diff --git a/src/model/howto.ts b/src/model/howto.ts index 307d5cc..e956fa9 100644 --- a/src/model/howto.ts +++ b/src/model/howto.ts @@ -155,6 +155,84 @@ const commons = async (text: string): Promise => { } const content = async (str: string, filters: FilterFunction[] = default_filters) => await applyFilters(str, filters) +const to_github = async (item: IHowto) => { + const itemDir = item_path(item) + mkdir(itemDir) + + // Create README.md with all content + const readmeContent = [ + '---', + `title: ${item.title}`, + `slug: ${item.slug}`, + `description: ${item.description}`, + `tags: ${JSON.stringify(item.tags)}`, + `category: ${item.category?.label || 'uncategorized'}`, + `difficulty: ${item.difficulty_level}`, + `time: ${item.time}`, + `location: ${item.user?.geo ? `${item.user.geo.city ? `${item.user.geo.city}, ` : ''}${item.user.geo.countryName || ''}` : ''}`, + '---', + '', + `# ${item.title}`, + '', + item.cover_image ? `![${item.title}](${item.cover_image.name})` : '', + '', + item.description, + item.user?.geo ? `\nUser Location: ${item.user.geo.city ? `${item.user.geo.city}, ` : ''}${item.user.geo.countryName || ''}` : '', + '', + '## Steps', + '', + ...item.steps.map((step, index) => [ + `### Step ${index + 1}: ${step.title}`, + '', + step.text, + '', + // Add step images if any + ...step.images.map(img => `\n![${img.name}](./${img.name})\n`) + ].join('\n')) + ].filter(Boolean).join('\n') + write(path.join(itemDir, 'README.md'), readmeContent) +} + +const to_mdx = async (item: IHowto) => { + const itemDir = item_path(item) + mkdir(itemDir) + + // Create index.mdx with all content + const mdxContent = [ + '---', + `title: ${item.title}`, + `slug: ${item.slug}`, + `description: ${item.description}`, + `tags: ${JSON.stringify(item.tags)}`, + `category: ${item.category?.label || 'uncategorized'}`, + `difficulty: ${item.difficulty_level}`, + `time: ${item.time}`, + `location: ${item.user?.geo ? `${item.user.geo.city ? `${item.user.geo.city}, ` : ''}${item.user.geo.countryName || ''}` : ''}`, + '---', + '', + `import { Image } from 'astro:assets'`, + '', + `# ${item.title}`, + '', + item.cover_image ? `${item.title}` : '', + '', + item.description, + item.user?.geo ? `\nUser Location: ${item.user.geo.city ? `${item.user.geo.city}, ` : ''}${item.user.geo.countryName || ''}` : '', + '', + '## Steps', + '', + ...item.steps.map((step, index) => [ + `### Step ${index + 1}: ${step.title}`, + '', + step.text, + '', + // Add step images if any using Astro's Image component + ...step.images.map(img => `\n${img.name}\n`) + ].join('\n')) + ].filter(Boolean).join('\n') + write(path.join(itemDir, 'index.mdx'), mdxContent) +} + const complete = async (item: IHowto) => { if (!HOWTO_ANNOTATIONS) { // return item @@ -193,55 +271,8 @@ const complete = async (item: IHowto) => { ...item.steps.map(step => step.text) ].filter(Boolean).join('\n\n') - const itemDir = item_path(item) - - // Store description - const descriptionContent = [ - '---', - `title: ${item.title}`, - `slug: ${item.slug}`, - `description: ${item.description}`, - `tags: ${JSON.stringify(item.tags)}`, - `category: ${item.category?.label || 'uncategorized'}`, - `difficulty: ${item.difficulty_level}`, - `time: ${item.time}`, - `location: ${item.user?.geo ? `${item.user.geo.city ? `${item.user.geo.city}, ` : ''}${item.user.geo.countryName || ''}` : ''}`, - '---', - '', - `# ${item.title}`, - '', - item.cover_image ? `![${item.title}](${item.cover_image.src})` : '', - '', - item.description, - userLocation - ].filter(Boolean).join('\n') - write(path.join(itemDir, `${item.slug}.md`), descriptionContent) - - // Store each step - await pMap(item.steps, async (step, index) => { - const stepSlug = slugify(step.title) - const stepContent = [ - '---', - `title: ${step.title}`, - `slug: ${stepSlug}`, - `step: ${index + 1}`, - `howto: ${item.slug}`, - `tags: ${JSON.stringify(item.tags)}`, - `category: ${item.category?.label || 'uncategorized'}`, - `difficulty: ${item.difficulty_level}`, - `time: ${item.time}`, - `location: ${item.user?.geo ? `${item.user.geo.city ? `${item.user.geo.city}, ` : ''}${item.user.geo.countryName || ''}` : ''}`, - '---', - '', - `# Step ${index + 1}: ${step.title}`, - '', - step.text, - '', - // Add step images if any - ...step.images.map(img => `![${img.alt}](${img.name})`) - ].filter(Boolean).join('\n') - write(path.join(itemDir, `${stepSlug}.md`), stepContent) - }) + await to_github(item) + await to_mdx(item) return item }