分享

使用n8n自动同步Mastodon嘟文到博客说说

思路

从长毛象获取嘟文,然后使用n8n的webhook功能将嘟文推送到博客的github所在的仓库。 触发github的action,将备份的嘟文.md文件生成网页,然后推送到博客的gh-pages分支。

步骤

  1. 安装n8n
  2. 在mastodon设置webhook
  3. 在github中创建action

n8n流程使用蜗牛大大的https://e5n.cc/@eallion/114184519111953516 在此基础上修改部分内容。

// 获取 mix_content 和 createdAt
const ID = $('Fetch Latest Mastodon').first().json.body[0].id || '';
const createdAt = $('Fetch Latest Mastodon').first().json.body[0].created_at || '';
const URL = $('Fetch Latest Mastodon').first().json.body[0].url || '';
const mixContent = $('Mix Content').first().json.mix_content || '';

// 解析 created_at 时间戳
const date = new Date(createdAt);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1,并补零
const day = String(date.getDate()).padStart(2, '0'); // 补零
const hours = String(date.getHours() + 8).padStart(2, '0'); // 补零
const minutes = String(date.getMinutes()).padStart(2, '0'); // 补零
const seconds = String(date.getSeconds()).padStart(2, '0'); // 补零

// 格式化时间戳
const formattedTimestamp = `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;

// 将时间戳添加到 mixContent 中
const contentWithTimestamp = `---\ntitle: "${year}-${month}-${day}"\ndate: ${formattedTimestamp}\n\n--- \n${mixContent}`;

// 拼接文件路径
const githubFilePath = `content/zh-cn/memo/${ID}.md`;

// 返回结果
return {
    json: {
        github_file_name: githubFilePath,
        content: contentWithTimestamp,
        commit_message: URL
    }
};

在获取内容的时候加入 Front-matter, 然后将内容保存到 content/zh-cn/memo/${ID}.md 中。

如此便可以顺利的自动生成说说了.