@@ -871,15 +871,15 @@ describe("ChatMarkdown heading levels", () => {
871871 /> ,
872872 ) ;
873873
874- expect ( html ) . toContain ( '<h1 aria-level="4">Top</h1>' ) ;
875- expect ( html ) . toContain ( '<h2 aria-level="5">Section</h2>' ) ;
876- expect ( html ) . toContain ( '<h6 aria-level="6">Fine print</h6>' ) ;
874+ expect ( html ) . toContain ( '<h1 id="user-content-top" aria-level="4">Top</h1>' ) ;
875+ expect ( html ) . toContain ( '<h2 id="user-content-section" aria-level="5">Section</h2>' ) ;
876+ expect ( html ) . toContain ( '<h6 id="user-content-fine-print" aria-level="6">Fine print</h6>' ) ;
877877 } ) ;
878878
879879 it ( "leaves heading levels alone when the markdown is not nested" , ( ) => {
880880 const html = renderToStaticMarkup ( < ChatMarkdown cwd = "/tmp/project" text = "# Top" /> ) ;
881881
882- expect ( html ) . toContain ( " <h1>Top</h1>" ) ;
882+ expect ( html ) . toContain ( ' <h1 id="user-content-top" >Top</h1>' ) ;
883883 } ) ;
884884} ) ;
885885
@@ -1272,3 +1272,77 @@ it.each([
12721272 }
12731273 } ,
12741274) ;
1275+
1276+ describe ( "ChatMarkdown heading ids" , ( ) => {
1277+ it ( "never gives two headings the same id, even when a suffix matches another heading" , ( ) => {
1278+ const html = renderToStaticMarkup (
1279+ < ChatMarkdown
1280+ cwd = "/tmp/project"
1281+ parseRawHtml
1282+ text = {
1283+ '## Setup\n\n## Setup\n\n## Setup-1\n\n<h2 id="install-1">Pinned</h2>\n\n## Install\n\n## Install'
1284+ }
1285+ /> ,
1286+ ) ;
1287+ const ids = [ ...html . matchAll ( / < h 2 i d = " ( [ ^ " ] + ) " / g) ] . map ( ( match ) => match [ 1 ] ) ;
1288+ expect ( ids ) . toEqual ( [
1289+ "user-content-setup" ,
1290+ "user-content-setup-1" ,
1291+ "user-content-setup-1-1" ,
1292+ "user-content-install-1" ,
1293+ "user-content-install" ,
1294+ "user-content-install-2" ,
1295+ ] ) ;
1296+ expect ( new Set ( ids ) . size ) . toBe ( ids . length ) ;
1297+ } ) ;
1298+ } ) ;
1299+
1300+ describe ( "ChatMarkdown in-page links" , ( ) => {
1301+ it . each ( [ true , false ] ) (
1302+ "scrolls a table-of-contents link to its heading without touching the URL (parseRawHtml=%s)" ,
1303+ async ( parseRawHtml ) => {
1304+ vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
1305+ const { createRoot } = await import ( "react-dom/client" ) ;
1306+ const container = document . createElement ( "div" ) ;
1307+ document . body . append ( container ) ;
1308+ const root = createRoot ( container ) ;
1309+ window . history . replaceState ( null , "" , "/#/env/thread" ) ;
1310+ const scrollIntoView = vi . fn ( ) ;
1311+ HTMLElement . prototype . scrollIntoView = scrollIntoView ;
1312+ try {
1313+ await act ( async ( ) => {
1314+ root . render (
1315+ < ChatMarkdown
1316+ cwd = "/tmp/project"
1317+ parseRawHtml = { parseRawHtml }
1318+ text = {
1319+ "- [Operating model](#1-operating-model)\n- [Missing](#nowhere)\n\n## 1. Operating model\n\n## 1. Operating model"
1320+ }
1321+ /> ,
1322+ ) ;
1323+ } ) ;
1324+ const headings = [ ...container . querySelectorAll ( "h2" ) ] ;
1325+ expect ( headings . map ( ( heading ) => heading . id ) ) . toEqual ( [
1326+ "user-content-1-operating-model" ,
1327+ "user-content-1-operating-model-1" ,
1328+ ] ) ;
1329+
1330+ const [ tocLink , missingLink ] = [ ...container . querySelectorAll ( "a" ) ] ;
1331+ const click = ( ) => new MouseEvent ( "click" , { bubbles : true , cancelable : true } ) ;
1332+ const tocClick = click ( ) ;
1333+ tocLink ! . dispatchEvent ( tocClick ) ;
1334+ expect ( tocClick . defaultPrevented ) . toBe ( true ) ;
1335+ expect ( scrollIntoView . mock . contexts ) . toEqual ( [ headings [ 0 ] ] ) ;
1336+
1337+ const missingClick = click ( ) ;
1338+ missingLink ! . dispatchEvent ( missingClick ) ;
1339+ expect ( missingClick . defaultPrevented ) . toBe ( true ) ;
1340+ expect ( scrollIntoView ) . toHaveBeenCalledTimes ( 1 ) ;
1341+ expect ( window . location . hash ) . toBe ( "#/env/thread" ) ;
1342+ } finally {
1343+ await act ( async ( ) => root . unmount ( ) ) ;
1344+ container . remove ( ) ;
1345+ }
1346+ } ,
1347+ ) ;
1348+ } ) ;
0 commit comments