{"id":205305,"date":"2025-05-29T16:49:58","date_gmt":"2025-05-29T08:49:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205305\/"},"modified":"2025-05-29T16:49:58","modified_gmt":"2025-05-29T08:49:58","slug":"python%e5%a6%82%e4%bd%95%e6%89%b9%e9%87%8f%e4%bf%ae%e6%94%b9markdown%e6%96%87%e6%a1%a3%e4%b8%ad%e5%9b%be%e7%89%87%e7%9a%84%e5%9c%b0%e5%9d%80%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205305\/","title":{"rendered":"Python\u5982\u4f55\u6279\u91cf\u4fee\u6539Markdown\u6587\u6863\u4e2d\u56fe\u7247\u7684\u5730\u5740\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u5982\u4f55\u6279\u91cf\u4fee\u6539Markdown\u6587\u6863\u4e2d\u56fe\u7247\u7684\u5730\u5740\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/17327857116748362f55cb0.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 python \u6279\u91cf\u66f4\u6539 markdown \u6587\u6863\u4e2d\u7684\u56fe\u7247\u5730\u5740<\/strong><\/p>\n<p>\u5728 markdown \u6587\u6863\u4e2d\uff0c\u56fe\u7247\u901a\u5e38\u901a\u8fc7\u4ee5\u4e0b\u8bed\u6cd5\u5f15\u7528\uff1a<\/p>\n<pre>![\u56fe\u7247\u63cf\u8ff0](\u56fe\u7247\u8def\u5f84)<\/pre>\n<p>\u5176\u4e2d\uff0c\u56fe\u7247\u63cf\u8ff0\u548c\u56fe\u7247\u8def\u5f84\u7684\u5185\u5bb9\u53ef\u80fd\u5305\u542b\u6c49\u5b57\u3002<\/p>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0<\/strong><\/p>\n<p>\u4e3a\u4e86\u5904\u7406\u591a\u4e2a markdown \u6587\u6863\u4e2d\u7684\u56fe\u7247\u5730\u5740\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 python \u548c\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<pre>import re\n\ndef process_markdown_content(content):\n    # \u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u5339\u914d markdown \u4e2d\u7684\u56fe\u7247\u94fe\u63a5\n    img_pattern = re.compile(r'!\\[([^\\]]*)\\]\\((\\.\/[^)]+)\\)')\n\n    # \u5b9a\u4e49\u4e00\u4e2a\u66ff\u6362\u51fd\u6570\uff0c\u5c06\u65e7\u56fe\u7247\u8def\u5f84\u66ff\u6362\u4e3a\u65b0\u7684\u8def\u5f84\n    def replacer(match):\n        description = match.group(1)  # \u83b7\u53d6\u56fe\u7247\u63cf\u8ff0\n        old_path = match.group(2)  # \u83b7\u53d6\u65e7\u56fe\u7247\u8def\u5f84\n        # \u5c06\u65e7\u8def\u5f84\u66ff\u6362\u4e3a\u65b0\u8def\u5f84\n        new_path = old_path.replace('.\/', '.\/new_folder\/')\n        return f'![{description}]({new_path})'  # \u66ff\u6362\u4e3a\u65b0\u94fe\u63a5\n\n    # \u4f7f\u7528\u66ff\u6362\u51fd\u6570\u66ff\u6362\u5339\u914d\u5230\u7684\u6240\u6709\u94fe\u63a5\n    new_content = img_pattern.sub(replacer, content)\n    return new_content\n\n# \u793a\u4f8b\nmarkdown_content = '''\nhere is some markdown content.\n![desc1](.\/images\/pic1.jpg)\n![desc2](.\/path\/to\/pic2.png)\n'''\n\nnew_content = process_markdown_content(markdown_content)\nprint(new_content)<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>Here is some markdown content.\n![desc1](.\/new_folder\/images\/pic1.jpg)\n![desc2](.\/new_folder\/path\/to\/pic2.png)<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPython\u5982\u4f55\u6279\u91cf\u4fee\u6539Markdown\u6587\u6863\u4e2d\u56fe\u7247\u7684\u5730\u5740\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u5982\u4f55\u6279\u91cf\u4fee\u6539Mark&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205305","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205305"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205305\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}