From 9ec72848e649b54c8bf47f025e00026ac344105b Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 23 Jun 2023 13:39:28 -0500 Subject: [PATCH 1/7] docs: add playground links to correct and incorrect code blocks --- docs/.eleventy.js | 36 ++++++++++++++++++++++++++++++++-- docs/src/assets/scss/docs.scss | 23 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/docs/.eleventy.js b/docs/.eleventy.js index 1bd1de1af2f..73725a9d207 100644 --- a/docs/.eleventy.js +++ b/docs/.eleventy.js @@ -179,14 +179,46 @@ module.exports = function(eleventyConfig) { `.trim(); } + /** + * Encodes text in the base 64 format used in playground URL params. + * @param {string} text Text to be encoded to base 64. + * @see https://github.com/eslint/eslint.org/blob/1b2f2aabeac2955a076d61788da8a0008bca6fb6/src/playground/utils/unicode.js + * @returns {string} The base 64 encoded equivalent of the text. + */ + function encodeToBase64(text) { + /* global btoa -- It does exist, and is what the playground uses. */ + return btoa(unescape(encodeURIComponent(text))); + } + + const withPlaygroundRender = { + render(tokens, index) { + if (tokens[index].nesting !== 1) { + return ""; + } + + const state = encodeToBase64( + JSON.stringify({ + text: tokens[index + 1].content + }) + ); + + return ` + "; + /** + * Creates markdownItContainer settings for a playground-linked codeblock. + * @param {string} name Plugin name and class name to add to the code block. + * @returns {[string, object]} Plugin name and options for markdown-it. + */ + function withPlaygroundRender(name) { + return [ + name, + { + render(tokens, index) { + if (tokens[index].nesting !== 1) { + return ""; + } + + // See https://github.com/eslint/eslint.org/blob/ac38ab41f99b89a8798d374f74e2cce01171be8b/src/playground/App.js#L44 + const options = tokens[index].info?.split("correct ")[1]?.trim(); + const { content } = tokens[index + 1]; + const state = encodeToBase64( + JSON.stringify({ + ...(options && { options: JSON.parse(options) }), + text: content + }) + ); + + return ` +
+ + Open in Playground ↗️ + + `.trim(); + } } - - const state = encodeToBase64( - JSON.stringify({ - text: tokens[index + 1].content - }) - ); - - return ` -