@@ -2,6 +2,7 @@ export interface Backref {
22 end_idx : number ;
33 start_idx : number ;
44 block_id ?: string ;
5+ page_id ?: string ;
56}
67
78export interface HighlightElement {
@@ -88,9 +89,15 @@ export function highlightTextNode(
8889}
8990
9091/**
91- * Processes a single backref and applies highlighting to the target block
92+ * Processes a single backref and applies highlighting to the target block or page title
9293 */
9394export function processBackref ( backref : Backref ) : HTMLSpanElement | null {
95+ // If page_id is provided, target the page title
96+ if ( backref . page_id ) {
97+ return processPageTitleBackref ( backref ) ;
98+ }
99+
100+ // Otherwise, handle block highlighting as before
94101 if ( ! backref . block_id ) {
95102 return null ;
96103 }
@@ -120,6 +127,36 @@ export function processBackref(backref: Backref): HTMLSpanElement | null {
120127 return firstSpanForThisBackref ;
121128}
122129
130+ /**
131+ * Processes a backref that targets a page title
132+ */
133+ export function processPageTitleBackref ( backref : Backref ) : HTMLSpanElement | null {
134+ // Find the specific page title element using data-page-id
135+ const pageTitleElement = document . querySelector (
136+ `[data-page-id="${ backref . page_id } "]`
137+ ) ;
138+
139+ if ( ! pageTitleElement ) {
140+ return null ;
141+ }
142+
143+ const textNodes = getTextNodes ( pageTitleElement ) ;
144+ let currentIndex = 0 ;
145+ let firstSpanForThisBackref : HTMLSpanElement | null = null ;
146+
147+ for ( const textNode of textNodes ) {
148+ const highlightSpan = highlightTextNode ( textNode , backref , currentIndex ) ;
149+
150+ if ( highlightSpan && ! firstSpanForThisBackref ) {
151+ firstSpanForThisBackref = highlightSpan ;
152+ }
153+
154+ currentIndex += textNode . textContent ?. length || 0 ;
155+ }
156+
157+ return firstSpanForThisBackref ;
158+ }
159+
123160/**
124161 * Sorts highlight elements by their DOM position (reading order)
125162 */
0 commit comments