blog_publisher_mcp_server

blog_publisher_mcp_server

3.2

This document outlines the creation of a Model Context Protocol (MCP) server for automating the process of uploading blog articles from Obsidian to a specified directory and pushing changes to GitHub.

The MCP blog upload service is designed to streamline the process of managing and publishing blog articles stored in Obsidian. When a user inputs 'blog+article_name', the service automatically copies the specified Markdown file from the local Obsidian directory to a designated directory, processes image links, copies images to the target folder, and pushes the changes to GitHub. This automation reduces manual effort and ensures consistency in file management and publication.

Features

  • {'name': 'Trigger Condition', 'description': "Activates when the user inputs 'blog+article_name'."}
  • {'name': 'File Copying', 'description': 'Copies the specified Markdown file from the Obsidian directory to the target directory.'}
  • {'name': 'Image Processing', 'description': 'Detects and updates image links in the Markdown file, copying images to the target folder.'}
  • {'name': 'GitHub Integration', 'description': 'Commits and pushes changes to GitHub automatically.'}

Usage with Different Platforms

obsidian_to_github

python
import os
import shutil
import subprocess

# Define paths
obsidian_path = 'I:/B-1 笔记/Android/Android'
blog_path = 'I:/B-MioBlogSites/_Android'
assets_path = 'I:/B-MioBlogSites/assets/images'

# Function to copy markdown and images
def copy_markdown_and_images(article_name):
    md_file = f'{obsidian_path}/{article_name}.md'
    target_md_file = f'{blog_path}/{article_name}.md'
    
    # Copy markdown file
    shutil.copy(md_file, target_md_file)
    
    # Process images
    with open(md_file, 'r', encoding='utf-8') as file:
        content = file.read()
    
    # Find and copy images
    image_links = re.findall(r'!\[.*?\]\((.*?)\)', content)
    for image_link in image_links:
        image_name = os.path.basename(image_link)
        shutil.copy(f'{obsidian_path}/z. attachments/{image_name}', f'{assets_path}/{image_name}')
        content = content.replace(image_link, f'../assets/images/{image_name}')
    
    # Write updated content
    with open(target_md_file, 'w', encoding='utf-8') as file:
        file.write(content)

    # Push to GitHub
    subprocess.run(['git', 'add', '.'], cwd=blog_path)
    subprocess.run(['git', 'commit', '-m', f'Add {article_name}'], cwd=blog_path)
    subprocess.run(['git', 'push'], cwd=blog_path)

# Example usage
copy_markdown_and_images('example_article')