Repository: angular/angular
File: adev/shared-docs/pipeline/shared/marked/extensions/docs-code/docs-code-block.mts (TypeScript)

Code before:
  },
  tokenizer(this: TokenizerThis, src: string): DocsCodeBlock | undefined {
    const match = tripleTickCodeRule.exec(src);
    if (match) {
      const metadataStr = match[2].trim();

      const headerRule = /header\s*:\s*(['"`])([^'"`]+)\1/; // The 2nd capture matters here
      const highlightRule = /highlight\s*:\s*(.*)([^,])/;
      const recommendedRule = /recommended/;
      const avoidRule = /avoid/;

      const hasRecommened = recommendedRule.test(metadataStr);
      const hasAvoid = avoidRule.test(metadataStr);

      if (hasAvoid && hasRecommened) {
        throw new Error('This code block cannot be both "Recommended" and "Avoid"');
      }

      const token: DocsCodeBlock = {
        raw: match[0],
        type: 'docs-code-block',
        code: deindent(match[3]),
        language: match[1],
        header: headerRule.exec(metadataStr)?.[2],
        highlight: highlightRule.exec(metadataStr)?.[1],
        style: hasRecommened ? 'recommended' : hasAvoid ? 'avoid' : undefined,
      };
      return token;
    }
    return undefined;
  },
  renderer(this: RendererThis, token: DocsCodeBlock) {
    if (token.language === 'mermaid') {
      return token.code;
    }
    return formatCode(token, (this.parser.renderer as AdevDocsRenderer).context);
  },
};

/**
 * Removes leading indentation from code blocks.
 */
function deindent(str: string): string {
  const lines = str.split('\n');
  const nonEmpty = lines.filter((line) => line.trim());
  let minIndent = Infinity;
  for (const line of lines) {
    if (!line.trim()) {
      minIndent = Math.min(line.match(/^(\s*)/)?.[1].length ?? 0, minIndent);
    }
  }

Review comment (refactor):
Since this is coming from the "header" text, shouldn't this be extracted the same way we extract everything else from the header? That also would make it easier for us to determine if there are other collisions

Code after:
  },
  tokenizer(this: TokenizerThis, src: string): DocsCodeBlock | undefined {
    const match = tripleTickCodeRule.exec(src);
    if (match) {
      const metadataStr = match[2].trim();

      const headerRule = /header\s*:\s*(['"`])([^'"`]+)\1/; // The 2nd capture matters here
      const highlightRule = /highlight\s*:\s*(.*)([^,])/;
      const hideCopyRule = /hideCopy/;
      const preferRule = /(prefer|avoid)/;

      const token: DocsCodeBlock = {
        raw: match[0],
        type: 'docs-code-block',
        code: deindent(match[3]),
        language: match[1],
        header: headerRule.exec(metadataStr)?.[2],
        highlight: highlightRule.exec(metadataStr)?.[1],
        hideCopy: hideCopyRule.test(metadataStr),
        style: preferRule.exec(metadataStr)?.[1] as 'prefer' | 'avoid' | undefined,
      };
      return token;
    }
    return undefined;
  },
  renderer(this: RendererThis, token: DocsCodeBlock) {
    if (token.language === 'mermaid') {
      return token.code;
    }
    return formatCode(token, (this.parser.renderer as AdevDocsRenderer).context);
  },
};

/**
 * Removes leading indentation from code blocks.
 */
function deindent(str: string): string {
  const lines = str.split('\n');
  let minIndent = Infinity;
  for (const line of lines) {
    if (!line.trim()) {
      minIndent = Math.min(line.match(/^(\s*)/)?.[1].length ?? 0, minIndent);
    }
  }
  if (minIndent === Infinity || minIndent === 0) {
    return str;
  }
  return lines.map((line) => line.slice(minIndent)).join('\n');
}