markdown-frontmatter.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { describe, expect, it } from "vitest";
  2. import { parseMarkdownFrontmatter } from "../src/plugins/markdown-frontmatter.js";
  3. describe("parseMarkdownFrontmatter", () => {
  4. it("reads a plain single-line description", () => {
  5. const content = ["---", "name: airtable-overview", "description: Explains the data model.", "---", "", "# Body"].join(
  6. "\n",
  7. );
  8. expect(parseMarkdownFrontmatter(content)).toEqual({
  9. name: "airtable-overview",
  10. description: "Explains the data model.",
  11. });
  12. });
  13. it("folds a `>` block scalar instead of returning the indicator", () => {
  14. // Bugfix 回归:市场详情技能描述此前只剩一个 `>`。
  15. const content = [
  16. "---",
  17. "name: 42crunch-api-security-testing",
  18. "description: >",
  19. " Automate API security directly in your editor with 42Crunch -",
  20. " automatically audit OpenAPI specs and apply AI-powered fixes.",
  21. "---",
  22. "",
  23. "# Body",
  24. ].join("\n");
  25. const result = parseMarkdownFrontmatter(content);
  26. expect(result.name).toBe("42crunch-api-security-testing");
  27. expect(result.description).toBe(
  28. "Automate API security directly in your editor with 42Crunch - automatically audit OpenAPI specs and apply AI-powered fixes.",
  29. );
  30. expect(result.description).not.toBe(">");
  31. });
  32. it("keeps newlines for a `|` literal block scalar", () => {
  33. const content = [
  34. "---",
  35. "description: |",
  36. " line one",
  37. " line two",
  38. "---",
  39. ].join("\n");
  40. expect(parseMarkdownFrontmatter(content).description).toBe("line one\nline two");
  41. });
  42. it("strips surrounding quotes from scalar values", () => {
  43. const content = ["---", 'name: "quoted-name"', "description: 'single quoted'", "---"].join("\n");
  44. expect(parseMarkdownFrontmatter(content)).toEqual({
  45. name: "quoted-name",
  46. description: "single quoted",
  47. });
  48. });
  49. it("returns empty object when there is no frontmatter", () => {
  50. expect(parseMarkdownFrontmatter("# Just a heading\n")).toEqual({});
  51. });
  52. it("omits keys whose block scalar bodies are empty", () => {
  53. const content = ["---", "name: only-name", "description: >", "---"].join("\n");
  54. expect(parseMarkdownFrontmatter(content)).toEqual({ name: "only-name" });
  55. });
  56. });