From fe2ade11c702c2e50e3971d5bfab6a2b72f3bb2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=B6ltje?= Date: Sat, 24 Mar 2018 11:14:25 -0400 Subject: [PATCH] remark-prismjs: add aliases for languages This allows specifying aliases for languages when using prismjs. Fixes #4549 --- packages/gatsby-remark-prismjs/README.md | 5 + .../src/__tests__/__snapshots__/index.js.snap | 99 +++++++++++++++++++ .../src/__tests__/index.js | 17 ++++ packages/gatsby-remark-prismjs/src/index.js | 12 ++- 4 files changed, 129 insertions(+), 4 deletions(-) diff --git a/packages/gatsby-remark-prismjs/README.md b/packages/gatsby-remark-prismjs/README.md index b514d9c4afe17..39cd77849baf6 100644 --- a/packages/gatsby-remark-prismjs/README.md +++ b/packages/gatsby-remark-prismjs/README.md @@ -34,6 +34,11 @@ plugins: [ // A suggested value for English speakers is the non-ascii // character '›'. inlineCodeMarker: null, + // This lets you set up language aliases. For example, + // setting this to '{ sh: "bash" }' will let you use + // the language "sh" which will highlight using the + // bash highlighter. + aliases: {}, }, }, ], diff --git a/packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/index.js.snap index ab43ad641c017..23fbb7bf76515 100644 --- a/packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/index.js.snap @@ -44,6 +44,49 @@ Object { } `; +exports[`remark prism plugin generates a
 tag with aliases applied 1`] = `
+Object {
+  "children": Array [
+    Object {
+      "lang": "foobar",
+      "position": Position {
+        "end": Object {
+          "column": 4,
+          "line": 3,
+          "offset": 21,
+        },
+        "indent": Array [
+          1,
+          1,
+        ],
+        "start": Object {
+          "column": 1,
+          "line": 1,
+          "offset": 0,
+        },
+      },
+      "type": "html",
+      "value": "
+
// Fake
+
", + }, + ], + "position": Object { + "end": Object { + "column": 4, + "line": 3, + "offset": 21, + }, + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "root", +} +`; + exports[`remark prism plugin generates a
 tag with class="language-*" prefix by default 1`] = `
 Object {
   "children": Array [
@@ -199,6 +242,62 @@ Object {
 }
 `;
 
+exports[`remark prism plugin generates an inline  tag with aliases applied 1`] = `
+Object {
+  "children": Array [
+    Object {
+      "children": Array [
+        Object {
+          "position": Position {
+            "end": Object {
+              "column": 16,
+              "line": 1,
+              "offset": 15,
+            },
+            "indent": Array [],
+            "start": Object {
+              "column": 1,
+              "line": 1,
+              "offset": 0,
+            },
+          },
+          "type": "html",
+          "value": "Fake
+",
+        },
+      ],
+      "position": Position {
+        "end": Object {
+          "column": 16,
+          "line": 1,
+          "offset": 15,
+        },
+        "indent": Array [],
+        "start": Object {
+          "column": 1,
+          "line": 1,
+          "offset": 0,
+        },
+      },
+      "type": "paragraph",
+    },
+  ],
+  "position": Object {
+    "end": Object {
+      "column": 16,
+      "line": 1,
+      "offset": 15,
+    },
+    "start": Object {
+      "column": 1,
+      "line": 1,
+      "offset": 0,
+    },
+  },
+  "type": "root",
+}
+`;
+
 exports[`remark prism plugin generates an inline  tag with class="language-*" prefix by default 1`] = `
 Object {
   "children": Array [
diff --git a/packages/gatsby-remark-prismjs/src/__tests__/index.js b/packages/gatsby-remark-prismjs/src/__tests__/index.js
index c9d8a3693fd22..271aed83b77e4 100644
--- a/packages/gatsby-remark-prismjs/src/__tests__/index.js
+++ b/packages/gatsby-remark-prismjs/src/__tests__/index.js
@@ -16,6 +16,13 @@ describe(`remark prism plugin`, () => {
       plugin({ markdownAST }, { classPrefix: `custom-` })
       expect(markdownAST).toMatchSnapshot()
     })
+
+    it(`with aliases applied`, () => {
+      const code = `\`\`\`foobar\n// Fake\n\`\`\``
+      const markdownAST = remark.parse(code)
+      plugin({ markdownAST }, { aliases: { foobar: `javascript` } })
+      expect(markdownAST).toMatchSnapshot()
+    })
   })
 
   describe(`generates an inline  tag`, () => {
@@ -39,5 +46,15 @@ describe(`remark prism plugin`, () => {
       plugin({ markdownAST }, { inlineCodeMarker: `🍺  ` })
       expect(markdownAST).toMatchSnapshot()
     })
+
+    it(`with aliases applied`, () => {
+      const code = `\`foobar : Fake\``
+      const markdownAST = remark.parse(code)
+      plugin(
+        { markdownAST },
+        { inlineCodeMarker: ` : `, aliases: { foobar: `javascript` } }
+      )
+      expect(markdownAST).toMatchSnapshot()
+    })
   })
 })
diff --git a/packages/gatsby-remark-prismjs/src/index.js b/packages/gatsby-remark-prismjs/src/index.js
index 31181f336a797..c02aa7ad1f1d4 100644
--- a/packages/gatsby-remark-prismjs/src/index.js
+++ b/packages/gatsby-remark-prismjs/src/index.js
@@ -5,8 +5,13 @@ const highlightCode = require(`./highlight-code`)
 
 module.exports = (
   { markdownAST },
-  { classPrefix = `language-`, inlineCodeMarker = null } = {}
+  { classPrefix = `language-`, inlineCodeMarker = null, aliases = {} } = {}
 ) => {
+  const normalizeLanguage = lang => {
+    const lower = lang.toLowerCase()
+    return aliases[lower] || lower
+  }
+
   visit(markdownAST, `code`, node => {
     let language = node.lang
     let { splitLanguage, highlightLines } = parseLineNumberRange(language)
@@ -20,8 +25,7 @@ module.exports = (
     // @see https://github.com/PrismJS/prism/blob/1d5047df37aacc900f8270b1c6215028f6988eb1/themes/prism.css#L49-L54
     let languageName = `text`
     if (language) {
-      language = language.toLowerCase()
-      languageName = language
+      languageName = normalizeLanguage(language)
     }
 
     // Allow users to specify a custom class prefix to avoid breaking
@@ -49,7 +53,7 @@ module.exports = (
     if (inlineCodeMarker) {
       let [language, restOfValue] = node.value.split(`${inlineCodeMarker}`, 2)
       if (language && restOfValue) {
-        languageName = language.toLowerCase()
+        languageName = normalizeLanguage(language)
         node.value = restOfValue
       }
     }