Skip to content

Escape literal names in the API Markdown export - #5042

Merged
akira28 merged 5 commits into
mainfrom
feature/api-export-literal-text
Nov 25, 2026
Merged

akira28 merged 5 commits into
mainfrom
feature/api-export-literal-text

Conversation

@akira28

@akira28 akira28 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

The API Markdown export now writes names from the spec, such as a group titled HTTP <code> responses, as escaped text. A reader or parser gets them back as written, and front-matter titles and descriptions are plain text.

Affects: Content export, API reference, Search

Prompt summary: The author asks to rethink making API group, type and topic pages searchable (elastic/docs-eng-team#1230) after review rounds on #5036 kept finding edge cases. The goal is the cleanest design, not more patches. This PR is the first part: an export that search and other readers can parse without guessing.

Stack: 1 of 2. #5043 builds searchable API pages on it.

Why

ApiCommonMark writes names from the spec into headings, link text and paragraphs as raw Markdown. A name such as HTTP <code> responses therefore reads back as an HTML tag and loses a word. A name with *, _, [ or a backtick can turn into emphasis, a link or code. The front-matter title is the raw H1 line, and some descriptions are raw Markdown while others are plain text. Every reader of the export, including the downloadable .md pages and search, has to guess which is which.

What

Literal text is escaped

ApiCommonMark.Text escapes only what could start markup where it stands: backslashes, backticks, *, brackets, angle brackets, an underscore at a word boundary, and an & that would read as an entity. max_docs and Snapshot & restore stay as they are. Each export writer that passes a name uses it, through TextHeading and TextLink: group, type, topic, structural and operation titles, catalog and overview rows, link text, example and scenario titles, and auth scheme labels. Callers that pass Markdown, such as authored supplemental headings or a link wrapped around a code span, keep the raw path. A literal title ending in # keeps its #, and the link pass skips a ] escaped by an odd run of backslashes.

Plain-text front matter

ApiMarkdownFrontMatter.Collect reads the H1 and every Markdown description source through one plain-text converter. So title: and description: hold what a reader sees, with escapes, code spans and link targets resolved. The title is read as a heading, so 1. List roles keeps its number, and it is written as quoted YAML, like the description. An operation without its own description takes its OpenAPI summary as written.

One plain-text converter

ApiMarkdown.ToPlainText defines plain text as what a reader sees on the page. Markdig renders the Markdown to HTML, as an API description is rendered, and ApiMarkdown.StripHtml reads its text. Code spans, escapes, entities and underscores inside words come out as written. Emphasis, link targets and tags go, while the text of HTML links and blocks stays. It replaces the eight regexes in ApiSeoDescription, so SEO excerpts, front matter and property-search descriptions all read text the same way. The property index no longer strips HTML a second time.

Review rules

These settle trade-offs that review findings can pull either way. A finding that breaks one is a bug; one that asks to trade against it is out of scope.

  • Literal names are escaped where they are written. Text that is already Markdown, such as authored headings or descriptions, is written as is.
  • Plain text is what the rendered page shows. The page renders a description's raw HTML and sanitizes it, so a bare <index> and image alt text do not appear, and plain text drops them too. A placeholder meant to show goes in a code span.

Verify

dotnet test --project tests/Elastic.ApiExplorer.Tests --treenode-filter "/*/*/ApiCommonMarkTests/*"
# Text_ReadBackAsMarkdown_IsTheTextAsWritten — escaped names read back unchanged
dotnet test --project tests/Elastic.ApiExplorer.Tests --treenode-filter "/*/*/OpenApiGeneratorMarkdownEmissionTests/*"
# Generate_NameWithAngleBrackets_IsEscapedInMarkdownAndPlainInFrontMatter
./build.sh unit-test

Check on the preview

🤖 Generated with Claude Code

@akira28
akira28 requested a review from a team as a code owner November 24, 2026 17:57
@akira28
akira28 requested a review from theletterf November 24, 2026 17:57
@akira28
akira28 added this pull request to stack #5044 November 24, 2026 17:57
@akira28 akira28 changed the title feature/api export literal text Escape literal names in the API Markdown export Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

akira28 and others added 2 commits November 24, 2026 19:37
The export wrote names from the spec as raw Markdown, so a group titled
`HTTP <code> responses` read back as a tag and lost a word. Names now go
through `ApiCommonMark.Text`, which escapes only what could start markup.
Front-matter titles and descriptions are plain text, read with Markdig. The
same converter replaces the regexes in `ApiSeoDescription`, so SEO excerpts
keep code spans, escapes and underscores inside words as written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A trailing `#` in a heading title is escaped so it is not read as a closing
sequence, and the link pass skips an escaped `](`. The front-matter title is
quoted YAML read as a heading, so `#404` and `1. List roles` survive. An
operation's summary stays literal, the catalog excerpt is escaped, and the
plain-text converter leaves HTML as written, as the regexes did, so literal
placeholders stay and the property index still strips real tags.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@akira28
akira28 force-pushed the feature/api-export-literal-text branch from 3f42887 to 7a2f962 Compare November 24, 2026 18:40
Plain text is now the text a reader sees: Markdig renders the Markdown to
HTML, as an API description is, and `ApiMarkdown.StripHtml` reads it. Real
HTML links and blocks keep their text, tags go, and code spans and escapes
stay. The converter moves to `ApiMarkdown`, and the property index no longer
strips HTML twice. `TextHeading` and `TextLink` write literal names, `Text`
escapes a closing `#` itself so authored headings keep theirs, and the link
pass counts backslashes before `]`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment on lines +264 to +270
private static readonly MarkdownPipeline PlainTextPipeline = new MarkdownPipelineBuilder().Build();

/// <summary>
/// Markdown as the text a reader sees on the page: rendered to HTML, as an API description is, then read for its text. Code spans,
/// escapes and entities come out as written; emphasis, link targets and tags go, while the text inside HTML blocks and links stays.
/// </summary>
internal static string ToPlainText(string markdown) => StripHtml(Markdown.ToHtml(markdown, PlainTextPipeline));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: P3 (Low)

PlainTextPipeline no longer calls DisableHtml(). Because of that, a bare <index> or <target> placeholder in a description is parsed as an inline HTML tag, and StripHtml then drops it. SEO meta descriptions, catalog summaries and front-matter description: values (all built through ApiSeoDescription.Excerpt/FirstLine) now lose those words. For example, Returns <index> when available. becomes Returns when available.. The old regex converter kept the placeholder, and the PR description says it still survives. The test ToPlainText_Html_StaysAsWritten that guarded this case was deleted, not updated. Either restore DisableHtml() and strip real tags only where they are needed, or update the claim and add a test for the new behaviour.

Generated by Libra. Specialist: libra-complexity. React with 👍 or 👎 to give feedback on this comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this, by the rule this PR now states: plain text is what the rendered page shows. The page renders a description's raw HTML through ApiDescriptionPipeline and then HtmlSanitizer, which removes an unknown tag such as <index>, so the page does not show it either. A placeholder meant to show belongs in a code span, which keeps it. The PR description does not claim a bare placeholder survives. ToPlainText_Markdown_IsTheTextThePageShows now pins this case in 78b8366.

/// Markdown as the text a reader sees on the page: rendered to HTML, as an API description is, then read for its text. Code spans,
/// escapes and entities come out as written; emphasis, link targets and tags go, while the text inside HTML blocks and links stays.
/// </summary>
internal static string ToPlainText(string markdown) => StripHtml(Markdown.ToHtml(markdown, PlainTextPipeline));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: P3 (Low)

Preserve Markdown image alt text in plain-text descriptions. Rendering See ![shard layout](diagram.png). to HTML puts the label in an alt attribute, which StripHtml never reads. This drops words from search descriptions and makes an image-only description fall back to generic text.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this, by the same rule: an image shows as a picture, and its alt text is not visible text on the page. Pinned in ToPlainText_Markdown_IsTheTextThePageShows in 78b8366.

A bare `<index>` in a description is raw HTML that the page's sanitizer
removes, and an image shows as a picture, so neither reaches plain text.
The tests now state that rule next to the cases that keep text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@akira28
akira28 merged commit 961fd9d into main Nov 25, 2026
44 checks passed
@akira28
akira28 deleted the feature/api-export-literal-text branch November 25, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants