notion-to-weixin

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Notion to Weixin

Notion转微信

Config (Optional)

配置(可选)

Set a default
author
in
~/.agents/config.yaml
:
yaml
notion_to_weixin:
  author: "Alice Wang"
You can also set a global default:
yaml
author: "Alice Wang"
~/.agents/config.yaml
中设置默认
author
yaml
notion_to_weixin:
  author: "Alice Wang"
你也可以设置全局默认值:
yaml
author: "Alice Wang"

Workflow (Sequential)

工作流(顺序执行)

  1. Resolve Notion page ID from the given title.
  2. Export the page to Markdown.
  3. Ensure
    author
    exists in Markdown front matter (and inject a byline if needed).
  4. Process images (upload to Weixin and replace URLs in Markdown).
  5. Prepare
    thumb_media_id
    :
    • If the Notion page has a cover, upload it as a Weixin
      thumb
      material and use that ID.
    • If no cover, use the last image material ID.
  6. Create a new Weixin draft using Markdown input (
    --format markdown
    ) and always pass
    --css-path
    .
  1. 根据给定标题解析Notion页面ID。
  2. 将页面导出为Markdown。
  3. 确保Markdown front matter中存在
    author
    (必要时插入署名行)。
  4. 处理图片(上传到微信并替换Markdown中的URL)。
  5. 准备
    thumb_media_id
    • 如果Notion页面有封面,将其作为微信
      thumb
      素材上传并使用返回的ID。
    • 如果没有封面,使用最近上传的图片素材ID。
  6. 使用Markdown输入(
    --format markdown
    )创建新的微信草稿,且始终传入
    --css-path
    参数。

Inputs (Ask the user if missing)

输入项(缺失时询问用户)

  • notion_title
    : exact Notion page title to publish.
  • author
    : optional author name (used for Markdown front matter and node-wxcli
    --author
    ). If missing, read from
    ~/.agents/config.yaml
    .
  • css_path
    : required when using
    --format markdown
    ; if missing, default to
    assets/default.css
    .
  • thumb_media_id
    : only required if cover and image fallback both fail.
  • notion_title
    :要发布的Notion页面的准确标题。
  • author
    :可选的作者名称(用于Markdown front matter和node-wxcli的
    --author
    参数)。如果缺失,从
    ~/.agents/config.yaml
    读取。
  • css_path
    :使用
    --format markdown
    时必填;如果缺失,默认使用
    assets/default.css
  • thumb_media_id
    :仅当封面和图片回退方案都失效时需要提供。

Prerequisites

前置要求

  • notion-cli
    installed and authenticated (NOTION_TOKEN or
    notion auth set
    ).
  • node-wxcli
    installed and authenticated (
    node-wxcli auth set
    or
    node-wxcli auth login
    ).
  • curl
    and
    jq
    available.
  • 已安装
    notion-cli
    并完成认证(通过NOTION_TOKEN或
    notion auth set
    )。
  • 已安装
    node-wxcli
    并完成认证(通过
    node-wxcli auth set
    node-wxcli auth login
    )。
  • 环境中存在
    curl
    jq
    工具。

Step 1: Resolve Page ID by Title

步骤1:根据标题解析页面ID

  1. Copy
    references/notion-search.json
    to a temp file and replace
    __TITLE__
    .
  2. Run
    notion search --body @query.json
    .
  3. Choose the exact-title match. If multiple matches remain, pick the most recently edited or ask the user to confirm.
  4. If no match is found, ask for a Notion page ID or URL and extract the ID.
  1. references/notion-search.json
    复制到临时文件并替换
    __TITLE__
    占位符。
  2. 执行
    notion search --body @query.json
  3. 选择标题完全匹配的结果。如果存在多个匹配项,选择最近编辑的版本或询问用户确认。
  4. 如果未找到匹配项,请求用户提供Notion页面ID或URL并从中提取ID。

Step 2: Export Page to Markdown

步骤2:将页面导出为Markdown

  • Use
    notion pages export <page_id> --assets=download -o <workdir>/page.md
    when the page contains images.
  • Use a workdir like
    /tmp/notion-to-weixin/<slug-or-timestamp>
    .
  • 当页面包含图片时,使用
    notion pages export <page_id> --assets=download -o <workdir>/page.md
    命令。
  • 使用类似
    /tmp/notion-to-weixin/<slug-or-timestamp>
    的工作目录。

Step 3: Default Author Handling

步骤3:默认作者处理

  • If
    author
    is not provided, read it from
    ~/.agents/config.yaml
    .
  • Ensure
    author
    exists in Markdown front matter (and add a byline if you want it visible in content).
  • 如果未提供
    author
    ,从
    ~/.agents/config.yaml
    读取该配置。
  • 确保Markdown front matter中存在
    author
    (如果需要在内容中显示,可添加署名行)。

Step 4: Process Images (Recommended)

步骤4:处理图片(推荐执行)

If the Notion page contains images, upload them to Weixin and replace Markdown image URLs so they remain valid for draft rendering.
  1. Find image links in
    <workdir>/page.md
    . Notion exports local images under the assets folder.
  2. For each local image file, upload it and capture the
    url
    :
bash
node-wxcli material upload --type image --file <workdir>/assets/<image-file> --json | jq -r '.url'
  1. Replace the Markdown image URL with the returned
    url
    :
markdown
![alt](<weixin-url>)
If you skip this step, images that reference local files or expiring Notion URLs may not render in Weixin.
如果Notion页面包含图片,将其上传到微信并替换Markdown中的图片URL,以确保草稿渲染时图片可正常显示。
  1. 查找
    <workdir>/page.md
    中的图片链接。Notion导出的本地图片存放在assets文件夹下。
  2. 对每个本地图片文件,上传并获取返回的
    url
bash
node-wxcli material upload --type image --file <workdir>/assets/<image-file> --json | jq -r '.url'
  1. 用返回的
    url
    替换Markdown中的图片URL:
markdown
![alt](<weixin-url>)
如果跳过此步骤,引用本地文件或临时Notion URL的图片可能无法在微信中渲染。

Step 5: Prepare
thumb_media_id

步骤5:准备
thumb_media_id

5.1 If Notion page has a cover

5.1 如果Notion页面有封面

  1. Fetch page metadata (use notion-cli):
bash
notion pages get <page_id> > <workdir>/page.json
  1. Extract cover type + cover URL:
bash
cover_type=$(jq -r '.cover.type // ""' <workdir>/page.json)
cover_url=$(jq -r '.cover | if .==null then "" elif .type=="external" then .external.url else .file.url end' <workdir>/page.json)
  1. If cover type is
    file
    , download via notion-cli; if
    external
    , use curl:
bash
if [ "$cover_type" = "file" ]; then
  jq -c '.cover' <workdir>/page.json | notion files read --body @- --output <workdir>/cover.jpg
else
  curl -L "$cover_url" -o <workdir>/cover.jpg
fi
thumb_media_id=$(node-wxcli material upload --type thumb --file <workdir>/cover.jpg --json | jq -r '.media_id')
Note: Notion file URLs expire quickly. If download fails, re-fetch page metadata and retry.
  1. 获取页面元数据(使用notion-cli):
bash
notion pages get <page_id> > <workdir>/page.json
  1. 提取封面类型+封面URL:
bash
cover_type=$(jq -r '.cover.type // ""' <workdir>/page.json)
cover_url=$(jq -r '.cover | if .==null then "" elif .type=="external" then .external.url else .file.url end' <workdir>/page.json)
  1. 如果封面类型是
    file
    ,通过notion-cli下载;如果是
    external
    ,使用curl下载:
bash
if [ "$cover_type" = "file" ]; then
  jq -c '.cover' <workdir>/page.json | notion files read --body @- --output <workdir>/cover.jpg
else
  curl -L "$cover_url" -o <workdir>/cover.jpg
fi
thumb_media_id=$(node-wxcli material upload --type thumb --file <workdir>/cover.jpg --json | jq -r '.media_id')
注意:Notion文件URL会快速过期。如果下载失败,重新获取页面元数据并重试。

5.2 If no cover exists

5.2 如果没有封面

  1. Get image material count:
bash
image_count=$(node-wxcli material count --json | jq -r '.image_count')
  1. If
    image_count > 0
    , fetch the last image and use its
    media_id
    :
bash
offset=$((image_count - 1))
thumb_media_id=$(node-wxcli material list --type image --offset "$offset" --count 1 --json | jq -r '.item[0].media_id')
  1. If no images exist, ask the user to provide a
    thumb_media_id
    or upload a thumb manually.
  1. 获取图片素材数量:
bash
image_count=$(node-wxcli material count --json | jq -r '.image_count')
  1. 如果
    image_count > 0
    ,获取最近上传的图片并使用其
    media_id
bash
offset=$((image_count - 1))
thumb_media_id=$(node-wxcli material list --type image --offset "$offset" --count 1 --json | jq -r '.item[0].media_id')
  1. 如果没有任何图片,请求用户提供
    thumb_media_id
    或手动上传封面。

Step 6: Create Weixin Draft (Markdown input)

步骤6:创建微信草稿(Markdown输入)

When using Markdown format, always pass
--css-path
. If
css_path
is missing, set:
bash
css_path=${css_path:-assets/default.css}
bash
node-wxcli draft add \
  --title "<notion_title>" \
  --author "<author>" \
  --format markdown \
  --content - \
  --css-path <css_path> \
  --need-open-comment=1 \
  --only-fans-can-comment=0 \
  --thumb-media-id "$thumb_media_id" < <workdir>/page.md
  • If you need machine-readable output, add
    --json
    and capture the returned
    media_id
    .
使用Markdown格式时,始终传入
--css-path
参数。如果
css_path
缺失,设置:
bash
css_path=${css_path:-assets/default.css}
bash
node-wxcli draft add \
  --title "<notion_title>" \
  --author "<author>" \
  --format markdown \
  --content - \
  --css-path <css_path> \
  --need-open-comment=1 \
  --only-fans-can-comment=0 \
  --thumb-media-id "$thumb_media_id" < <workdir>/page.md
  • 如果需要机器可读的输出,添加
    --json
    参数并捕获返回的
    media_id

Resources

资源

  • references/notion-search.json
    : JSON template for Notion title search.
  • references/cli-commands.md
    : Canonical CLI command examples for
    notion-cli
    and
    node-wxcli
    .
  • assets/default.css
    : Default CSS theme for
    --format markdown
    (
    --css-path
    ).
Notes:
  • Use
    --page-id
    if the title search is ambiguous.
  • Use
    --thumb-media-id
    only if cover and image fallback are unavailable.
  • references/notion-search.json
    :用于Notion标题搜索的JSON模板。
  • references/cli-commands.md
    notion-cli
    node-wxcli
    的标准CLI命令示例。
  • assets/default.css
    --format markdown
    模式下使用的默认CSS主题(
    --css-path
    参数)。
注意:
  • 如果标题搜索结果不明确,使用
    --page-id
    参数。
  • 仅当封面和图片回退方案都不可用时,才使用
    --thumb-media-id
    参数。