Repository: eslint/eslint
File: docs/tools/markdown-it-rule-example.js (JavaScript)

Code before:
 *         open(type, code, parserOptions, codeBlockToken) {
 *             // do something
 *         }
 *         close() {
 *             // do something
 *         }
 *     }))
 *     .render(text);
 *
 */
function markdownItRuleExample({ open, close }) {
    return {
        validate(info) {
            return /^\s*(?:in)?correct(?!\S)/u.test(info);
        },
        render(tokens, index) {
            const tagToken = tokens[index];

            if (tagToken.nesting < 0) {
                const text = close ? close() : void 0;

                // Return an empty string to avoid appending unexpected text to the output.
                return typeof text === "string" ? text : "";
            }

            const { type, parserOptionsJSON } = /^\s*(?<type>\S+)(\s+(?<parserOptionsJSON>.+?))?\s*$/u.exec(tagToken.info).groups;
            const parserOptions = { sourceType: "module", ...(parserOptionsJSON && JSON.parse(parserOptionsJSON)) };
            const codeBlockToken = tokens[index + 1];

            // Remove trailing newline and presentational `⏎` characters (https://github.com/eslint/eslint/issues/17627):
            const code = codeBlockToken.content
                .replace(/\n$/u, "")
                .replace(/⏎(?=\n)/gu, "");

            const text = open(type, code, parserOptions, codeBlockToken);

            // Return an empty string to avoid appending unexpected text to the output.
            return typeof text === "string" ? text : "";
        }
    };
}

module.exports = markdownItRuleExample;


Review comment (bug):
```suggestion
            const { type, parserOptionsJSON } = /^\s*(?<type>\S+)(\s+(?<parserOptionsJSON>\S.*?))?\s*$/u.exec(tagToken.info).groups;
```

To avoid crashing on multiple trailing spaces.

```js
const { type, parserOptionsJSON } = /^\s*(?<type>\S+)(\s+(?<parserOptionsJSON>.+?))?\s*$/u.exec("correct  ").groups;

console.log({ type, parserOptionsJSON }); // { type: 'correct', parserOptionsJSON: ' ' }

parserOptionsJSON && JSON.parse(parserOptionsJSON); // SyntaxError: Unexpected end of JSON input
```

Code after:
 *         open(type, code, parserOptions, codeBlockToken) {
 *             // do something
 *         }
 *         close() {
 *             // do something
 *         }
 *     }))
 *     .render(text);
 *
 */
function markdownItRuleExample({ open, close }) {
    return {
        validate(info) {
            return /^\s*(?:in)?correct(?!\S)/u.test(info);
        },
        render(tokens, index) {
            const tagToken = tokens[index];

            if (tagToken.nesting < 0) {
                const text = close ? close() : void 0;

                // Return an empty string to avoid appending unexpected text to the output.
                return typeof text === "string" ? text : "";
            }

            const { type, parserOptionsJSON } = /^\s*(?<type>\S+)(\s+(?<parserOptionsJSON>\S.*?))?\s*$/u.exec(tagToken.info).groups;
            const parserOptions = { sourceType: "module", ...(parserOptionsJSON && JSON.parse(parserOptionsJSON)) };
            const codeBlockToken = tokens[index + 1];

            // Remove trailing newline and presentational `⏎` characters (https://github.com/eslint/eslint/issues/17627):
            const code = codeBlockToken.content
                .replace(/\n$/u, "")
                .replace(/⏎(?=\n)/gu, "");

            const text = open(type, code, parserOptions, codeBlockToken);

            // Return an empty string to avoid appending unexpected text to the output.
            return typeof text === "string" ? text : "";
        }
    };
}

module.exports = markdownItRuleExample;