{"id":225753,"date":"2021-09-19T08:31:39","date_gmt":"2021-09-19T00:31:39","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=225753"},"modified":"2021-09-02T10:35:04","modified_gmt":"2021-09-02T02:35:04","slug":"vscode-markdown","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/vscode-markdown.html","title":{"rendered":"\u6559\u4f60\u5b9e\u73b0Vscode\u7684Markdown\u9884\u89c8"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nvscode \u7684 markdown \u9884\u89c8\u662f\u6211\u4eec\u6574\u5929\u90fd\u5728\u7528\u7684\u529f\u80fd\uff0c\u6709\u6ca1\u6709\u60f3\u8fc7\u5b83\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3002\u6216\u8bb8\u6709\u4e00\u5929\u4f60\u4f1a\u63a5\u5230\u4e2a\u5b9a\u5236 markdown \u9884\u89c8\u7684\u9700\u6c42\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u5462?<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

vscode \u7684 markdown \u9884\u89c8\u662f\u6211\u4eec\u6574\u5929\u90fd\u5728\u7528\u7684\u529f\u80fd\uff0c\u6709\u6ca1\u6709\u60f3\u8fc7\u5b83\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3002\u6216\u8bb8\u6709\u4e00\u5929\u4f60\u4f1a\u63a5\u5230\u4e2a\u5b9a\u5236 markdown \u9884\u89c8\u7684\u9700\u6c42\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u5462?
\n\"\"
\n\u6709\u4ec0\u4e48\u601d\u8def\u4e48?\u601d\u8003\u4e94\u79d2\u949f\u3002
\n5
\n4
\n3
\n2
\n1<\/p>\n

\u5176\u5b9e\u6574\u4f53\u601d\u8def\u6bd4\u8f83\u7b80\u5355\uff0c\u5c31\u662f\u521b\u5efa\u4e00\u4e2a webview panel\uff0c\u8bbe\u7f6e\u5185\u5bb9\u4e3a markdown \u751f\u6210\u7684 html\uff0c\u4e4b\u540e\u5728 markdown \u66f4\u65b0\u7684\u65f6\u5019\u540c\u6b65\u4fee\u6539 webview \u7684 html \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n

\u601d\u8def\u5206\u6790<\/strong><\/div>\n

\u901a\u8fc7 vscode.window.createWebviewPanel \u521b\u5efa\u4e00\u4e2a webview\uff0c\u6307\u5b9a\u5728\u4fa7\u8fb9\u6253\u5f00\uff0c\u4e4b\u540e\u901a\u8fc7\u8be5 panel \u5bf9\u8c61\u7684 webview.html \u5c5e\u6027\u6765\u8bbe\u7f6e html\u3002<\/p>\n

html \u662f\u901a\u8fc7\u7f16\u8f91\u5668\u7684 markdown \u5185\u5bb9\u751f\u6210\u7684\uff0c \u7f16\u8f91\u5668\u5185\u5bb9\u901a\u8fc7 editor.document.getText() \u62ff\u5230\uff0c\u7136\u540e\u8c03\u7528\u7b2c\u4e09\u65b9\u7684 markdown \u8f6c html \u7684\u5e93\u6765\u751f\u6210\u3002<\/p>\n

\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86 markdown \u7684\u9884\u89c8\u3002<\/p>\n

\u9884\u89c8\u4e4b\u540e\u9700\u8981\u66f4\u65b0\uff0c\u76d1\u542c vscode.workspace.onDidSaveTextDocument \u548c vscode.workspace.onDidChangeTextDocument \u7684\u4e8b\u4ef6\uff0c\u5728\u6587\u6863\u66f4\u65b0\u548c\u4fdd\u5b58\u7684\u65f6\u5019\uff0c\u62ff\u5230\u7f16\u8f91\u5668\u7684\u5185\u5bb9\uff0c\u91cd\u65b0\u751f\u6210 html\uff0c\u7136\u540e\u8bbe\u7f6e\u5230 webview\u3002<\/p>\n

webviewPanel \u652f\u6301 webview.postMessage(message); \u7684\u65b9\u5f0f\u4f20\u9012\u6d88\u606f\uff0c\u652f\u6301 updateHTML \u7b49\u4e00\u7cfb\u5217 command\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u6d88\u606f\u6765\u89e6\u53d1\u3002<\/p>\n

\u4f46\u662f\u600e\u4e48\u77e5\u9053\u54ea\u4e2a\u6587\u6863\u66f4\u65b0\u54ea\u4e2a webview \u5462?<\/p>\n

\u53ef\u4ee5\u7ef4\u62a4\u4e00\u4e2a map\uff0c\u5728\u521b\u5efa webviewPanel \u7684\u65f6\u5019\u8bb0\u5f55\u5230 map \u4e2d\uff0ckey \u4e3a\u6587\u4ef6\u8def\u5f84\uff0c\u8fd9\u6837\u66f4\u65b0\u7684\u65f6\u5019\u5c31\u80fd\u67e5\u627e\u5230\u5bf9\u5e94\u7684 webview \u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n

\u8fd9\u6837\uff0c\u5c31\u5b8c\u6210\u4e86 markdown \u5185\u5bb9\u7684\u66f4\u65b0\u3002<\/p>\n

\u5176\u5b9e\u6574\u4f53\u601d\u8def\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u4e0b\u9762\u6211\u4eec\u6765\u5199\u4e0b\u4ee3\u7801\uff1a<\/p>\n

\u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n

\u6211\u4eec\u770b\u4e0b vscode-markdown-preview-enhanced \u7684\u63d2\u4ef6\u7684\u4ee3\u7801\uff0c\u8fd9\u4e5f\u662f\u4e00\u4e2a\u9884\u89c8 markdown \u7684\u63d2\u4ef6\uff0c\u4ee3\u7801\u8fd8\u7b97\u7b80\u6d01\uff0c\u53ef\u4ee5\u7528\u6765\u5b66\u4e60\u3002<\/p>\n

(\u4ee5\u4e0b\u4ee3\u7801\u662f\u7b80\u5316\u540e\u7684\u4ee3\u7801)<\/p>\n

\u9996\u5148\uff0c\u63d2\u4ef6\u8981\u6307\u5b9a\u89e6\u53d1\u7684\u6761\u4ef6\uff0c\u4e5f\u5c31\u662f\u5728 package.json \u91cc\u9762\u6307\u5b9a activationEvents\uff1a<\/p>\n

\r\n\"activationEvents\": [ \r\n    \"onLanguage:markdown\", \r\n    \"onCommand:markdown-preview-enhanced.openPreviewToTheSide\" \r\n], \r\n<\/pre>\n

\u8fd9\u91cc\u4e00\u4e2a\u662f\u7f16\u8f91 markdown \u5185\u5bb9\u7684\u65f6\u5019\u6fc0\u6d3b\uff0c\u4e00\u4e2a\u662f\u6267\u884c command \u7684\u65f6\u5019\u6fc0\u6d3b\u3002<\/p>\n

\u5177\u4f53\u6fc0\u6d3b\u7684\u903b\u8f91\u5728 active \u65b9\u6cd5\u91cc\uff1a<\/p>\n

\r\nexport function activate(context: vscode.ExtensionContext) { \r\n \r\n  const contentProvider = new MarkdownPreviewEnhancedView(context); \r\n \r\n  context.subscriptions.push( \r\n    vscode.commands.registerCommand( \r\n      \"markdown-preview-enhanced.openPreviewToTheSide\", \r\n      openPreviewToTheSide, \r\n    ), \r\n  ); \r\n   \r\n  function openPreviewToTheSide(uri?: vscode.Uri) { \r\n    let resource = uri; \r\n    if (!(resource instanceof vscode.Uri)) { \r\n      if (vscode.window.activeTextEditor) { \r\n        resource = vscode.window.activeTextEditor.document.uri; \r\n      } \r\n    } \r\n    contentProvider.initPreview(resource, vscode.window.activeTextEditor, { \r\n      viewColumn: vscode.ViewColumn.Two, \r\n      preserveFocus: true, \r\n    }); \r\n  } \r\n} \r\n<\/pre>\n

\u6211\u4eec\u6ce8\u518c\u4e86\u90a3\u4e2a command\uff0c\u6267\u884c command \u4f1a\u62ff\u5230\u5f53\u524d editor \u7684 url\uff0c\u7136\u540e\u8fdb\u884c markdown \u7684 preview\u3002<\/p>\n

preview \u7684\u6240\u6709\u903b\u8f91\u90fd\u96c6\u4e2d\u5b9a\u4e49\u5728\u4e86 MarkdownPreviewEnhancedView \u7684\u5b9e\u4f8b\u5bf9\u8c61\u4e2d\uff0c\u5728 command \u89e6\u53d1\u65f6\u6267\u884c initPreivew\u3002<\/p>\n

\r\npublic async initPreview( \r\n    sourceUri: vscode.Uri, \r\n    editor: vscode.TextEditor, \r\n    viewOptions: { viewColumn: vscode.ViewColumn; preserveFocus?: boolean }, \r\n) { \r\n    \/\/ \u521b\u5efa webview \r\n    let previewPanel: vscode.WebviewPanel = vscode.window.createWebviewPanel( \r\n        \"markdown-preview-enhanced\", \r\n        `Preview ${path.basename(sourceUri.fsPath)}`, \r\n        viewOptions \r\n    ); \r\n \r\n    \/\/ \u76d1\u542c webview \u7684\u6d88\u606f \r\n    previewPanel.webview.onDidReceiveMessage((message) => {}); \r\n \r\n    \/\/ \u8bb0\u5f55 webview \u5230 map \u4e2d \r\n    this.previewMaps[sourceUri.fsPath] = previewPanel; \r\n     \r\n    \/\/ \u62ff\u5230\u7f16\u8f91\u5668\u7684\u6587\u672c\uff0c\u751f\u6210 html \r\n    const text = editor.document.getText(); \r\n    engine \r\n      .generateHTMLTemplateForPreview({inputString: text}) \r\n      .then((html) => { \r\n        \/\/ \u8bbe\u7f6e html \u5230 previewPanel \r\n        previewPanel.webview.html = html; \r\n      }); \r\n} \r\n<\/pre>\n

\u5728 initWebivew \u91cc\u9762\u521b\u5efa webviewPanel\uff0c\u540c\u65f6\u628a webviewPanel \u4fdd\u5b58\u5230 map \u4e2d\uff0ckey \u4e3a\u6587\u6863\u7684\u6587\u4ef6\u8def\u5f84\u3002\u62ff\u5230\u7f16\u8f91\u5668\u6587\u672c\u6765\u751f\u6210 html\uff0c\u8bbe\u7f6e\u5230 webview.html\uff0c\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86 markdown \u7684\u9884\u89c8\u3002<\/p>\n

\u8fd9\u6761\u8def\u5f84\u8d70\u901a\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u5b9e\u73b0\u4e86 markdown \u7684\u9884\u89c8\u3002<\/p>\n

\u4f46\u662f\u53ea\u9884\u89c8\u4e00\u6b21\u4e0d\u884c\uff0c\u66f4\u65b0\u6587\u6863\u4e4b\u540e\u9700\u8981\u81ea\u52a8\u66f4\u65b0\uff0c\u6211\u4eec\u7ee7\u7eed\u5728 active \u65b9\u6cd5\u91cc\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u542c\uff1a<\/p>\n

\r\ncontext.subscriptions.push( \r\n    vscode.workspace.onDidSaveTextDocument((document) => { \r\n      if (isMarkdownFile(document)) { \r\n        contentProvider.updateMarkdown(document.uri, true); \r\n      } \r\n    }), \r\n  ); \r\n \r\n  context.subscriptions.push( \r\n    vscode.workspace.onDidChangeTextDocument((event) => { \r\n      if (isMarkdownFile(event.document)) { \r\n        contentProvider.update(event.document.uri); \r\n      } \r\n    }), \r\n  ); \r\n<\/pre>\n

\u76d1\u542c\u6587\u672c\u4fee\u6539\u548c\u4fdd\u5b58\u7684\u65f6\u5019\uff0c\u8c03\u7528 update \u65b9\u6cd5\u6765\u66f4\u65b0\u3002<\/p>\n

\r\npublic updateMarkdown(sourceUri: Uri) { \r\n \r\n    \/\/ \u4ece map \u4e2d\u6839\u636e\u6587\u4ef6\u8def\u5f84\u53d6\u51fa\u5bf9\u5e94\u7684 webviewPanel \r\n    const previewPanel = this.previewMaps[sourceUri.fsPath]; \r\n     \r\n    \/\/ \u751f\u6210\u6700\u65b0\u7684 html \u4f20\u9012\u7ed9 webview \r\n    const text = document.getText(); \r\n    engine \r\n        .parseMD(text) \r\n        .then(({ markdown, html }) => { \r\n            previewPanel.webview.postMessage({ \r\n              command: \"updateHTML\", \r\n              html \r\n            }); \r\n        } \r\n \r\n} \r\n<\/pre>\n

\u8fd9\u91cc\u662f\u901a\u8fc7 webview.postMessage \u7ed9 html \u5185\u5bb9\u4f20\u9012 updateHTML \u7684 command \u6d88\u606f\uff0c\u89e6\u53d1 html \u5185\u5bb9\u7684\u66f4\u65b0\u3002<\/p>\n

\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u5b9e\u73b0\u4e86 markdown \u7684\u540c\u6b65\u5237\u65b0\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

vscode \u91cc\u9762 markdown \u7684\u9884\u89c8\u662f\u4e00\u4e2a\u5e38\u7528\u4f46\u5b9e\u73b0\u8d77\u6765\u5e76\u4e0d\u96be\u7684\u529f\u80fd\uff0c\u6211\u4eec\u770b\u4e86\u4e0b vscode-markdown-preview-enhanced \u63d2\u4ef6\u7684\u6e90\u7801\uff0c\u7406\u6e05\u4e86\u6574\u4f53\u7684\u6d41\u7a0b\uff1a<\/p>\n

    \n
  1. \u901a\u8fc7 vscode.window.createWebviewPanel \u521b\u5efa webviewPanel \u6765\u663e\u793a html<\/li>\n
  2. html \u901a\u8fc7 editor.document.getText() \u62ff\u5230\u6587\u672c\u5185\u5bb9\u4e4b\u540e\u901a\u8fc7\u7b2c\u4e09\u65b9\u5305\u751f\u6210\uff0c\u8bbe\u7f6e\u5230 webviewPanel<\/li>\n
  3. \u76d1\u542c workspace.onDidSaveTextDocument \u548c workspace.onDidChangeTextDocument\uff0c\u6765\u62ff\u5230\u6700\u65b0\u5185\u5bb9\uff0c\u4e4b\u540e\u751f\u6210 html \u901a\u8fc7 webview.postMessage \u4f20\u9012 udpateHTML \u7684\u6d88\u606f\u6765\u66f4\u65b0\u5230 webview\u3002<\/li>\n
  4. \u8981\u6ce8\u610f\u7684\u662f\uff0c\u9700\u8981\u8bb0\u5f55\u4e00\u4e2a map \u6765\u4fdd\u5b58 uri.fsPath \u548c webviewPanel \u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u5b9e\u73b0\u6587\u672c\u5185\u5bb9\u6539\u53d8\u66f4\u65b0\u5bf9\u5e94\u7684 webview <\/li>\n<\/ol>\n

    markdown \u7684\u9884\u89c8\u662f\u4e00\u4e2a\u5e38\u89c1\u4f46\u662f\u5e76\u4e0d\u96be\u7684\u9700\u6c42\uff0c\u4e5f\u6bd4\u8f83\u9002\u5408\u5165\u95e8 vscode \u63d2\u4ef6\u7684\u5f00\u53d1\uff0c\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u5927\u5bb6\u7406\u6e05\u601d\u8def\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    vscode \u7684 markdown \u9884\u89c8\u662f\u6211\u4eec\u6574\u5929\u90fd\u5728\u7528\u7684\u529f\u80fd\uff0c\u6709\u6ca1\u6709\u60f3\u8fc7\u5b83\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3002\u6216\u8bb8\u6709\u4e00\u5929\u4f60\u4f1a\u63a5\u5230\u4e2a […]<\/p>\n","protected":false},"author":1898,"featured_media":225756,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[740],"class_list":["post-225753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-markdown"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/225753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=225753"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/225753\/revisions"}],"predecessor-version":[{"id":225757,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/225753\/revisions\/225757"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/225756"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=225753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=225753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=225753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}