wxmp - WeChat Official Account Markdown Editor

repository·master·Indexed 19 days ago

https://github.com/jaywcjlove/wxmp

A specialized Markdown editor designed for WeChat Official Account authors to create visually appealing articles. It bridges standard Markdown syntax with WeChat's formatting requirements, offering a web-based editor and a desktop application. Key features include custom CSS styling via HTML comments, remote Markdown loading via URL parameters, theme customization, and a dedicated copy command to export rendered HTML optimized for the WeChat platform.

Tokens
4.5K
Snippets
20
Records
22
Agent score
68%

What's inside wxmp

  1. Overview of wxmp: WeChat Official Account Markdown Editor

    master

    wxmp is an online Markdown editor specifically designed for creating beautiful and concise articles for WeChat Official Accounts. It allows users to use standard Markdown syntax to generate content that is optimized for the WeChat platform's visual style.

    Key Information:

    • Primary Use Case: Creating WeChat Official Account articles using Markdown.
    • Platform: Primarily a web-based editor. Note that development on the Chrome extension has been paused in favor of expanding features in the web version.
    • Deployment Options: Available as a web application and as a desktop application (via releases).
  2. Load Markdown content via URL parameters

    master

    You can load a remote Markdown file directly into the editor by appending the md parameter to the application URL.

    URL Format: https://<URL>?md=<Markdown_Resource_URL>

    Example: https://jaywcjlove.github.io/wxmp/#/?theme=underscore&md=https://raw.githubusercontent.com/jaywcjlove/c-tutorial/master/README.md

    https://jaywcjlove.github.io/wxmp/#/?theme=underscore&md=https://raw.githubusercontent.com/jaywcjlove/c-tutorial/master/README.md
  3. Apply custom styles in Markdown using HTML comments

    master

    You can apply custom CSS styles directly within your Markdown content using the <!--rehype:style=...--> syntax. This is useful for styling specific text elements or entire blocks like headings. The syntax starts with <!--rehype:style= and ends with -->, with standard CSS properties inside.

    Examples:

    • Styling text: _text_<!--rehype:style=color: red;--> sets the text color to red.
    • Styling headings: <!--rehype:style=display: flex; ...;--> can be used to apply layout styles to a heading.
    <!--rehype:style=color: red;background: #ff000033;-->
    
    ## 定义标题样式
    <!--rehype:style=display: flex; height: 230px; align-items: center; justify-content: center; font-size: 38px;-->
  4. Hide content from WeChat Markdown editor preview

    master

    To mark content that should be ignored (hidden) in the WeChat Markdown editor preview but still visible in other Markdown viewers (like GitHub), wrap the content in specific HTML comment tags:

    • Start tag: <!--rehype:ignore:start-->
    • End tag: <!--rehype:ignore:end-->
    # 注释忽略
    
    <!--rehype:ignore:start-->内容在微信 Markdown 编辑器预览中不显示。在其它预览工具中展示内容。<!--rehype:ignore:end-->
  5. Deploy wxmp using Docker

    master

    You can quickly deploy the WeChat Markdown Editor web application using Docker.

    1. Pull the image:

    docker pull wcjiang/wxmp
    # Or
    docker pull ghcr.io/jaywcjlove/wxmp:latest

    2. Run the container: Map port 3000 inside the container to port 8113 on your host to access the application.

    docker run --name wxmp --rm -d -p 8113:3000 wcjiang/wxmp:latest

    After running, access the application at http://localhost:8113/ (Note: The README mentions http://localhost:96611/ but the command uses 8113).

    docker run --name wxmp --rm -d -p 8113:3000 wcjiang/wxmp:latest
  6. Customize themes in the wxmp source code

    master

    To create or modify themes, you can add CSS definitions to the project source:

    1. Theme Files: Place default theme definitions in website/src/themes.
    2. Theme Configuration: Configure themes in website/src/store/context.tsx.

    Supported CSS Selectors for Theming: Themes use standard CSS selectors. Note that complex selectors are not supported. Key selectors include:

    • Typography: h1 through h6, a, strong, del, em, u, p, blockquote.
    • Lists: ul, ol, li.
    • Tables: table, td, th.
    • Code Blocks: pre, .code-highlight, .code-line, .code-spans.
    • GFM Footnotes: sup, .footnotes-title, .footnotes-list.
    • Images: .image-warpper, .image.
    • Syntax Highlighting: .comment, .property, .function, .keyword, .punctuation, .unit, .tag, .color, .selector, .quote, .number, .attr-name, .attr-value.
    /* Example theme CSS structure */
    h1 {} 
    h2 {} 
    a { color: red; } 
    pre {} 
    .code-highlight {} 
    .code-line {} 
    .code-spans {}
  7. Use the Context API for Global State

    master

    The application uses a React Context named Context to manage global state. Developers can consume this context to access and update the editor's content, selected themes, and loading states.

    The CreateContext interface defines the following state properties and setters:

    PropertyTypeDescription
    markdownstringThe current Markdown content
    setMarkdownDispatch<SetStateAction<string>>Setter for Markdown content
    themeThemeValueThe selected editor CodeMirror theme
    setThemeDispatch<SetStateAction<ThemeValue>>Setter for editor theme
    previewThemePreviewThemeValueThe selected Markdown preview theme
    setPreviewThemeDispatch<SetStateAction<PreviewThemeValue>>Setter for preview theme
    cssstringThe current CSS applied to the preview
    setCssDispatch<SetStateAction<string>>Setter for preview CSS
    preColorstringThe color used for theme replacements
    setPreColorDispatch<SetStateAction<string>>Setter for the replacement color
    isLoadingbooleanLoading state indicator
    setIsLoadingDispatch<SetStateAction<boolean>>Setter for loading state
    import { useContext } from 'react';
    import { Context } from './store/context';
    
    const MyComponent = () => {
      const { markdown, setMarkdown, theme, setTheme } = useContext(Context);
      // ...
    };
  8. Configure the Markdown Editor theme

    master

    The theme command allows users to switch between different editor themes. It uses a selection menu that iterates through available editorThemes. When a theme is selected, it updates the editor's visual style via the application context.

    // The command is exported as an ICommand object
    export const theme: ICommand = {
      name: 'theme',
      keyCommand: 'theme',
      button: (command, props, opts) => <ThemeView command={command} editorProps={{ ...props, ...opts }} />,
    };
  9. Configure the App.createWindow method

    master

    The createWindow method initializes the main application window. It accepts an Options object and an optional loadURL string.

    Default Behavior:

    • Dimensions: Defaults to 850x600.
    • Web Preferences: Enables nodeIntegration: true, nodeIntegrationInWorker: true, and sets contextIsolation: false by default.
    • Development Mode: If process.env.NODE_ENV === 'development', it attempts to load the provided loadURL (defaulting to http://localhost:3000/) and automatically opens the Developer Tools.
    • Production Mode: Loads the file specified in options.webpath using win.loadFile().
    • Link Handling: Automatically intercepts window.open calls. External web links are opened in the system browser, while internal links are allowed in a modal window.
    interface Options extends Electron.BrowserWindowConstructorOptions {
      preload?: string;
      webpath?: string;
    }
    
    // Usage example:
    await appInstance.createWindow({
      width: 1200,
      height: 800,
      preload: '/path/to/preload.js',
      webpath: 'path/to/index.html'
    });
  10. Initialize the application with the App class

    master

    To start the application, instantiate the App class from @wcj/wxmp-main and call createWindow(options). The options object configures the preload script and the web content path.

    Depending on the environment, you should provide different paths:

    • Development: Use require.resolve to point to @wcj/wxmp-preload and the build directory of the website.
    • Production: Use absolute paths for the preload script and relative paths for the web content.
    const { App } = require('@wcj/wxmp-main');
    
    (async () => {
      const options = {
        preload: '/path/to/preload.js',
        webpath: '/path/to/index.html'
      };
      const app = new App();
      await app.createWindow(options);
    })();
  11. Configure the Preview theme

    master

    The previeTheme command (note the spelling in the source) allows users to switch the theme of the Markdown preview pane. Selecting a preview theme updates both the previewTheme state and applies the corresponding CSS via setCss from the application context.

    // The command is exported as an ICommand object
    export const previeTheme: ICommand = {
      name: 'previewTtheme',
      keyCommand: 'previewTtheme',
      button: () => <ThemePreviewView />,
    };
  12. Convert Markdown to HTML with `markdownToHTML`

    master

    The markdownToHTML function converts a Markdown string into an HTML string, applying custom CSS styles and supporting advanced features like GFM (GitHub Flavored Markdown), KaTeX math formulas, and Prism.js syntax highlighting.

    It processes the provided css string to extract styles and maps them to HTML elements. It also supports a special .md.css pattern where elements can inherit styles based on their class names or tag names from the provided CSS.

    Key features supported by the processor:

    • GFM: Tables, task lists, and strikethrough.
    • Math: LaTeX formulas via KaTeX.
    • Code Highlighting: Syntax highlighting via Prism.js.
    • Custom Styling: Automatically applies styles to code spans, footnotes, and images based on the input CSS.
    • HTML Support: Allows raw HTML within Markdown via rehype-raw.
    import { markdownToHTML } from './utils/markdownToHTML';
    
    const markdown = '# Hello World\n\nThis is **bold** text.';
    const customCss = '.my-class { color: red; }';
    const options = {
      preColor: '#333',
      previewTheme: 'light'
    };
    
    const html = markdownToHTML(markdown, customCss, options);