SEO Front Matter
Generating and maintaining good SEO front matter fields can be a tedious task. GenAIScript can help you automate this process.
The script below will generate SEO information and update the existing file. The script uses a custom merge strategy to merge the new front matter with the existing front matter.
script({ model: "large", accept: ".md,.mdx", parameters: { force: false, },})
// force refreshing all filesconst { force, dbg } = env.vars
// filter out files that don't have a front matter.descriptionconst file = env.files[0]const fm = MD.frontmatter(file.content)if (!force && fm?.description) cancel("file already has description")if (file.content?.includes("autogenerated")) cancel("file is autogenerated")
// insert markdown files in contextconst res = await runPrompt( (ctx) => { ctx.def("FILE", file)
// prompt to generate front matter for markdown files ctx.$`##Role
You are a search engine optimization expert at creating front matter for markdown document.
## Task
Generate the front matter content as the new file content.
## Guidance
- Update description as needed.- Update keywords as needed, only 5 keywords or less.- optimize for search engine optimization.- If no front matter is present, generate it.
## Things to avoid
- Do NOT repeat project name (GenAIScript) in 'title' field- DO NOT modify the existing 'title' or 'sidebar' fields.- Do NOT use 'Guide' in title.` }, { responseType: "json_schema", responseSchema: { title: "", description: "", keywords: [""], }, })
const frontmatter = res.jsonconst { title, description, keywords, tags } = frontmatterfile.content = MD.updateFrontmatter(file.content, { title, description, keywords, tags,})await workspace.writeFiles(file)
Batching over all files
Section titled “Batching over all files”Once the script has been tuned on a few files, you can automate using the CLI. The CLI has a —apply-edits flag to apply the changes to the file.
for file in src/**/*.md; do genaiscript run frontmatter "$file" --apply-edits
You can run this command in your CI/CD pipeline to keep your SEO front matter up to date.