API 文档

HTML2WEB 接口说明

HTML2WEB API 接口文档

提供 Markdown 转换和内容分享功能的 RESTful API 接口

文件创建 API
创建 HTML 或 Markdown 文件并自动生成分享链接
POST

接口地址

/api/files

请求参数

{
  "content": "string (required)",
  "type": "string (html|markdown, required)",
  "title": "string (optional)",
  "filename": "string (optional)",
  "isPublic": "boolean (default: false)",
  "createdBy": "string (optional, admin ID)"
}

请求示例

{
  "content": "# Hello World\n\nThis is **bold** text.",
  "type": "markdown",
  "title": "My Document",
  "filename": "hello-world",
  "isPublic": true
}

响应示例

{
  "success": true,
  "file": {
    "id": "file123",
    "filename": "hello-world",
    "title": "My Document",
    "type": "markdown",
    "shareUrl": "/preview/share456",
    "isPublic": true,
    "createdAt": "2024-01-01T00:00:00.000Z"
  },
  "message": "文件创建成功"
}

代码示例

// 创建文件
async function createFile() {
  const response = await fetch('/api/files', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      content: '# Hello World\n\nThis is **bold** text.',
      type: 'markdown',
      title: 'My Document',
      filename: 'hello-world',
      isPublic: true
    })
  })

  const result = await response.json()
  console.log('分享链接:', result.file.shareUrl)
}
使用示例
常见的使用场景和代码示例

JavaScript/TypeScript

// 转换 Markdown
async function convertMarkdown() {
  const response = await fetch('/api/markdown', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      markdown: '# Hello World\n\nThis is **bold** text.',
      options: {
        enableGfm: true,
        enableHighlight: true,
        wrapper: true
      }
    })
  })
  
  const result = await response.json()
  console.log(result.html)
}

// 创建分享
async function createShare() {
  const response = await fetch('/api/share', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      htmlCode: '<h1>Hello World</h1>',
      markdownCode: '# Hello World',
      type: 'markdown'
    })
  })
  
  const result = await response.json()
  console.log(`分享链接: /preview/${result.id}`)
}

cURL

# 转换 Markdown
curl -X POST http://localhost:3000/api/markdown \
  -H "Content-Type: application/json" \
  -d '{
    "markdown": "# Hello World\n\nThis is **bold** text.",
    "options": {
      "enableGfm": true,
      "enableHighlight": true,
      "wrapper": true
    }
  }'

# 创建分享
curl -X POST http://localhost:3000/api/share \
  -H "Content-Type: application/json" \
  -d '{
    "htmlCode": "<h1>Hello World</h1>",
    "markdownCode": "# Hello World",
    "type": "markdown"
  }'