Skip to main content
Glama

convert_markdown

Convert HTML to clean Markdown for LLM processing, or Markdown to HTML for web rendering. Preserves structure like headings, lists, code blocks, links, and tables.

Instructions

Convert HTML to clean Markdown, or Markdown to HTML. Use HTML→Markdown when you've fetched a web page and need readable text for an LLM — strips tags, preserves headings, lists, code blocks, links, and tables. Use Markdown→HTML when rendering content in a web context.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYesThe content to convert
fromYesInput format
toYesOutput format

Implementation Reference

  • The core logic function 'handler' that performs the conversion between HTML and Markdown using Turndown and Marked libraries.
    async function handler(input: Input) {
      const { content, from, to, options } = input;
    
      if (from === to) {
        return { output: content, from, to, note: "Input and output formats are the same" };
      }
    
      let output: string;
    
      if (from === "html" && to === "markdown") {
        const td = new TurndownService({
          headingStyle: options?.headingStyle ?? "atx",
          bulletListMarker: options?.bulletListMarker ?? "-",
          codeBlockStyle: options?.codeBlockStyle ?? "fenced",
        });
        output = td.turndown(content);
      } else {
        // markdown → html
        output = await marked(content, { async: true });
      }
    
      return {
        output,
        from,
        to,
        inputLength: content.length,
        outputLength: output.length,
      };
    }
  • The Zod schema definition for input validation, including content, from, to, and conversion options.
    const inputSchema = z.object({
      content: z.string().min(1).max(100_000).describe("The content to convert"),
      from: z.enum(["html", "markdown"]).describe("Input format"),
      to: z.enum(["html", "markdown"]).describe("Output format"),
      options: z
        .object({
          headingStyle: z
            .enum(["atx", "setext"])
            .optional()
            .describe("Markdown heading style: atx (#) or setext (underline)"),
          bulletListMarker: z
            .enum(["-", "*", "+"])
            .optional()
            .describe("Bullet list marker character"),
          codeBlockStyle: z
            .enum(["fenced", "indented"])
            .optional()
            .describe("Code block style in markdown output"),
        })
        .optional(),
    });
  • The tool definition and registration call that makes 'markdown-converter' available. Note that mcp-server/src/index.ts calls it via an API bridge, while this file defines the actual underlying tool.
    const markdownConverterTool: ToolDefinition<Input> = {
      name: "markdown-converter",
      description:
        "Convert between HTML and Markdown. HTML → Markdown for clean agent-readable content; Markdown → HTML for rendering. Handles headings, lists, links, code blocks, tables, and more.",
      version: "1.0.0",
      inputSchema,
      handler,
      metadata: {
        tags: ["markdown", "html", "conversion", "formatting", "content"],
        pricing: "$0.0005 per call",
        exampleInput: {
          content: "<h1>Hello World</h1><p>This is a <strong>bold</strong> paragraph with a <a href='https://example.com'>link</a>.</p><ul><li>Item one</li><li>Item two</li></ul>",
          from: "html",
          to: "markdown",
          options: { headingStyle: "atx", bulletListMarker: "-", codeBlockStyle: "fenced" },
        },
      },
    };
    
    registerTool(markdownConverterTool);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/marras0914/agent-toolbelt'

If you have feedback or need assistance with the MCP directory API, please join our Discord server