<commit_before>import React, { useEffect, useRef, useCallback } from 'react';
import ReactDOM from 'react-dom';
import { transform } from '@babel/standalone';
import { Panel } from 'zarm';
import enUS from 'zarm/config-provider/locale/en_US';
import zhCN from 'zarm/config-provider/locale/zh_CN';
import 'zarm/style/entry';

export default ({ location, globalContext, children }) => {
  const containerId = `${parseInt(Math.random() * 1e9, 10).toString(36)}`;
  const document = children.match(/([^]*)\n?(```[^]+```)/);
  const title = String(document[1]);
  const containerRef = useRef();

  const renderSource = useCallback(() => {
    const source = document[2].match(/```(.*)\n?([^]+)```/);

    import('zarm')
      .then((Element) => {
        const locale = {
          en_US: enUS,
          zh_CN: zhCN,
        };
        const args = ['context', 'React', 'ReactDOM', 'Zarm', 'GlobalContext', 'Locale'];
        const argv = [this, React, ReactDOM, Element, globalContext, locale];

        return {
          args,
          argv,
        };
      })
      .then(({ args, argv }) => {
        const value = source[2]
          .replace(/import\s+\{\s+(.*)\s+\}\s+from\s+'react';/, 'const { $1 } = React;')
          .replace(/import\s+\{\s+(.*)\s+\}\s+from\s+'zarm';/, 'const { $1 } = Zarm;')
          .replace(
            /import\s+(.*)\s+from\s+'zarm\/lib\/config-provider\/locale\/(.*)';/g,
            "const $1 = Locale['$2'];",
          )
          // 替换格式
          // ReactDOM.render(<Demo />, mountNode);
          .replace(
            /ReactDOM.render\(\s?([^]+?)(,\s?mountNode\s?\))/g,
            `ReactDOM.render($1, document.getElementById('${containerId}'))`,
          )
          // 替换格式
          // ReactDOM.render(
          //   <>
          //     <Button>default</Button>
          //     <Button theme="primary">primary</Button>
          //   </>,
          //   mountNode,
          // );
          .replace(
            /ReactDOM.render\(\s?([^]+?)(,([\r\n])(\s)*mountNode,\s?\))/g,
            `ReactDOM.render($1, document.getElementById('${containerId}'))`,
          );

        const { code } = transform(value, {
          presets: ['es2015', 'react'],
          plugins: ['proposal-class-properties'],
        });

        args.push(code);
        // eslint-disable-next-line
        new Function(...args)(...argv);
        // source[2] = value;
      })
      .catch((err) => {<commit_msg>chore: fix regular parsing errors. fix #733<commit_after>import React, { useEffect, useRef, useCallback } from 'react';
import ReactDOM from 'react-dom';
import { transform } from '@babel/standalone';
import { Panel } from 'zarm';
import enUS from 'zarm/config-provider/locale/en_US';
import zhCN from 'zarm/config-provider/locale/zh_CN';
import 'zarm/style/entry';

export default ({ location, globalContext, children }) => {
  const containerId = `${parseInt(Math.random() * 1e9, 10).toString(36)}`;
  const document = children.match(/([^]*)\n?(```[^]+```)/);
  const title = String(document[1]);
  const containerRef = useRef();

  const renderSource = useCallback(() => {
    const source = document[2].match(/```(.*)\n?([^]+)```/);

    import('zarm')
      .then((Element) => {
        const locale = {
          en_US: enUS,
          zh_CN: zhCN,
        };
        const args = ['context', 'React', 'ReactDOM', 'Zarm', 'GlobalContext', 'Locale'];
        const argv = [this, React, ReactDOM, Element, globalContext, locale];

        return {
          args,
          argv,
        };
      })
      .then(({ args, argv }) => {
        const value = source[2]
          .replace(/import\s+\{\s+(.*)\s+\}\s+from\s+'react';/, 'const { $1 } = React;')
          .replace(/import\s+\{\s+(.*)\s+\}\s+from\s+'zarm';/, 'const { $1 } = Zarm;')
          .replace(
            /import\s+(.*)\s+from\s+'zarm\/lib\/config-provider\/locale\/(.*)';/g,
            "const $1 = Locale['$2'];",
          )
          // 替换格式
          // ReactDOM.render(<Demo />, mountNode);
          .replace(
            /ReactDOM.render\(\s?([^]+?)(,\s?mountNode\s?\))/g,
            `ReactDOM.render($1, document.getElementById('${containerId}'))`,
          )
          // 替换格式
          // ReactDOM.render(
          //   <>
          //     <Button>default</Button>
          //     <Button theme="primary">primary</Button>
          //   </>,
          //   mountNode,
          // );
          .replace(
            /ReactDOM.render\(\s?([^]+?)(,([\r\n])(\s)*mountNode,(\s)*\))/g,
            `ReactDOM.render($1, document.getElementById('${containerId}'))`,
          );

        const { code } = transform(value, {
          presets: ['es2015', 'react'],
          plugins: ['proposal-class-properties'],
        });

        args.push(code);
        // eslint-disable-next-line
        new Function(...args)(...argv);
        // source[2] = value;
      })
      .catch((err) => {
{'fraction_non_alphanumeric': 0.21771894093686353, 'fraction_numerical': 0.008757637474541752, 'mean_word_length': 2.001833740831296, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1443112', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1592, 'n_words': 270}