Skip to main content
Glama

telegraph_export_page

Export Telegraph pages to Markdown or HTML format for backup, sharing, or content migration purposes.

Instructions

Export a Telegraph page to Markdown or HTML format

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the Telegraph page
formatNoExport formatmarkdown

Implementation Reference

  • Handler function logic that executes the telegraph_export_page tool: parses input, fetches the page using telegraph.getPage, converts content to markdown or html, and returns structured JSON output.
    if (name === 'telegraph_export_page') {
      const input = ExportPageSchema.parse(args);
      const page = await telegraph.getPage(input.path, true);
    
      if (!page.content) {
        throw new Error('Page has no content');
      }
    
      const content = input.format === 'markdown'
        ? nodesToMarkdown(page.content)
        : nodesToHtml(page.content);
    
      return {
        content: [{
          type: 'text' as const,
          text: JSON.stringify({
            title: page.title,
            path: page.path,
            url: page.url,
            format: input.format,
            content,
          }, null, 2),
        }],
      };
    }
  • Zod schema defining input parameters for telegraph_export_page tool: path (required) and format (markdown or html).
    export const ExportPageSchema = z.object({
      path: z.string().describe('Path to the Telegraph page'),
      format: z.enum(['markdown', 'html']).default('markdown').describe('Export format'),
    });
  • Tool registration entry in exportTools array, including name, description, and JSON inputSchema.
    {
      name: 'telegraph_export_page',
      description: 'Export a Telegraph page to Markdown or HTML format',
      inputSchema: {
        type: 'object' as const,
        properties: {
          path: {
            type: 'string',
            description: 'Path to the Telegraph page',
          },
          format: {
            type: 'string',
            enum: ['markdown', 'html'],
            default: 'markdown',
            description: 'Export format',
          },
        },
        required: ['path'],
      },
    },
  • Helper function to convert Telegraph page nodes to Markdown format, used in the handler.
    function nodesToMarkdown(nodes: Node[]): string {
      let markdown = '';
    
      for (const node of nodes) {
        if (typeof node === 'string') {
          markdown += node;
        } else {
          markdown += nodeElementToMarkdown(node as NodeElement);
        }
      }
    
      return markdown;
    }
  • Helper function to convert individual Telegraph NodeElement to Markdown, supporting various tags.
    function nodeElementToMarkdown(node: NodeElement): string {
      const children = node.children ? nodesToMarkdown(node.children) : '';
    
      switch (node.tag) {
        case 'h3':
          return `\n# ${children}\n`;
        case 'h4':
          return `\n## ${children}\n`;
        case 'p':
          return `\n${children}\n`;
        case 'b':
        case 'strong':
          return `**${children}**`;
        case 'i':
        case 'em':
          return `*${children}*`;
        case 'a':
          return `[${children}](${node.attrs?.href || ''})`;
        case 'img':
          return `![image](${node.attrs?.src || ''})`;
        case 'figure':
          return children;
        case 'figcaption':
          return `\n*${children}*\n`;
        case 'ul':
          return `\n${children}`;
        case 'ol':
          return `\n${children}`;
        case 'li':
          return `- ${children}\n`;
        case 'blockquote':
          return `\n> ${children}\n`;
        case 'code':
          return `\`${children}\``;
        case 'pre':
          return `\n\`\`\`\n${children}\n\`\`\`\n`;
        case 'br':
          return '\n';
        case 'hr':
          return '\n---\n';
        case 's':
          return `~~${children}~~`;
        case 'u':
          return children; // No markdown equivalent
        case 'aside':
          return `\n*${children}*\n`;
        default:
          return children;
      }
    }

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/NehoraiHadad/telegraph-mcp'

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