fix-markdown-format-of-notion-page

修复 Notion Page Content 复制为 Markdown 后的格式错乱问题。

  1. // ==UserScript==
  2. // @name fix-markdown-format-of-notion-page
  3. // @name:en Fix markdown format of notion page after copy action
  4. // @name:zh-CN 修复 Notion Page Content 复制为 Markdown 后的格式错乱问题
  5. // @description 修复 Notion Page Content 复制为 Markdown 后的格式错乱问题。
  6. // @description:zh-CN 修复 Notion Page Content 复制为 Markdown 后的格式错乱问题。
  7. // @description:en Fix markdown format of notion page after copy action.
  8. // @namespace https://github.com/Seven-Steven/tampermonkey-scripts/tree/main/fix-markdown-format-of-notion-page
  9. // @version 0.1
  10. // @license MIT
  11. // @author Seven
  12. // @match *://www.notion.so/*
  13. // @icon https://www.google.com/s2/favicons?sz=64&domain=notion.so
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. 'use strict';
  18.  
  19. init();
  20.  
  21. /**
  22. * 初始化动作
  23. */
  24. function init() {
  25. window.addEventListener('copy', fixNotionMarkdownInClipboard);
  26. }
  27.  
  28. /**
  29. * 修正剪切板中 markdown 的格式
  30. */
  31. function fixNotionMarkdownInClipboard(event) {
  32. navigator.clipboard.readText().then(text => {
  33. const markdown = fixMarkdownFormat(text);
  34. navigator.clipboard.writeText(markdown).then(() => {
  35. }, () => {
  36. console.log('failed.');
  37. })
  38. })
  39. }
  40.  
  41. /**
  42. * 修正 markdown 格式
  43. */
  44. function fixMarkdownFormat(markdown) {
  45. if (!markdown) {
  46. return;
  47. }
  48.  
  49. // 给没有 Caption 的图片添加默认 ALT 文字
  50. markdown = markdown.replaceAll(/\!(http.*\.\w+)/g, (match, group1) => {
  51. const processedText = decodeURIComponent(group1);
  52. console.log('regex', processedText);
  53. return `![picture](${processedText})`;
  54. });
  55. // 给有 Caption 的图片去除多余文字
  56. const captionRegex = /(\!\[(?<title>.+?)\]\(.*?\)\s*)\k<title>\s*/g;
  57. return markdown.replaceAll(captionRegex, '$1');
  58. }
  59. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址