Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add playground links to correct and incorrect code blocks #17306

Merged
merged 7 commits into from Jul 14, 2023
Merged
53 changes: 51 additions & 2 deletions docs/.eleventy.js
Expand Up @@ -179,14 +179,63 @@ 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)));
}

/**
* 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 "</div>";
}

// See https://github.com/eslint/eslint.org/blob/ac38ab41f99b89a8798d374f74e2cce01171be8b/src/playground/App.js#L44
const parserOptions = tokens[index].info?.split("correct ")[1]?.trim();
const { content } = tokens[index + 1];
const state = encodeToBase64(
JSON.stringify({
...(parserOptions && { options: { parserOptions: JSON.parse(parserOptions) } }),
text: content
})
);
harish-sethuraman marked this conversation as resolved.
Show resolved Hide resolved
const prefix = process.env.CONTEXT && process.env.CONTEXT !== "deploy-preview"
harish-sethuraman marked this conversation as resolved.
Show resolved Hide resolved
? ""
: "https://eslint.org";

return `
<div class="${name}">
<a class="c-btn c-btn--secondary c-btn--playground" href="${prefix}/play#${state}" target="_blank">
Open in Playground
</a>
`.trim();
}
}
];
}

const markdownIt = require("markdown-it");
const md = markdownIt({ html: true, linkify: true, typographer: true, highlight: (str, lang) => highlighter(md, str, lang) })
.use(markdownItAnchor, {
slugify: s => slug(s)
})
.use(markdownItContainer, "img-container", {})
.use(markdownItContainer, "correct", {})
.use(markdownItContainer, "incorrect", {})
.use(markdownItContainer, ...withPlaygroundRender("correct"))
.use(markdownItContainer, ...withPlaygroundRender("incorrect"))
.use(markdownItContainer, "warning", {
render(tokens, idx) {
return generateAlertMarkup("warning", tokens, idx);
Expand Down
18 changes: 0 additions & 18 deletions docs/src/assets/js/main.js
Expand Up @@ -192,24 +192,6 @@
}
})();

// add "Open in Playground" button to code blocks
// (function() {
// let blocks = document.querySelectorAll('pre[class*="language-"]');
// if (blocks) {
// blocks.forEach(function(block) {
// let button = document.createElement("a");
// button.classList.add('c-btn--playground');
// button.classList.add('c-btn');
// button.classList.add('c-btn--secondary');
// button.setAttribute("href", "#");
// button.innerText = "Open in Playground";
// block.appendChild(button);
// });
// }
// })();



// add utilities
var util = {
keyCodes: {
Expand Down
9 changes: 5 additions & 4 deletions docs/src/assets/scss/docs.scss
Expand Up @@ -142,13 +142,14 @@ pre[class*="language-"] {
.c-btn.c-btn--playground {
position: absolute;
font-size: var(--step--1);
bottom: 0.5rem;
right: 0.5rem;
bottom: -1rem;
right: 1rem;
offset-block-end: 0.5rem;
offset-inline-end: 0.5rem;
z-index: 1;

@media all and (max-width: 768px) {
display: none;
@media all and (min-width: 768px) {
bottom: 1.5rem;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be an accessibility issue. Users on less-wide screens, such as mobile phones and zoomed-in desktop displays, still would want to be able to access the Open in Playground button.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, I'll have to ask @nzakas what the original intention was.

The Playground is quite good on small screens, but this button is very likely to cover the code:

Screenshot_2023-07-08-16-45-43-843_com android chrome

Though that can happen on wider screens too, depending on the text. Perhaps the button should be below the code block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of moving the button below in mobile screens, yeah. Will wait to implement until directed to 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the intent was that smaller screens (mobile) would probably not want to use the playground as it's a bit clunky without a keyboard.

However, I'm not opposed to moving the button underneath the code block. That seems like a nice compromise to keep the functionality available without disrupting the UI too much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super, that's two votes in favor - I had a 💡 moment and moved the button to be half overlapping, half under the code block. That way it takes up much less vertical space and doesn't overlap lines of code.

}

Expand Down
22 changes: 11 additions & 11 deletions docs/src/rules/no-implicit-globals.md
Expand Up @@ -44,7 +44,7 @@ This rule disallows `var` and `function` declarations at the top-level script sc

Examples of **incorrect** code for this rule:

::: incorrect
::: incorrect { "sourceType": "script" }

```js
/*eslint no-implicit-globals: "error"*/
Expand All @@ -58,7 +58,7 @@ function bar() {}

Examples of **correct** code for this rule:

::: correct
::: correct { "sourceType": "script" }

```js
/*eslint no-implicit-globals: "error"*/
Expand All @@ -79,7 +79,7 @@ window.bar = function() {};

Examples of **correct** code for this rule with `"parserOptions": { "sourceType": "module" }` in the ESLint configuration:

::: correct
::: correct { "sourceType": "script" }

```js
/*eslint no-implicit-globals: "error"*/
Expand All @@ -100,7 +100,7 @@ This does not apply to ES modules since the module code is implicitly in `strict

Examples of **incorrect** code for this rule:

::: incorrect
::: incorrect { "sourceType": "script" }

```js
/*eslint no-implicit-globals: "error"*/
Expand All @@ -127,7 +127,7 @@ or in a `/*global */` comment.

Examples of **incorrect** code for this rule:

::: incorrect
::: incorrect { "sourceType": "script" }

```js
/*eslint no-implicit-globals: "error"*/
Expand Down Expand Up @@ -155,7 +155,7 @@ If the variable is intended to be local to the script, wrap the code with a bloc

Examples of **correct** code for this rule with `"lexicalBindings"` option set to `false` (default):

::: correct
::: correct { "sourceType": "script" }

```js
/*eslint no-implicit-globals: ["error", {"lexicalBindings": false}]*/
Expand All @@ -171,7 +171,7 @@ class Bar {}

Examples of **incorrect** code for this rule with `"lexicalBindings"` option set to `true`:

::: incorrect
::: incorrect { "sourceType": "script" }

```js
/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/
Expand All @@ -187,7 +187,7 @@ class Bar {}

Examples of **correct** code for this rule with `"lexicalBindings"` option set to `true`:

::: correct
::: correct { "sourceType": "script" }

```js
/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/
Expand Down Expand Up @@ -221,7 +221,7 @@ Even the `typeof` check is not safe from TDZ reference exceptions.

Examples of **incorrect** code for this rule with `"lexicalBindings"` option set to `true`:

::: incorrect
::: incorrect { "sourceType": "script" }

```js
/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/
Expand All @@ -239,7 +239,7 @@ const MyGlobalFunction = (function() {

Examples of **correct** code for this rule with `"lexicalBindings"` option set to `true`:

::: correct
::: correct { "sourceType": "script" }

```js
/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/
Expand All @@ -261,7 +261,7 @@ You can use `/* exported variableName */` block comments in the same way as in [

Examples of **correct** code for `/* exported variableName */` operation:

::: correct
::: correct { "sourceType": "script" }

```js
/* exported global_var */
Expand Down
36 changes: 18 additions & 18 deletions docs/src/rules/no-restricted-imports.md
Expand Up @@ -130,7 +130,7 @@ To restrict the use of all Node.js core imports (via <https://github.com/nodejs/

Examples of **incorrect** code for this rule:

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", "fs"]*/
Expand All @@ -140,7 +140,7 @@ import fs from 'fs';

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", "fs"]*/
Expand All @@ -150,7 +150,7 @@ export { fs } from 'fs';

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", "fs"]*/
Expand All @@ -160,7 +160,7 @@ export * from 'fs';

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { "paths": ["cluster"] }]*/
Expand All @@ -170,7 +170,7 @@ import cluster from 'cluster';

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { "patterns": ["lodash/*"] }]*/
Expand All @@ -180,7 +180,7 @@ import pick from 'lodash/pick';

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { paths: [{
Expand All @@ -194,7 +194,7 @@ import DisallowedObject from "foo";

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { paths: [{
Expand All @@ -212,7 +212,7 @@ import { "DisallowedObject" as AllowedObject } from "foo";

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { paths: [{
Expand All @@ -226,7 +226,7 @@ import * as Foo from "foo";

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
Expand All @@ -239,7 +239,7 @@ import pick from 'lodash/pick';

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
Expand All @@ -252,7 +252,7 @@ import pick from 'fooBar';

:::

::: incorrect
::: incorrect { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
Expand All @@ -268,7 +268,7 @@ import { isEmpty } from 'utils/collection-utils';

Examples of **correct** code for this rule:

::: correct
::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", "fs"]*/
Expand All @@ -279,7 +279,7 @@ export { foo } from "bar";

:::

::: correct
::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { "paths": ["fs"], "patterns": ["eslint/*"] }]*/
Expand All @@ -291,7 +291,7 @@ export * from "path";

:::

::: correct
::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { paths: [{ name: "foo", importNames: ["DisallowedObject"] }] }]*/
Expand All @@ -301,7 +301,7 @@ import DisallowedObject from "foo"

:::

::: correct
::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { paths: [{
Expand All @@ -315,7 +315,7 @@ import { AllowedObject as DisallowedObject } from "foo";

:::

::: correct
::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
Expand All @@ -328,7 +328,7 @@ import lodash from 'lodash';

:::

::: correct
::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
Expand All @@ -341,7 +341,7 @@ import pick from 'food';

:::

::: correct
::: correct { "sourceType": "module" }

```js
/*eslint no-restricted-imports: ["error", { patterns: [{
Expand Down