function serializeRouteConfig()

in packages/docusaurus/src/server/routes.ts [92:146]


function serializeRouteConfig({
  routePath,
  routeHash,
  exact,
  subroutesCodeStrings,
  props,
}: {
  routePath: string;
  routeHash: string;
  exact?: boolean;
  subroutesCodeStrings?: string[];
  props: {[propName: string]: unknown};
}) {
  const parts = [
    `path: '${routePath}'`,
    `component: ComponentCreator('${routePath}', '${routeHash}')`,
  ];

  if (exact) {
    parts.push(`exact: true`);
  }

  if (subroutesCodeStrings) {
    parts.push(
      `routes: [
${indent(subroutesCodeStrings.join(',\n'))}
]`,
    );
  }

  Object.entries(props).forEach(([propName, propValue]) => {
    // Inspired by https://github.com/armanozak/should-quote/blob/main/packages/should-quote/src/lib/should-quote.ts
    const shouldQuote = ((key: string) => {
      // Pre-sanitation to prevent injection
      if (/[.,;:}/\s]/.test(key)) {
        return true;
      }
      try {
        // If this key can be used in an expression like ({a:0}).a
        // eslint-disable-next-line no-eval
        eval(`({${key}:0}).${key}`);
        return false;
      } catch {
        return true;
      }
    })(propName);
    // Escape quotes as well
    const key = shouldQuote ? JSON.stringify(propName) : propName;
    parts.push(`${key}: ${JSON.stringify(propValue)}`);
  });

  return `{
${indent(parts.join(',\n'))}
}`;
}