From 0f05a6abeb905c42d5a4b316da5e74bf224f90f4 Mon Sep 17 00:00:00 2001 From: Robert McGovern Date: Sat, 4 Feb 2023 17:33:25 +0000 Subject: [PATCH] Added option for lexer overrides As Chroma doesn't support liquid or njk as languages, I have added an lexerOverride option so you can specify a different lexer to use instead of the language specified. --- demo/eleventy-config.js | 8 +- demo/file-with-liquid-tags.md | 173 ++++++++++++++++++++++++++++++++++ src/parseSyntaxArguments.js | 10 +- 3 files changed, 186 insertions(+), 5 deletions(-) create mode 100644 demo/file-with-liquid-tags.md diff --git a/demo/eleventy-config.js b/demo/eleventy-config.js index c6f37fc..2b6eb8f 100644 --- a/demo/eleventy-config.js +++ b/demo/eleventy-config.js @@ -1,9 +1,13 @@ const syntaxHighlight = require("../.eleventy.js"); -module.exports = function(eleventyConfig) { +module.exports = function (eleventyConfig) { eleventyConfig.addPlugin(syntaxHighlight, { // alwaysWrapLineHighlights: true - preAttributes: { tabindex: 0 } + lexerOverrides: { + njk: "vue", + liquid: "swift", + }, + preAttributes: { tabindex: 0 }, }); eleventyConfig.setTemplateFormats("njk,liquid,md,css"); diff --git a/demo/file-with-liquid-tags.md b/demo/file-with-liquid-tags.md new file mode 100644 index 0000000..9327bbd --- /dev/null +++ b/demo/file-with-liquid-tags.md @@ -0,0 +1,173 @@ +JSON + +```json +"services": [ + { + "name": "Frontend Mentor", + "svgSource": "/svgs/frontendmentor.svg", + "svgAltText": "Frontend Mentor Logo", + "description": "Collection of challenges I completed for Frontend Mentor", + "cssClass": "", + "difficulty": [ + { + "title": "Junior", + "cssClass": "frontEndMentorChallenges", + "challenges": [ + { + "title": "Advice Generator App", + "url": "/FrontendMentor/junior/advice-generator-app/", + "description": "", + "techUsed": [ + "html5", + "css3", + "js" + ], + "screenshotURL": "/FrontendMentor/junior/advice-generator-app/screenshots/mobile.png", + "screenshotAltText": "Advice Generator App" + } + ] + }, +``` +RAW Liquid + +{% raw %} +```liquid +{% assign navigation = site.data.navigation[include.nav] %} + + +``` +{% endraw %} + +RAW - NJK + +{% raw %} +```njk +{# {% set projectPrefix = project %} #} + +{% if projectContent %} +
+ + {{projectContent.screenshotAltText}} + +
+

+ {{projectContent.title}} +

+ {% if projectContent.description | length %} +

+ {{projectContent.description}} +

+ {% endif %} +
    + {% for tech in projectContent.techUsed %} + {% if tech === "html5" %} +
  • + HTML5 +
  • + {% endif %} + {% if tech === "css3" %} +
  • + CSS3 +
  • + {% endif %} + {% if tech === "js" %} +
  • + JavaScript +
  • + {% endif %} + {% endfor %} +
+
+
+{% endif %} +``` +{% endraw %} + +The finished build is then copied and published. + +None + +```text +name: Build Eleventy + +on: + push: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [17.x] + + steps: + - uses: actions/checkout@v2 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + + - name: Install dependencies & build + run: | + npm ci + npm run build + + - name: Deploy + uses: peaceiris/actions-gh-pages@v3.7.3 + with: + publish_dir: ./www + github_token: ${{ secrets.GITHUB_TOKEN }} +``` + +NJK + + +{% raw %} + +```njk + + + + + {{ title }} - {{ site.url }} + + {% include "global/site-meta.njk" %} + {% include "global/site-css.njk" %} + {% include "global/site-favicon.njk" %} + + + {% include "global/site-header.njk" %} + {% include "global/site-main.njk" %} + {% include "global/site-footer.njk" %} + + +``` +{% endraw %} diff --git a/src/parseSyntaxArguments.js b/src/parseSyntaxArguments.js index 87c1813..68c462e 100644 --- a/src/parseSyntaxArguments.js +++ b/src/parseSyntaxArguments.js @@ -63,10 +63,14 @@ function parseSyntaxArguments(args, context = {}) { let opts = ""; - opts += `--lexer ${splitArgs[0]} `; + // Remove the lang from the arguments + let lang = splitArgs.shift(); - // Remove the language which should be the first arg - splitArgs.shift(); + if (context.lexerOverrides[lang]) { + lang = context.lexerOverrides[lang]; + } + + opts += `--lexer ${lang} `; if (Array.isArray(splitArgs)) { splitArgs.forEach((arg) => {