howto md to_github

This commit is contained in:
lovebird 2025-03-27 14:58:37 +01:00
parent baf196ff2d
commit be441f8482
2 changed files with 81 additions and 50 deletions

View File

@ -23,7 +23,7 @@
"format": "unix-time"
}
],
"default": "2025-03-27T13:50:50.493Z"
"default": "2025-03-27T13:56:51.679Z"
},
"description": {
"type": "string",

View File

@ -155,6 +155,84 @@ const commons = async (text: string): Promise<string> => {
}
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 ? `<Image src={import('./${item.cover_image.name}')} alt="${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<Image src={import('./${img.name}')} alt="${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
}