From a3442155a50ecd4f895bd8b047ddce2dd2c42acf Mon Sep 17 00:00:00 2001 From: Sana Javed Date: Mon, 6 Mar 2023 12:59:38 +0100 Subject: [PATCH] canSplit calculation and new codeExample short code --- eleventy.config.js | 50 ++++------------- source/_includes/code_example.liquid | 38 ++++++------- source/guide.liquid | 84 ++++------------------------ source/helpers/sass_helpers.ts | 57 +++++++++++++++++++ 4 files changed, 98 insertions(+), 131 deletions(-) diff --git a/eleventy.config.js b/eleventy.config.js index 0657f3d..1308dd1 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -1,5 +1,6 @@ 'use strict'; +const path = require('path'); const { EleventyRenderPlugin } = require('@11ty/eleventy'); const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight'); const formatDistanceToNow = require('date-fns/formatDistanceToNow'); @@ -9,8 +10,8 @@ const markdown = require('markdown-it'); const markdownItAttrs = require('markdown-it-attrs'); const markdownDefList = require('markdown-it-deflist'); const typogrify = require('typogr'); -const sass = require('sass'); -const { getImplStatus } = require('./source/helpers/sass_helpers.ts'); +const { Liquid } = require('liquidjs'); +const { getImplStatus, canSplit, generateCodeExample } = require('./source/helpers/sass_helpers.ts'); /** @param {import('@11ty/eleventy').UserConfig} eleventyConfig */ module.exports = (eleventyConfig) => { @@ -53,6 +54,15 @@ module.exports = (eleventyConfig) => { return ''; }); + const engine = new Liquid({ + root: path.resolve(__dirname, 'source/_includes/'), + extname: '.liquid', + }); + eleventyConfig.addLiquidShortcode('codeExample', (contents, exampleName, autogenCSS = true, syntax = null) => { + const codeExample = generateCodeExample(contents, autogenCSS) + return engine.renderFile('code_example.liquid', {code: codeExample, exampleName: exampleName}) + }) + // Paired shortcodes... eleventyConfig.addPairedLiquidShortcode('markdown', (content) => mdown.render(content), @@ -109,42 +119,6 @@ module.exports = (eleventyConfig) => { }, ); - eleventyConfig.addLiquidFilter( - 'codeExample', - (contents, autogenCSS = true, syntax = null) => { - //TODO when are values for syntax passed in? - //TODO add tests - const splitContents = contents.split('==='); - - const scssContents = splitContents[0]; - const sassContents = splitContents[1]; - const cssContents = splitContents[2]; - - const scssExamples = scssContents.split('---'); - const sassExamples = sassContents.split('---'); - - let cssExample; - if (cssContents) { - cssExample = cssContents; - } else if (!cssContents && autogenCSS) { - // TODO check first if you even have scss or sass to generate css from - // TODO what if no scss but sass? - cssExample = ''; - scssExamples.forEach((scssSnippet) => { - const generatedCSS = sass.compileString(scssSnippet); - cssExample += generatedCSS.css; - }); - } - - return { - scss: scssExamples, - css: cssExample, - sass: sassExamples, - splitLocation: '50.0%', //TODO dynamically determine - }; - }, - ); - eleventyConfig.addPlugin(EleventyRenderPlugin); eleventyConfig.addPlugin(syntaxHighlight); diff --git a/source/_includes/code_example.liquid b/source/_includes/code_example.liquid index 9ee0726..5bd5677 100644 --- a/source/_includes/code_example.liquid +++ b/source/_includes/code_example.liquid @@ -1,56 +1,52 @@ -{% assign first_tab_id = ui_id %} -{% assign second_tab_id = first_tab_id | plus: 1 %} -{% assign third_tab_id = second_tab_id | plus: 1 %} - -
+
@@ -59,9 +55,9 @@ {%- endfor -%}