Repository: facebook/docusaurus
File: packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts (TypeScript)

Code before:
 * If the metastring contains a range, the `content` will be returned as-is
 * without any parsing. The returned `lineClassNames` will be a map from that
 * number range to the first magic comment config entry (which _should_ be for
 * line highlight directives.)
 *
 * @param content The raw code with magic comments. Trailing newline will be
 * trimmed upfront.
 * @param options Options for parsing behavior.
 */
export function parseLines(
  content: string,
  options: CodeBlockParseLinesOptions,
): CodeBlockParsedLines {
  const parsedLines: CodeBlockParsedLines = {
    code: content.replace(/\n$/, ''),
    lineClassNames: {},
  };

  // Highlighted lines specified in props: don't parse the content
  if (fillLineClassNamesFromMetastring(parsedLines, options)) {
    return parsedLines;
  }

  fillLineClassNamesFromCode(parsedLines, options);

  return parsedLines;
}

/**
 * Parses {@link CodeBlockParsedLines.lineClassNames} from the given metastring and fills
 * it into {@link parsedLines}.
 * @param parsedLines The object to fill the parsed class names into.
 * @param options The options to configure the parsing behavior.
 * @throws {Error} Will throw an error if the metastring contains highlighted lines, but there are no magic comments configured.
 * @returns `true` if the metastring contained any highlighted lines, otherwise `false`.
 */
function fillLineClassNamesFromMetastring(
  parsedLines: CodeBlockParsedLines,
  options: CodeBlockParseLinesOptions,
): boolean {
  const {magicComments, metastring} = options;
  const {lineClassNames} = parsedLines;

  if (metastring && metastringLinesRangeRegex.test(metastring)) {
    const linesRange = metastring.match(metastringLinesRangeRegex)!.groups!
      .range!;
    if (magicComments.length === 0) {
      throw new Error(
        `A highlight range has been given in code block's metastring (\`\`\` ${metastring}), but no magic comment config is available. Docusaurus applies the first magic comment entry's className for metastring ranges.`,
      );
    }

Review comment (suggestion):
Pseudo code but I expect it to look more like this:

```suggestion
  return getLineClassNamesFromMetastring(options) ?? getLineClassNamesFromCode(code, options);
```



Code after:
  if (metastring && metastringLinesRangeRegex.test(metastring)) {
    const linesRange = metastring.match(metastringLinesRangeRegex)!.groups!
      .range!;
    if (magicComments.length === 0) {
      throw new Error(
        `A highlight range has been given in code block's metastring (\`\`\` ${metastring}), but no magic comment config is available. Docusaurus applies the first magic comment entry's className for metastring ranges.`,
      );
    }
    const metastringRangeClassName = magicComments[0]!.className;
    const lines = rangeParser(linesRange)
      .filter((n) => n > 0)
      .map((n) => [n - 1, [metastringRangeClassName]] as [number, string[]]);
    return {lineClassNames: Object.fromEntries(lines), code};
  }
  if (language === undefined) {
    return {lineClassNames: {}, code};
  }
  const directiveRegex = getAllMagicCommentDirectiveStyles(
    language,
    magicComments,
  );
  // Go through line by line
  const lines = code.split('\n');
  const blocks = Object.fromEntries(
    magicComments.map((d) => [d.className, {start: 0, range: ''}]),
  );
  const lineToClassName: {[comment: string]: string} = Object.fromEntries(
    magicComments
      .filter((d) => d.line)
      .map(({className, line}) => [line!, className] as [string, string]),
  );
  const blockStartToClassName: {[comment: string]: string} = Object.fromEntries(
    magicComments
      .filter((d) => d.block)
      .map(({className, block}) => [block!.start, className]),
  );
  const blockEndToClassName: {[comment: string]: string} = Object.fromEntries(
    magicComments
      .filter((d) => d.block)
      .map(({className, block}) => [block!.end, className]),
  );
  for (let lineNumber = 0; lineNumber < lines.length; ) {
    const line = lines[lineNumber]!;
    const match = line.match(directiveRegex);
    if (!match) {
      // Lines without directives are unchanged
      lineNumber += 1;
      continue;
    }
    const directive = match
      .slice(1)