ReviewOS

also looking at this

stacks/stx

fix(layouts): fill @parent in a layout that extends another layout

#2103
Open mkantautas wants to merge fix/nested-layout-parent into main
2 files +88 -1
packages/stx/src/process.tsmodified+17-1
Changes to packages/stx/src/process.ts
@@ -99,6 +99,22 @@ export { validateClientScript } from './script-validation'
9999 */
100100const PARENT_PLACEHOLDER = '<!--PARENT_CONTENT_PLACEHOLDER-->'
101101
102/**
103 * Mark the `@parent` in a section of a layout that extends another layout, as
104 * the page's own sections are marked, so the layout it extends splices its
105 * section in there. Layout source arrives raw, so `@@parent`, a longer word
106 * (`a@parents.com`) and a `@parent` inside a comment stay text.
107 */
108function markLayoutParent(content: string): string {
109 const visible = maskAtElementPosition(
110 content.replace(/\{\{--[\s\S]*?--\}\}/g, comment => ' '.repeat(comment.length)),
111 matchHtmlComment,
112 comment => ' '.repeat(comment.length),
113 ).output
114 const at = visible.search(/(?<![\w@])@parent(?!\w)/)
115 return at === -1 ? content : content.slice(0, at) + PARENT_PLACEHOLDER + content.slice(at + '@parent'.length)
116}
117
102118function escapeHtmlAttribute(value: string): string {
103119 return value
104120 .replace(/&/g, '&amp;')
@@ -1445,7 +1461,7 @@ async function processDirectivesInternal(
14451461 // Extract layout sections for parent content
14461462 const layoutSections: Record<string, string> = {}
14471463 const processedLayout = layoutContent.replace(/@section\(\s*['"]([^'"]+)['"]\s*\)([\s\S]*?)(?:@show|@endsection)/g, (_, name, content) => {
1448 layoutSections[name] = content.trim()
1464 layoutSections[name] = markLayoutParent(content.trim())
14491465 return '' // Remove the section from the template
14501466 })
14511467