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

Code before:

    const apiLink = getSymbolUrl(symbol, apiEntries);
    if (apiLink) {
      // Special handling for 'readonly' - check context including sibling elements
      if (symbol === 'readonly' && !shouldLinkReadonly(span, before, after)) {
        return; // Skip linking for TypeScript readonly keyword
      }

      // Create a new link element
      const linkElement = fragment.ownerDocument!.createElement('a');
      linkElement.href = apiLink;
      linkElement.textContent = symbol;

      // Clear the span's content and insert the link as a child
      span.textContent = before;
      span.appendChild(linkElement);
      span.append(fragment.ownerDocument!.createTextNode(after));
    }
  });
}

/**
 * Determines if 'readonly' should be linked based on its context.
 * Returns true if it should be linked (API usage), false if it's a TypeScript keyword.
 */
function shouldLinkReadonly(span: Element, before: string, after: string): boolean {
  // Pattern 1: Function call - readonly() or readonly(...)
  if (after.trim().startsWith('(')) {
    return true;
  }

  // Check next sibling spans for context
  let nextText = after;
  let nextSibling = span.nextSibling;

  // Collect text from next few siblings to get proper context
  let siblingCount = 0;
  while (nextSibling && siblingCount < 3) {
    if (nextSibling.nodeType === 3) {
      // Text node
      nextText += nextSibling.textContent || '';
    } else if (nextSibling.nodeType === 1) {
      // Element node
      nextText += (nextSibling as Element).textContent || '';
    }
    nextSibling = nextSibling.nextSibling;
    siblingCount++;
  }

  // Pattern 2: Import statement
  if (before.includes('import') && (before.includes('{') || nextText.includes('}'))) {

Review comment (suggestion):
I think we should rather not link js/ts keywords instead of going through that mess.  We have `LINK_EXEMPT` for that. 

Code after:

    const apiLink = getSymbolUrl(symbol, apiEntries);
    if (apiLink) {
      // Create a new link element
      const linkElement = fragment.ownerDocument!.createElement('a');
      linkElement.href = apiLink;
      linkElement.textContent = symbol;

      // Clear the span's content and insert the link as a child
      span.textContent = before;
      span.appendChild(linkElement);
      span.append(fragment.ownerDocument!.createTextNode(after));
    }
  });
}

/** Build the header element if a header is provided in the token. */
function buildHeaderElement(token: CodeToken) {
  return token.header ? `<div class="docs-code-header"><h3>${token.header}</h3></div>` : '';
}

function applyContainerAttributesAndClasses(el: Element, token: CodeToken) {
  // Attributes
  // String value attributes
  if (token.path) {
    el.setAttribute('path', token.path);
  }
  if (token.visibleLines) {
    el.setAttribute('visibleLines', expandRangeStringValues(token.visibleLines).toString());
  }
  if (token.header) {
    el.setAttribute('header', token.header);
  }

  // Boolean value attributes
  if (token.preview) {
    el.setAttribute('preview', 'true');
  }
  if (token.hideCode) {
    el.setAttribute('hideCode', 'true');
  }

  const language = token.language;

  if (language === 'mermaid') {
    el.setAttribute('mermaid', 'true');
  }

  // Classes
  if (language === 'shell' || language === 'bash') {
    el.classList.add('shell');