feat(editor): add Toggle Heading 2, 3, 4 to markdown editor (#8822)

* feat(editor): add Toggle Heading 2, 3, 4 to markdown editor

Generalize the heading-1-only collapsible-toggle plumbing to support
heading levels 1-4, mirroring the plain heading scales the slash menu and
toolbar dropdown already offer.

- Introduce ToggleHeadingVariant type + parseToggleHeadingVariant helper
- Widen markdown parse/render/sanitize allowlists from heading-1 to 1-4
- Add Toggle Heading 2/3/4 slash commands
- Add matching summary sizing in editor + preview CSS
- Extend round-trip tests for the new levels

Co-authored-by: Orca <help@stably.ai>

* fix(editor): preserve toggle heading variants

---------

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinjing
2026-07-14 23:14:22 -07:00
committed by GitHub
co-authored by Orca
parent 49817bab75
commit bf0b65a049
14 changed files with 239 additions and 22 deletions
@@ -556,6 +556,22 @@
line-height: 1.3;
}
/* Heading toggle sizes mirror the plain h2–h4 scales (1.4 / 1.15 / 1em). */
.markdown-body details.orca-details[data-orca-toggle='heading-2'] > summary {
font-size: 1.4em;
line-height: 1.3;
}
.markdown-body details.orca-details[data-orca-toggle='heading-3'] > summary {
font-size: 1.15em;
line-height: 1.3;
}
.markdown-body details.orca-details[data-orca-toggle='heading-4'] > summary {
font-size: 1em;
line-height: 1.3;
}
.markdown-body details.orca-details > :not(summary) {
margin-left: 0.15em;
}
@@ -764,6 +764,29 @@
height: 2.405em;
}
/* Heading toggle sizes mirror the plain h2–h4 scales (1.4 / 1.15 / 1em). */
.rich-markdown-editor details.orca-details[data-orca-toggle='heading-2'] > summary,
.rich-markdown-editor .orca-details[data-type='details'][data-orca-toggle='heading-2'] summary {
font-size: 1.4em;
line-height: 1.3;
}
.rich-markdown-editor .orca-details[data-type='details'][data-orca-toggle='heading-2'] > button {
height: 1.82em;
}
.rich-markdown-editor details.orca-details[data-orca-toggle='heading-3'] > summary,
.rich-markdown-editor .orca-details[data-type='details'][data-orca-toggle='heading-3'] summary {
font-size: 1.15em;
line-height: 1.3;
}
.rich-markdown-editor details.orca-details[data-orca-toggle='heading-4'] > summary,
.rich-markdown-editor .orca-details[data-type='details'][data-orca-toggle='heading-4'] summary {
font-size: 1em;
line-height: 1.3;
}
.rich-markdown-editor blockquote {
padding: 0.5em 1em;
border-left: 3px solid var(--border);
@@ -310,7 +310,7 @@ const markdownPreviewSanitizeSchema = {
...(defaultSchema.attributes?.details ?? []),
'open',
['className', 'orca-details'],
['dataOrcaToggle', 'heading-1']
['dataOrcaToggle', 'heading-1', 'heading-2', 'heading-3', 'heading-4']
],
h1: [...(defaultSchema.attributes?.h1 ?? []), 'id'],
h2: [...(defaultSchema.attributes?.h2 ?? []), 'id'],
@@ -1,5 +1,23 @@
import type { MarkdownToken } from '@tiptap/core'
// Toggle summaries can render at heading scales 1–4, mirroring the plain
// heading levels the slash menu / toolbar dropdown offer (h1–h4).
export type ToggleHeadingVariant = 'heading-1' | 'heading-2' | 'heading-3' | 'heading-4'
export const TOGGLE_HEADING_VARIANTS: readonly ToggleHeadingVariant[] = [
'heading-1',
'heading-2',
'heading-3',
'heading-4'
]
export function parseToggleHeadingVariant(value: unknown): ToggleHeadingVariant | null {
return typeof value === 'string' &&
TOGGLE_HEADING_VARIANTS.includes(value as ToggleHeadingVariant)
? (value as ToggleHeadingVariant)
: null
}
export type DetailsHtmlToken = MarkdownToken & {
attributes?: Record<string, unknown>
bodyTokens?: MarkdownToken[]
@@ -28,11 +46,16 @@ export function escapeDetailsHtml(value: string): string {
}
export function parseDetailsAttributes(rawAttributes: string): Record<string, unknown> {
// Why: validation accepts normal HTML whitespace around `=`, so parsing
// must accept it too or an editable toggle loses its heading variant.
const variantMatch = rawAttributes.match(
/\sdata-orca-toggle\s*=\s*(?:"(heading-[1-4])"|'(heading-[1-4])'|(heading-[1-4]))(?:\s|$)/i
)
return {
open: /\sopen(?:\s|=|$)/i.test(rawAttributes),
variant: /\sdata-orca-toggle=(?:"heading-1"|'heading-1'|heading-1)(?:\s|$)/i.test(rawAttributes)
? 'heading-1'
: null
variant: parseToggleHeadingVariant(
(variantMatch?.[1] ?? variantMatch?.[2] ?? variantMatch?.[3])?.toLowerCase()
)
}
}
@@ -47,8 +70,9 @@ export function detailsBodyHtmlToMarkdown(body: string): string {
export function renderDetailsAttributes(attrs: Record<string, unknown> | undefined): string {
const attributes = ['class="orca-details"']
if (attrs?.variant === 'heading-1') {
attributes.push('data-orca-toggle="heading-1"')
const variant = parseToggleHeadingVariant(attrs?.variant)
if (variant) {
attributes.push(`data-orca-toggle="${variant}"`)
}
if (attrs?.open === true) {
@@ -155,7 +179,10 @@ function hasOnlySupportedDetailsAttributes(rawAttributes: string): boolean {
rawAttributes
.replace(/\s+open(?:\s*=\s*(?:""|"open"|''|'open'|open))?(?=\s|$)/giu, '')
.replace(/\s+class\s*=\s*(?:"orca-details"|'orca-details'|orca-details)(?=\s|$)/giu, '')
.replace(/\s+data-orca-toggle\s*=\s*(?:"heading-1"|'heading-1'|heading-1)(?=\s|$)/giu, '')
.replace(
/\s+data-orca-toggle\s*=\s*(?:"heading-[1-4]"|'heading-[1-4]'|heading-[1-4])(?=\s|$)/giu,
''
)
.trim() === ''
)
}
@@ -132,6 +132,29 @@ describe('rich markdown round trip', () => {
)
})
it.each(['heading-2', 'heading-3', 'heading-4'])(
'preserves %s-styled details blocks',
(variant) => {
expect(
roundTripMarkdown(
`<details data-orca-toggle="${variant}"><summary>Toggle</summary><p>Body</p></details>\n`
)
).toBe(
`<details class="orca-details" data-orca-toggle="${variant}">\n<summary>Toggle</summary>\n\nBody\n\n</details>`
)
}
)
it('preserves a heading toggle when its attribute uses HTML whitespace around equals', () => {
expect(
roundTripMarkdown(
'<details data-orca-toggle = "heading-4"><summary>Toggle</summary><p>Body</p></details>\n'
)
).toBe(
'<details class="orca-details" data-orca-toggle="heading-4">\n<summary>Toggle</summary>\n\nBody\n\n</details>'
)
})
it('preserves details blocks with raw html as passthrough html', () => {
const input = '<details><summary><span>Toggle</span></summary><p><em>Body</em></p></details>\n'
expect(roundTripMarkdown(input)).toBe(input.trimEnd())
@@ -182,6 +205,17 @@ describe('rich markdown round trip', () => {
expect(slashCommandSelectionParent('toggle-h1')).toBe('detailsSummary')
})
it.each([
['toggle-h2', 'heading-2'],
['toggle-h3', 'heading-3'],
['toggle-h4', 'heading-4']
] as const)('inserts editable %s toggles from slash commands', (commandId, variant) => {
expect(slashCommandMarkdown(commandId)).toBe(
`<details class="orca-details" data-orca-toggle="${variant}" open>\n<summary></summary>\n\n\n\n</details>`
)
expect(slashCommandSelectionParent(commandId)).toBe('detailsSummary')
})
it('preserves markdown tables', () => {
expect(roundTripMarkdown('| a | b |\n| - | - |\n| 1 | 2 |\n')).toContain('| a')
})
@@ -42,8 +42,11 @@ describe('rich markdown slash commands', () => {
'Headings:heading-1',
'Headings:toggle-h1',
'Headings:heading-2',
'Headings:toggle-h2',
'Headings:heading-3',
'Headings:toggle-h3',
'Headings:heading-4',
'Headings:toggle-h4',
'Headings:heading-5',
'Basic blocks:blockquote',
'Basic blocks:ordered-list',
@@ -2,6 +2,7 @@ import { decodeHtmlEntities, type AnyExtension, type Editor } from '@tiptap/core
import { Details, DetailsContent, DetailsSummary } from '@tiptap/extension-details'
import type { PlaceholderOptions } from '@tiptap/extension-placeholder'
import { TextSelection } from '@tiptap/pm/state'
import { translate } from '@/i18n/i18n'
import {
detailsBodyHtmlToMarkdown,
escapeDetailsHtml,
@@ -9,13 +10,25 @@ import {
isEditableDetailsHtmlBlock,
matchDetailsHtmlBlock,
parseDetailsAttributes,
parseToggleHeadingVariant,
renderDetailsAttributes,
type DetailsHtmlToken
type DetailsHtmlToken,
type ToggleHeadingVariant
} from './details-markdown-html'
const RICH_MARKDOWN_PLACEHOLDER = 'Write markdown… Type / for blocks.'
const TOGGLE_TEXT_PLACEHOLDER = 'text'
const TOGGLE_HEADING_PLACEHOLDER = 'Heading 1'
const TOGGLE_HEADING_PLACEHOLDERS: Record<ToggleHeadingVariant, readonly [string, string]> = {
'heading-1': ['auto.components.editor.rich.markdown.slash.commands.e66e7f04c6', 'Heading 1'],
'heading-2': ['auto.components.editor.rich.markdown.slash.commands.c209a116b7', 'Heading 2'],
'heading-3': ['auto.components.editor.rich.markdown.slash.commands.30566ee962', 'Heading 3'],
'heading-4': ['auto.components.editor.rich.markdown.slash.commands.5f9a0ed7c4', 'Heading 4']
}
function toggleHeadingPlaceholder(variant: ToggleHeadingVariant): string {
const [key, fallback] = TOGGLE_HEADING_PLACEHOLDERS[variant]
return translate(key, fallback)
}
export function getRichMarkdownPlaceholder({
editor,
@@ -29,9 +42,12 @@ export function getRichMarkdownPlaceholder({
}
const parent = editor.state.doc.resolve(pos).parent
return parent.type.name === 'details' && parent.attrs.variant === 'heading-1'
? TOGGLE_HEADING_PLACEHOLDER
: TOGGLE_TEXT_PLACEHOLDER
if (parent.type.name !== 'details') {
return TOGGLE_TEXT_PLACEHOLDER
}
const variant = parseToggleHeadingVariant(parent.attrs.variant)
return variant ? toggleHeadingPlaceholder(variant) : TOGGLE_TEXT_PLACEHOLDER
}
export function moveDetailsSummarySelectionToContent(editor: Editor): boolean {
@@ -191,10 +207,11 @@ const OrcaDetails = Details.extend({
...this.parent?.(),
variant: {
default: null,
parseHTML: (element) =>
element.getAttribute('data-orca-toggle') === 'heading-1' ? 'heading-1' : null,
renderHTML: ({ variant }) =>
variant === 'heading-1' ? { 'data-orca-toggle': 'heading-1' } : {}
parseHTML: (element) => parseToggleHeadingVariant(element.getAttribute('data-orca-toggle')),
renderHTML: ({ variant }) => {
const parsed = parseToggleHeadingVariant(variant)
return parsed ? { 'data-orca-toggle': parsed } : {}
}
}
}
},
@@ -68,6 +68,27 @@ export const headingSlashCommands: SlashCommand[] = [
editor.chain().focus().setHeading({ level: 2 }).run()
}
},
{
id: 'toggle-h2',
get label() {
return translate(
'auto.components.editor.rich.markdown.slash.commands.7a2c1f9b04',
'Toggle Heading 2'
)
},
aliases: ['toggle-h2'],
icon: icon(ChevronRight),
group: 'Headings',
get description() {
return translate(
'auto.components.editor.rich.markdown.slash.commands.b3e5d8a1c6',
'Create a collapsible section with a medium heading summary.'
)
},
run: (editor) => {
insertToggle(editor, 'heading-2')
}
},
{
id: 'heading-3',
get label() {
@@ -90,6 +111,27 @@ export const headingSlashCommands: SlashCommand[] = [
editor.chain().focus().setHeading({ level: 3 }).run()
}
},
{
id: 'toggle-h3',
get label() {
return translate(
'auto.components.editor.rich.markdown.slash.commands.2f9d6b4e10',
'Toggle Heading 3'
)
},
aliases: ['toggle-h3'],
icon: icon(ChevronRight),
group: 'Headings',
get description() {
return translate(
'auto.components.editor.rich.markdown.slash.commands.8c1a3e7d52',
'Create a collapsible section with a small heading summary.'
)
},
run: (editor) => {
insertToggle(editor, 'heading-3')
}
},
{
id: 'heading-4',
get label() {
@@ -112,6 +154,27 @@ export const headingSlashCommands: SlashCommand[] = [
editor.chain().focus().setHeading({ level: 4 }).run()
}
},
{
id: 'toggle-h4',
get label() {
return translate(
'auto.components.editor.rich.markdown.slash.commands.5e0b9c2a71',
'Toggle Heading 4'
)
},
aliases: ['toggle-h4'],
icon: icon(ChevronRight),
group: 'Headings',
get description() {
return translate(
'auto.components.editor.rich.markdown.slash.commands.d4f16a8b39',
'Create a collapsible section with a nested heading summary.'
)
},
run: (editor) => {
insertToggle(editor, 'heading-4')
}
},
{
id: 'heading-5',
get label() {
@@ -1,6 +1,7 @@
import type React from 'react'
import type { Editor } from '@tiptap/react'
import { TextSelection } from '@tiptap/pm/state'
import type { ToggleHeadingVariant } from './details-markdown-html'
export type SlashMenuState = {
query: string
@@ -16,8 +17,11 @@ export type SlashCommandId =
| 'heading-1'
| 'toggle-h1'
| 'heading-2'
| 'toggle-h2'
| 'heading-3'
| 'toggle-h3'
| 'heading-4'
| 'toggle-h4'
| 'heading-5'
| 'task-list'
| 'bullet-list'
@@ -92,7 +96,7 @@ export function insertCodeBlock(editor: Editor, language: string, text: string):
})
}
export function insertToggle(editor: Editor, variant?: 'heading-1'): void {
export function insertToggle(editor: Editor, variant?: ToggleHeadingVariant): void {
const insertAt = editor.state.selection.from
editor
+7 -1
View File
@@ -11847,7 +11847,13 @@
"5f9a0ed7c4": "Heading 4",
"01a71dbbdd": "Nested section heading.",
"8440fa4acf": "Heading 5",
"b287b93c66": "Deep section heading."
"b287b93c66": "Deep section heading.",
"7a2c1f9b04": "Toggle Heading 2",
"b3e5d8a1c6": "Create a collapsible section with a medium heading summary.",
"2f9d6b4e10": "Toggle Heading 3",
"8c1a3e7d52": "Create a collapsible section with a small heading summary.",
"5e0b9c2a71": "Toggle Heading 4",
"d4f16a8b39": "Create a collapsible section with a nested heading summary."
}
}
}
+7 -1
View File
@@ -11847,7 +11847,13 @@
"5f9a0ed7c4": "Título 4",
"01a71dbbdd": "Encabezado de sección anidada.",
"8440fa4acf": "Título 5",
"b287b93c66": "Encabezado de sección profunda."
"b287b93c66": "Encabezado de sección profunda.",
"7a2c1f9b04": "Alternar título 2",
"b3e5d8a1c6": "Cree una sección plegable con un resumen de encabezado mediano.",
"2f9d6b4e10": "Alternar título 3",
"8c1a3e7d52": "Cree una sección plegable con un resumen de encabezado pequeño.",
"5e0b9c2a71": "Alternar título 4",
"d4f16a8b39": "Cree una sección plegable con un resumen de encabezado anidado."
}
}
}
+7 -1
View File
@@ -11847,7 +11847,13 @@
"5f9a0ed7c4": "見出し 4",
"01a71dbbdd": "ネストされたセクション見出し。",
"8440fa4acf": "見出し 5",
"b287b93c66": "深いセクションの見出し。"
"b287b93c66": "深いセクションの見出し。",
"7a2c1f9b04": "見出し 2 を切り替えます",
"b3e5d8a1c6": "中程度の見出しの概要を含む折りたたみ可能なセクションを作成します。",
"2f9d6b4e10": "見出し 3 を切り替えます",
"8c1a3e7d52": "小さな見出しの概要を含む折りたたみ可能なセクションを作成します。",
"5e0b9c2a71": "見出し 4 を切り替えます",
"d4f16a8b39": "入れ子の見出しの概要を含む折りたたみ可能なセクションを作成します。"
}
}
}
+7 -1
View File
@@ -11847,7 +11847,13 @@
"5f9a0ed7c4": "제목 4",
"01a71dbbdd": "중첩된 섹션 제목.",
"8440fa4acf": "제목 5",
"b287b93c66": "깊은 섹션 제목."
"b287b93c66": "깊은 섹션 제목.",
"7a2c1f9b04": "토글 제목 2",
"b3e5d8a1c6": "중간 제목 요약이 포함된 접을 수 있는 섹션을 만듭니다.",
"2f9d6b4e10": "토글 제목 3",
"8c1a3e7d52": "작은 제목 요약이 포함된 접을 수 있는 섹션을 만듭니다.",
"5e0b9c2a71": "토글 제목 4",
"d4f16a8b39": "중첩된 제목 요약이 포함된 접을 수 있는 섹션을 만듭니다."
}
}
}
+7 -1
View File
@@ -11847,7 +11847,13 @@
"5f9a0ed7c4": "标题 4",
"01a71dbbdd": "嵌套小节标题。",
"8440fa4acf": "标题 5",
"b287b93c66": "深层小节标题。"
"b287b93c66": "深层小节标题。",
"7a2c1f9b04": "切换标题 2",
"b3e5d8a1c6": "创建一个带有中等标题摘要的可折叠部分。",
"2f9d6b4e10": "切换标题 3",
"8c1a3e7d52": "创建一个带有小标题摘要的可折叠部分。",
"5e0b9c2a71": "切换标题 4",
"d4f16a8b39": "创建一个带有嵌套标题摘要的可折叠部分。"
}
}
}