diff --git a/README.md b/README.md index 2353403..64e63d3 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,8 @@ This extension contributes the following settings: * `wordpress-post.typeAttachedImageSlug` : Processing rules for attached image file names. * `wordpress-post.mediaTypes` : File extensions and media types to enable. * `wordpress-post.useLinkableImage` : Add a tag to img tag. +* `wordpress-post.codeBlockLanguagePrefix` : Prefix of class name for code block +* `wordpress-post.codeBlockTag` : HTML tag which specifies language class * `wordpress-post.debug` : Debug of this extension. My setting.json is: diff --git a/package.json b/package.json index 8bb19d6..975dacb 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,24 @@ "default": false, "description": "Add a tag to img tag" }, + "wordpress-post.codeBlockLanguagePrefix": { + "type": "string", + "default": "language-", + "description": "Prefix of class name for code block" + }, + "wordpress-post.codeBlockTag": { + "type": "string", + "default": "code", + "description" : "HTML tag which specifies language class", + "enum" : [ + "code", + "pre" + ], + "enumDescriptions": [ + "Add class for language to tag", + "Add class for language to
 tag"
+          ]
+        },
         "wordpress-post.debug": {
           "type": "boolean",
           "default": false,
diff --git a/src/context.ts b/src/context.ts
index 6e0849f..dd117dc 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -112,6 +112,22 @@ export class Context {
     return this.getConf("useLinkableImage");
   }
 
+  /**
+   * Code Block
+   */
+  getCodeBlockStartTag(lang: string) : string {
+    const prefix:string = this.getConf("codeBlockLanguagePrefix");
+    const tag:string = this.getConf("codeBlockTag");
+    if ( tag === "pre" ) {
+      return "
";
+    } else {
+      return "
";
+    }
+  }
+  getCodeBlockEndTag() : string {
+    return "
"; + } + getConf(id: string): any { return vscode.workspace.getConfiguration(this.prefixOfSettings).get(id); } diff --git a/src/post.ts b/src/post.ts index bea7df2..278a6b2 100644 --- a/src/post.ts +++ b/src/post.ts @@ -2,7 +2,6 @@ import * as vscode from "vscode"; import * as path from "path"; import * as fs from "fs"; import axios from "axios"; -import MarkdownIt = require("markdown-it"); import * as matter from "gray-matter"; import * as cheerio from "cheerio"; import { Context } from "./context"; @@ -71,7 +70,12 @@ export const post = async (context: Context) => { // markdown -> post data content context.debug(`[06S] convert to html`); - postData["content"] = MarkdownIt().render(markdown.content); + const md = require('markdown-it')({ + highlight: function (str: string, lang: string) { + return context.getCodeBlockStartTag(lang) + md.utils.escapeHtml(str) + context.getCodeBlockEndTag(); + } + }); + postData["content"] = md.render(markdown.content); context.debug(`[06E] converted to html`); // upload attached image file, change src