Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playwright/e2e/footnotes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test.describe('renders footnotes', () => {
test('shows reference and footnote from Markdown', async ({ editor, open }) => {
await open()
await expect(editor.getFootnoteReference('bar')).toBeVisible()
await expect(editor.getFootnoteReference('bar').locator('a')).toHaveText('1')
await expect(editor.getFootnoteReference('bar').locator('a')).toHaveCSS('--footnote-ordinal', '1')
await expect(editor.getFootnote('bar')).toContainText('footnote')
})
})
Expand Down
5 changes: 5 additions & 0 deletions src/css/prosemirror.scss
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ div.ProseMirror {
}
}

a.footnote-ref::after, .footnote-label::before {
counter-reset: footnote-ordinal var(--footnote-ordinal);
content: counter(footnote-ordinal);
}

section[data-type="footnotes"] {
display: flex;
flex-direction: column;
Expand Down
8 changes: 1 addition & 7 deletions src/nodes/Footnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ const Footnote = Node.create({
'data-reference-id': attrs.referenceId,
}),
},
ordinal: {
default: 0,
rendered: false,
},
}
},

Expand All @@ -39,15 +35,13 @@ const Footnote = Node.create({

renderHTML({ node, HTMLAttributes }) {
const id = node.attrs.referenceId
const ordinal = node.attrs.ordinal
return [
'div',
mergeAttributes(HTMLAttributes, {
'data-type': 'footnote',
id: `fn-${id}`,
style: `order: ${ordinal || 0}`,
}),
['span', { class: 'footnote-label', contenteditable: 'false', title: id }, String(ordinal || '')],
['span', { class: 'footnote-label', contenteditable: 'false', title: id }],
[
'div',
{ class: 'footnote-body-wrapper' },
Expand Down
6 changes: 0 additions & 6 deletions src/nodes/FootnoteReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ const FootnoteReference = Node.create({
'data-reference-id': attrs.referenceId,
}),
},
ordinal: {
default: 0,
rendered: false,
},
}
},

Expand All @@ -66,7 +62,6 @@ const FootnoteReference = Node.create({

renderHTML({ node, HTMLAttributes }) {
const id = node.attrs.referenceId
const ordinal = node.attrs.ordinal
return [
'sup',
mergeAttributes(HTMLAttributes, {
Expand All @@ -76,7 +71,6 @@ const FootnoteReference = Node.create({
[
'a',
{ href: `#fn-${id}`, class: 'footnote-ref', role: 'doc-noteref', title: id },
String(ordinal || ''),
],
]
},
Expand Down
79 changes: 45 additions & 34 deletions src/nodes/Footnotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { Node as PMNode } from '@tiptap/pm/model'

import { mergeAttributes, Node } from '@tiptap/core'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { Decoration, DecorationSet } from '@tiptap/pm/view'
import Footnote from './Footnote.ts'
import FootnoteReference from './FootnoteReference.ts'

Expand Down Expand Up @@ -39,40 +42,14 @@ const Footnotes = Node.create({
return [
new Plugin({
key: new PluginKey('footnoteOrdinals'),
appendTransaction(transactions, _oldState, newState) {
if (!transactions.some((tr) => tr.docChanged)) {
return null
}

// Compute ordinals
const ordinals = new Map<string, number>()
newState.doc.descendants((node) => {
if (node.type.name === 'footnoteReference') {
const id = node.attrs.referenceId
if (id && !ordinals.has(id)) {
ordinals.set(id, ordinals.size + 1)
}
}
})

const tr = newState.tr
let hasChange = false
newState.doc.descendants((node, pos) => {
if (node.type.name !== 'footnoteReference' && node.type.name !== 'footnote') {
return
}

const targetOrdinal = ordinals.get(node.attrs.referenceId) ?? 0
if (node.attrs.ordinal !== targetOrdinal) {
tr.setNodeAttribute(pos, 'ordinal', targetOrdinal)
hasChange = true
}
})

if (!hasChange) {
return null
}
return tr
state: {
init: (_, { doc }) => footnoteOrdinals(doc),
apply: (tr, value) => (tr.docChanged ? footnoteOrdinals(tr.doc) : value),
},
props: {
decorations(state) {
return this.getState(state)
},
},
}),
new Plugin({
Expand Down Expand Up @@ -144,4 +121,38 @@ const Footnotes = Node.create({
},
})

/**
* Number footnotes by first reference and expose the number as a CSS variable
*
* @param doc the document node
*/
function footnoteOrdinals(doc: PMNode): DecorationSet {
const ordinals = new Map<string, number>()
const nodes: { node: PMNode, pos: number }[] = []
doc.descendants((node, pos) => {
if (node.type.name === 'footnoteReference') {
const id = node.attrs.referenceId
if (id && !ordinals.has(id)) {
ordinals.set(id, ordinals.size + 1)
}
nodes.push({ node, pos })
} else if (node.type.name === 'footnote') {
nodes.push({ node, pos })
}
})

const decorations = []
for (const { node, pos } of nodes) {
const ordinal = ordinals.get(node.attrs.referenceId)
if (!ordinal) {
continue
}
const style = node.type.name === 'footnote'
? `order: ${ordinal}; --footnote-ordinal: ${ordinal}`
: `--footnote-ordinal: ${ordinal}`
decorations.push(Decoration.node(pos, pos + node.nodeSize, { style }))
}
return DecorationSet.create(doc, decorations)
}

export default Footnotes
44 changes: 44 additions & 0 deletions src/tests/nodes/Footnotes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,50 @@ describe('insertFootnote command', () => {
})
})

describe('Footnote ordinals', () => {
function ordinals(editor: Editor, selector: string): string[] {
return [...editor.view.dom.querySelectorAll<HTMLElement>(selector)]
.map((el) => el.style.getPropertyValue('--footnote-ordinal'))
}

test('numbers footnotes by first reference and reuses the number for repeated references', ({ editor }) => {
editor.commands.setContent('<p>A<sup data-type="footnote-reference" data-reference-id="b"></sup>'
+ ' B<sup data-type="footnote-reference" data-reference-id="a"></sup>'
+ ' C<sup data-type="footnote-reference" data-reference-id="b"></sup></p>'
+ '<section data-type="footnotes">'
+ '<div data-type="footnote" data-reference-id="a"><p>a</p></div>'
+ '<div data-type="footnote" data-reference-id="b"><p>b</p></div>'
+ '</section>')

expect(ordinals(editor, 'sup[data-type="footnote-reference"]')).toEqual(['1', '2', '1'])
expect(ordinals(editor, 'div[data-type="footnote"]')).toEqual(['2', '1'])
expect(editor.view.dom.querySelector<HTMLElement>('div[data-type="footnote"]')!.style.order).toBe('2')
})

test('renumbers when a reference is inserted before existing ones', ({ editor }) => {
editor.commands.setContent('<p>Foo<sup data-type="footnote-reference" data-reference-id="1"></sup></p>'
+ '<section data-type="footnotes">'
+ '<div data-type="footnote" data-reference-id="1"><p>x</p></div>'
+ '</section>')
editor.commands.setTextSelection(1)
editor.commands.insertFootnote()

expect(ordinals(editor, 'sup[data-type="footnote-reference"]')).toEqual(['1', '2'])
expect(ordinals(editor, 'div[data-type="footnote"]')).toEqual(['2', '1'])
})

test('does not store ordinals in the document', ({ editor }) => {
editor.commands.setContent('<p>Foo<sup data-type="footnote-reference" data-reference-id="1"></sup></p>'
+ '<section data-type="footnotes">'
+ '<div data-type="footnote" data-reference-id="1"><p>x</p></div>'
+ '</section>')

editor.state.doc.descendants((node) => {
expect(node.attrs).not.toHaveProperty('ordinal')
})
})
})

describe('Footnotes cleanup', () => {
function deleteFirstReference(editor: Editor) {
// Delete reference node
Expand Down
Loading