X Article Publisher Skill

repository·main·Indexed 21 days ago

https://github.com/wshuyi/x-article-publisher-skill

An automation tool for Claude Code that converts Markdown files into rich-text X (Twitter) Articles. It utilizes Playwright MCP and Python to automate the browser, handling the upload of cover images, titles, and content images using a precise block-index strategy. The tool saves articles as drafts for manual review and supports Markdown syntax including H2 headers, bold, italics, hyperlinks, blockquotes, lists, dividers, tables, and Mermaid diagrams.

Tokens
5.8K
Snippets
12
Records
28
Agent score
70%

What's inside x-article-publisher-skill

  1. Understand project structure and core components

    main

    The project is organized into skills and scripts. The core logic for X Article publishing resides in the skills/x-article-publisher/ directory:

    • SKILL.md: Contains the core instructions for the skill.
    • scripts/parse_markdown.py: Handles Markdown parsing.
    • scripts/copy_to_clipboard.py: Manages clipboard operations (currently macOS specific).
    x-article-publisher-skill/
    ├── .claude-plugin/
    │   └── plugin.json           # Plugin configuration
    ├── skills/
    │   └── x-article-publisher/
    │       ├── SKILL.md          # Skill core instructions
    │       └── scripts/
    │           ├── parse_markdown.py    # Markdown parsing
    │           └── copy_to_clipboard.py # Clipboard operations
    ├── docs/
    │   └── GUIDE.md              # This documentation
    ├── README.md
    └── LICENSE
  2. How to use browser_wait_for correctly

    main

    When using the Playwright MCP browser_wait_for tool, be aware that providing both a time and a condition (like textGone) causes a delay because the tool waits for the full time duration before checking the condition.

    Best Practices:

    • To wait for a condition to disappear: Use only textGone. Let Playwright poll automatically.
      browser_wait_for textGone="正在上传媒体"
    • To wait for a fixed duration: Use only time.
    • Avoid: Using textGone + time together, as this results in unnecessary idle time.
  3. Efficiency guidelines for browser automation

    main

    When automating the X Articles editor via Playwright, follow these principles to minimize latency and errors:

    1. Avoid redundant snapshots

    Most browser actions (click, type, press_key) return the updated page state. Do not call browser_snapshot manually after every action; use the state returned by the action itself.

    2. Minimize browser_wait_for

    Only use browser_wait_for for specific asynchronous events like image uploads (e.g., waiting for textGone="正在上传媒体"). Do not use it to wait for buttons or input fields that are already present in the DOM.

    3. Parallelize independent tasks

    If two tasks have no dependency, run them in parallel in a single message:

    • Allowed: Filling the title (browser_type) + Copying HTML to clipboard (Bash).
    • Not Allowed: Clicking 'create' + Uploading cover (must click create first).

    4. Use direct element references

    Each browser operation returns element references. Use these references directly for subsequent steps instead of re-searching the page.

    5. Front-load preparation

    Complete all data parsing (Markdown $\rightarrow$ JSON) and file generation (HTML to /tmp/) before starting any browser interactions. This ensures the browser phase is a continuous, uninterrupted sequence of actions.

  4. How image positioning works via Block-Index

    main

    In v1.1.0, the skill uses a Block-Index strategy for precise image placement instead of unstable text matching.

    Mechanism:

    1. parse_markdown.py extracts a block_index for each image, representing the Nth block element after which the image should appear (0-indexed).
    2. The skill uses a Reverse Insertion Strategy: It inserts images starting from the highest block_index down to the lowest. This ensures that inserting an image does not shift the indices of subsequent target locations.

    Example Data Structure:

    {
      "content_images": [
        {
          "path": "/path/to/img1.jpg",
          "block_index": 5,
          "after_text": "context text..."
        }
      ],
      "total_blocks": 12
    }
    {
      "content_images": [
        {
          "path": "/path/to/img1.jpg",
          "block_index": 5,
          "after_text": "上下文文字(仅用于调试)..."
        }
      ],
      "total_blocks": 12
    }
  5. How Block-Index positioning works

    main

    To ensure images are inserted into the correct locations without causing layout shifts, the skill uses a block_index system.

    1. Parsing: The parse_markdown.py script identifies the number of block elements (paragraphs, headings, etc.) and assigns a block_index to each image based on which block it should follow.
    2. Reverse Insertion: Images are inserted in descending order (from the highest block_index to the lowest). This prevents the insertion of an image from shifting the indices of subsequent blocks, ensuring every image lands in its intended position.

    Example JSON output from the parser:

    {
      "title": "2024 年最值得关注的 5 个 AI 工具",
      "cover_image": "./images/cover.jpg",
      "content_images": [
        {"path": "./images/claude-demo.png", "block_index": 4},
        {"path": "./images/midjourney.jpg", "block_index": 6}
      ],
      "total_blocks": 7
    }

    In this example, the image with block_index: 6 is inserted first, followed by block_index: 4.

  6. Publish Markdown articles to X

    main

    You can trigger the publishing workflow using natural language or the specific skill command.

    Natural Language

    Ask Claude to publish a file directly:

    • Publish /path/to/article.md to X
    • Help me post this article to X Articles: ~/Documents/my-post.md

    Skill Command

    Use the explicit command for precision:

    /x-article-publisher /path/to/article.md

    Note: The skill only saves the article as a draft in the X Articles editor. It never publishes automatically, allowing you to review the content before going live.

    /x-article-publisher /path/to/article.md
  7. Install X Article Publisher Skill prerequisites

    main

    To use this skill, ensure you have the following requirements met:

    Core Requirements

    • Playwright MCP: For browser automation.
    • X (Twitter) Account: Must be logged in with a Premium Plus subscription.
    • Python: Version 3.9 or higher.

    OS-Specific Python Dependencies

    Install the necessary Python packages based on your operating system:

    macOS:

    pip install Pillow pyobjc-framework-Cocoa

    Windows:

    pip install Pillow pywin32 clip-util

    Optional: Mermaid Diagrams

    If your Markdown contains Mermaid diagrams, install the Mermaid CLI globally:

    npm install -g @mermaid-js/mermaid-cli
  8. Insert Dividers in X Articles via Menu

    main

    Markdown dividers (---) cannot be pasted as HTML <hr> tags because X Articles will ignore them. You must use the X Articles built-in menu to insert them.

    Insertion Steps

    For each divider in the dividers array, process them in reverse order of block_index:

    1. Click the block element: Use the block_index to click the correct position in the editor.
    2. Open Insert menu: Click the "Insert" or "添加媒体" (Add Media) button.
    3. Select Divider: Click the "Divider" or "分割线" menu item.

    To avoid layout issues, follow this sequence:

    1. Insert all images (starting from the highest block_index down to the lowest).
    2. Insert all dividers (starting from the highest block_index down to the lowest).
    # 1. Click the block element at block_index position
    browser_click on the element at position block_index in the editor
    
    # 2. Open Insert menu (Add Media button)
    browser_click on "Insert" or "添加媒体" button
    
    # 3. Click Divider menu item
    browser_click on "Divider" or "分割线" menuitem
  9. Configure Prerequisites for X Article Publisher Skill

    main

    Before using the skill, ensure the following requirements are met:

    1. X Premium Plus Subscription: You must have access to the X Articles editor at https://x.com/compose/articles.
    2. Playwright MCP: The skill requires Playwright MCP for browser automation. Verify installation with:
      cat ~/.claude/settings.json | grep playwright
      If missing, ask Claude Code to 帮我安装 Playwright MCP.
    3. Python Dependencies: Install the required Python packages:
      pip install Pillow pyobjc-framework-Cocoa
      Verify the installation with:
      python -c "from AppKit import NSPasteboard; print('OK')"
  10. Install X Article Publisher Skill

    main

    You can install the skill using two methods: Git Clone (recommended for manual management) or via the Plugin Marketplace.

    Method 1: Git Clone

    Clone the repository and copy the skill directory to your Claude skills folder:

    git clone https://github.com/wshuyi/x-article-publisher-skill.git
    cp -r x-article-publisher-skill/skills/x-article-publisher ~/.claude/skills/

    Method 2: Plugin Marketplace

    Use the following commands within your environment:

    /plugin marketplace add wshuyi/x-article-publisher-skill
    /plugin install x-article-publisher@wshuyi/x-article-publisher-skill
    git clone https://github.com/wshuyi/x-article-publisher-skill.git
    cp -r x-article-publisher-skill/skills/x-article-publisher ~/.claude/skills/
  11. Configure System Requirements and Dependencies

    main

    Before using the skill, ensure you meet the following requirements:

    Core Requirements

    • Claude Code: Installed via claude.ai/code.
    • Playwright MCP: Required for browser automation.
    • X Premium Plus: Required to access the X Articles feature.
    • Python 3.9+
    • OS: macOS or Windows.

    OS-Specific Python Dependencies

    Install the necessary Python packages based on your operating system:

    macOS:

    pip install Pillow pyobjc-framework-Cocoa

    Windows:

    pip install Pillow pywin32 clip-util

    Optional: Mermaid Diagram Support

    To support Mermaid diagrams, install the Mermaid CLI globally via npm:

    npm install -g @mermaid-js/mermaid-cli
  12. Configure Environment Requirements

    main

    Before using the skill, ensure you meet the following requirements:

    • Claude Code: Installed and available.
    • Playwright MCP: Required for browser automation.
    • X Premium Plus: An active subscription is required to use the X Articles feature.
    • Python 3.9+: Required for parsing and clipboard operations.
    • Operating System: macOS or Windows (Linux support is in development).

    Install OS-Specific Dependencies

    macOS:

    pip install Pillow pyobjc-framework-Cocoa

    Windows:

    pip install Pillow pywin32 clip-util

    Optional (for Mermaid diagram support):

    npm install -g @mermaid-js/mermaid-cli
    pip install Pillow pyobjc-framework-Cocoa # macOS
    pip install Pillow pywin32 clip-util # Windows