{"id":383,"date":"2025-06-09T13:54:08","date_gmt":"2025-06-09T05:54:08","guid":{"rendered":"https:\/\/www.hyw.life\/?p=383"},"modified":"2025-10-21T17:07:02","modified_gmt":"2025-10-21T09:07:02","slug":"uniapp%e8%bd%acmarkdown","status":"publish","type":"post","link":"https:\/\/www.hyw.life\/?p=383","title":{"rendered":"uniapp\u8f6cmarkdown"},"content":{"rendered":"\n<p>\u5728UNIAPP\u9879\u76ee\u4e2d\uff0c\u8981\u5c06\u6587\u672c\u5185\u5bb9\u4ece\u666e\u901a\u7684\u6587\u672c\u683c\u5f0f\u8f6c\u6362\u6210Markdown\u683c\u5f0f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<span id=\"h3-0\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h3 class=\"wp-block-heading\">1. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n\n\n\n<p>\u5728UNIAPP\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9bJavaScript\u5e93\u6765\u5e2e\u52a9\u4f60\u8f6c\u6362\u6587\u672c\u4e3aMarkdown\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>markdown-it<\/code>\u8fd9\u4e2a\u5e93\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5b89\u88c5\u8fd9\u4e2a\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install markdown-it<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u7ec4\u4ef6\u4e2d\u8fd9\u6837\u4f7f\u7528\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import MarkdownIt from 'markdown-it';\nconst md = new MarkdownIt();\n\nexport default {\n   data() {\n     return {\n         text: '\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\u3002'\n     };\n  },\n  computed: {\n     markdownText() {\n         return md.render(this.text);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u6a21\u677f\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u663e\u793a\u8f6c\u6362\u540e\u7684Markdown\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n&lt;view v-html=\"markdownText\">&lt;\/view><\/code><\/pre>\n\n\n\n<span id=\"h3-1\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h3 class=\"wp-block-heading\">2. \u4f7f\u7528\u5728\u7ebfAPI\u670d\u52a1<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u5728\u5ba2\u6237\u7aef\u5904\u7406Markdown\u8f6c\u6362\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5728\u7ebf\u7684API\u670d\u52a1\uff0c\u4f8b\u5982<code>markdownify<\/code>\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001HTTP\u8bf7\u6c42\u5230\u8fd9\u6837\u7684\u670d\u52a1\uff0c\u5c06\u4f60\u7684\u6587\u672c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u7136\u540e\u83b7\u53d6\u8f6c\u6362\u540e\u7684Markdown\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>uni.request<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default {\n     data() {\n        return {\n              text: '\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\u3002',\n              markdownText: ''\n        };\n     },\n     methods: {\n           convertToMarkdown() {\n                   uni.request({\n                       url: 'https:\/\/api.someonlineservice.com\/markdown', \/\/ \u66ff\u6362\u4e3a\u5b9e\u9645\u7684API URL\n                       method: 'POST',\n                       data: {\n                          text: this.text\n                       },\n                       success: (res) => {\n                          this.markdownText = res.data; \/\/ \u5047\u8bbe\u8fd4\u56de\u7684\u6570\u636e\u76f4\u63a5\u662fMarkdown\u683c\u5f0f\u7684\u6587\u672c\n                       },\n                       fail: (err) => {\n                           console.error(err);\n                       }\n                  });\n           }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u6a21\u677f\u4e2d\u8c03\u7528<code>convertToMarkdown<\/code>\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>\u8f6c\u6362\u4e3aMarkdown<br>{{ markdownText }}<\/p>\n\n\n\n<span id=\"h3-2\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h3 class=\"wp-block-heading\">3. \u624b\u52a8\u8f6c\u6362\uff08\u4e0d\u63a8\u8350\uff09<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u7b80\u5355\u7684\u8f6c\u6362\u9700\u6c42\uff0c\u4f60\u4e5f\u53ef\u4ee5\u624b\u52a8\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u6765\u5c06\u666e\u901a\u6587\u672c\u8f6c\u6362\u4e3aMarkdown\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5c06\u52a0\u7c97\u548c\u659c\u4f53\u7b49\u8f6c\u6362\u4e3aMarkdown\u7684\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default {\n    data() {\n       return {\n            text: '\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\u3002',\n            markdownText: ''\n      };\n    },\n    computed: {\n       manualMarkdown() {\n           return this.text.replace(\/**(.?)**\/g, '$1') \/\/ \u52a0\u7c97\u66ff\u6362\u4e3a\u666e\u901a\u6587\u672c\uff08\u793a\u4f8b\uff09 \n                  .replace(\/*(.?)*\/g, '$1'); \/\/ \u659c\u4f53\u66ff\u6362\u4e3a\u659c\u4f53\u8bed\u6cd5\uff08\u793a\u4f8b\uff09\n      }\n   }\n}\n\n{{ manualMarkdown }}<\/code><\/pre>\n\n\n\n<span id=\"h3-3\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h3 class=\"wp-block-heading\">\u7ed3\u8bba\uff1a<\/h3>\n\n\n\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u3002\u5982\u679c\u4f60\u9700\u8981\u9891\u7e41\u5730\u8fdb\u884c\u8f6c\u6362\u4e14\u683c\u5f0f\u590d\u6742\uff0c\u4f7f\u7528<code>markdown-it<\/code>\u6216\u7c7b\u4f3c\u7684\u5e93\u53ef\u80fd\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u4f60\u53ea\u662f\u5076\u5c14\u9700\u8981\u8f6c\u6362\u6216\u8005\u5e0c\u671b\u51cf\u5c11\u5ba2\u6237\u7aef\u4ee3\u7801\u7684\u590d\u6742\u5ea6\uff0c\u4f7f\u7528\u5728\u7ebfAPI\u670d\u52a1\u53ef\u80fd\u66f4\u65b9\u4fbf\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u8f6c\u6362\u9700\u6c42\uff0c\u624b\u52a8\u7f16\u5199\u8f6c\u6362\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u53ef\u884c\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728UNIAPP\u9879\u76ee\u4e2d\uff0c\u8981\u5c06\u6587\u672c\u5185\u5bb9\u4ece\u666e\u901a\u7684\u6587\u672c\u683c\u5f0f\u8f6c\u6362\u6210Markdown\u683c\u5f0f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[31,15],"class_list":["post-383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uniapp","tag-markdown","tag-uniapp"],"_links":{"self":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=383"}],"version-history":[{"count":6,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions\/390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/media\/484"}],"wp:attachment":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}