Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

refactor: improve document handling and validation in knowledge management

Dmitry Ng3cfe5cba4bf9
diff --git a/CLAUDE.md b/CLAUDE.md
index dfe7cbb9..600a5f27 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -22,7 +22,7 @@ The target: comment lines at 0–2% of a file of logic and about 18% of a file o
3. **English only.** No Russian, not even a quoted requirement.
-4. **No `§`, no requirement ids like `(R37)`, no reference to a document that is not in this repository.** A planning document that is not tracked leaves every pointer into it dead. Do not just delete the token — rewrite the sentence so it stands alone, or delete the comment. "The spec says X" is not a reason; if the reason is real, state the reason. (`§` is a legitimate *string* separator in `backend/cmd/installer/wizard/models/types.go` and a legitimate reference to the CommonMark spec in `markdown-editor-marked.ts` — those are not comments.)
+4. **No `§`, no requirement ids like `(R37)`, no reference to a document that is not in this repository.** A planning document that is not tracked leaves every pointer into it dead. Do not just delete the token — rewrite the sentence so it stands alone, or delete the comment. "The spec says X" is not a reason; if the reason is real, state the reason. (`§` is a legitimate *string* separator in `backend/cmd/installer/wizard/models/types.go`, and in `markdown-editor-marked.ts` it is the delimiter of the anonymizer placeholder `§*Name*§` that the code reads — neither is a pointer into a document.)
5. **No archaeology.** Git holds the history. Delete "it used to be", "no longer", "before this", "now that X arrives", "was an overgeneralisation". Keep the invariant the story was there to protect. This applies to test *names* as well as test comments.
diff --git a/backend/pkg/database/knowledge/knowledge.go b/backend/pkg/database/knowledge/knowledge.go
index 7116005a..da20adb1 100644
--- a/backend/pkg/database/knowledge/knowledge.go
+++ b/backend/pkg/database/knowledge/knowledge.go
@@ -502,12 +502,13 @@ func (ks *knowledgeStore) CreateDocument(ctx context.Context, userID int64, inpu
return nil, err
}
- content := strings.TrimSpace(input.Content)
+ content := input.Content
meta.PartSize = len(content)
meta.TotalSize = len(content)
- // Truncate to embedding size limit for vector computation; full content goes to DB.
- embeddingText := content
+ // The vector is computed from the text without the whitespace around it, cut to the embedding size
+ // limit; the document is stored whole, as it was sent.
+ embeddingText := strings.TrimSpace(content)
if len(embeddingText) > ks.maxEmbeddingBytes {
embeddingText = embeddingText[:ks.maxEmbeddingBytes]
}
@@ -645,7 +646,7 @@ func (ks *knowledgeStore) doUpdate(ctx context.Context, userID int64, id string,
meta := metaFromDoc(existing)
// Apply input fields.
- content := strings.TrimSpace(input.Content)
+ content := input.Content
if input.Question != nil {
meta.Question = *input.Question
}
@@ -696,9 +697,9 @@ func (ks *knowledgeStore) doUpdate(ctx context.Context, userID int64, id string,
meta.TotalSize = existing.TotalSize + deltaContentLen
}
- // Compute new embedding. Truncate to maxEmbeddingBytes to avoid token limit
- // errors; the full content is stored in the document column.
- embeddingText := content
+ // Compute new embedding from the text without the whitespace around it. Truncate to
+ // maxEmbeddingBytes to avoid token limit errors; the full content is stored in the document column.
+ embeddingText := strings.TrimSpace(content)
if len(embeddingText) > ks.maxEmbeddingBytes {
embeddingText = embeddingText[:ks.maxEmbeddingBytes]
}
diff --git a/backend/pkg/database/knowledge/knowledge_test.go b/backend/pkg/database/knowledge/knowledge_test.go
index bbeb8189..84eaeaa9 100644
--- a/backend/pkg/database/knowledge/knowledge_test.go
+++ b/backend/pkg/database/knowledge/knowledge_test.go
@@ -849,7 +849,7 @@ func TestKnowledge_DeleteUserDocument_DeletesOnlyTheUsersDocument(t *testing.T)
}
}
-func TestKnowledge_CreateDocument_StoresATrimmedManualDocumentAndAnnouncesIt(t *testing.T) {
+func TestKnowledge_CreateDocument_StoresAManualDocumentAsSentAndAnnouncesIt(t *testing.T) {
for _, tc := range []struct {
name string
maxBytes int
@@ -861,17 +861,17 @@ func TestKnowledge_CreateDocument_StoresATrimmedManualDocumentAndAnnouncesIt(t *
{
name: "code with a description",
input: model.CreateKnowledgeDocumentInput{
- DocType: model.KnowledgeDocTypeCode, Content: " func main() {} ", Question: "how to main?",
+ DocType: model.KnowledgeDocTypeCode, Content: " func main() {}\n", Question: "how to main?",
Description: ptr("a Go main"), CodeLang: ptr("go"),
},
wantEmbedded: "func main() {}",
wantMeta: knowledgeMeta{
DocType: "code", UserID: 11, Question: "how to main?", Description: "a Go main", CodeLang: "go",
- PartSize: 14, TotalSize: 14, Manual: true,
+ PartSize: 17, TotalSize: 17, Manual: true,
},
want: model.KnowledgeDocument{
- DocType: model.KnowledgeDocTypeCode, Content: "func main() {}", Question: "how to main?",
- Description: ptr("a Go main"), UserID: 11, CodeLang: ptr("go"), PartSize: 14, TotalSize: 14, Manual: true,
+ DocType: model.KnowledgeDocTypeCode, Content: " func main() {}\n", Question: "how to main?",
+ Description: ptr("a Go main"), UserID: 11, CodeLang: ptr("go"), PartSize: 17, TotalSize: 17, Manual: true,
},
},
{
@@ -897,12 +897,12 @@ func TestKnowledge_CreateDocument_StoresATrimmedManualDocumentAndAnnouncesIt(t *
{
name: "content past the embedding limit is embedded cut and stored whole",
maxBytes: 4,
- input: model.CreateKnowledgeDocumentInput{DocType: model.KnowledgeDocTypeAnswer, Content: " trimmed ", Question: "q", AnswerType: ptr(model.KnowledgeAnswerTypeTool)},
- wantEmbedded: "trim",
- wantMeta: knowledgeMeta{DocType: "answer", UserID: 11, Question: "q", AnswerType: "tool", PartSize: 7, TotalSize: 7, Manual: true},
+ input: model.CreateKnowledgeDocumentInput{DocType: model.KnowledgeDocTypeAnswer, Content: " padded ", Question: "q", AnswerType: ptr(model.KnowledgeAnswerTypeTool)},
+ wantEmbedded: "padd",
+ wantMeta: knowledgeMeta{DocType: "answer", UserID: 11, Question: "q", AnswerType: "tool", PartSize: 10, TotalSize: 10, Manual: true},
want: model.KnowledgeDocument{
- DocType: model.KnowledgeDocTypeAnswer, Content: "trimmed", Question: "q", UserID: 11,
- AnswerType: ptr(model.KnowledgeAnswerTypeTool), PartSize: 7, TotalSize: 7, Manual: true,
+ DocType: model.KnowledgeDocTypeAnswer, Content: " padded ", Question: "q", UserID: 11,
+ AnswerType: ptr(model.KnowledgeAnswerTypeTool), PartSize: 10, TotalSize: 10, Manual: true,
},
},
} {
@@ -1031,6 +1031,22 @@ func TestKnowledge_DoUpdate_MergesTheInputIntoTheStoredDocument(t *testing.T) {
FlowID: ptr(int64(1)), TaskID: ptr(int64(2)), SubtaskID: ptr(int64(3)), PartSize: 11, TotalSize: 11,
},
},
+ {
+ name: "the content is written as it was sent, the line break it ends with included",
+ content: "old",
+ cmetadata: `{"doc_type":"guide","guide_type":"pentest","question":"q"}`,
+ input: model.UpdateKnowledgeDocumentInput{Content: " # Title\n\ntext\n"},
+ wantEmbedded: "# Title\n\ntext",
+ wantMeta: knowledgeMeta{DocType: "guide", GuideType: "pentest", Question: "q", PartSize: 16, TotalSize: 16},
+ },
+ {
+ name: "the sizes of a stored part move by the bytes of the content as it was sent",
+ content: "old",
+ cmetadata: `{"doc_type":"guide","guide_type":"pentest","question":"q","part_size":3,"total_size":9}`,
+ input: model.UpdateKnowledgeDocumentInput{Content: " # Title\n\ntext\n"},
+ wantEmbedded: "# Title\n\ntext",
+ wantMeta: knowledgeMeta{DocType: "guide", GuideType: "pentest", Question: "q", PartSize: 16, TotalSize: 22},
+ },
{
name: "guide to answer drops the guide type and takes the answer type",
content: "old",
diff --git a/backend/pkg/graph/context.go b/backend/pkg/graph/context.go
index 7f97a78a..5a623d40 100644
--- a/backend/pkg/graph/context.go
+++ b/backend/pkg/graph/context.go
@@ -230,13 +230,15 @@ const (
maxFlowTemplateTextLen = 65536
)
+// The title is one line and is stored trimmed. The text is a document and is stored as it was sent, the line
+// break it ends with included; only one that holds nothing but whitespace is refused.
func validateFlowTemplateFields(title, text string) (string, string, error) {
- title, text = strings.TrimSpace(title), strings.TrimSpace(text)
+ title = strings.TrimSpace(title)
if title == "" {
return "", "", fmt.Errorf("title is required")
}
- if text == "" {
+ if strings.TrimSpace(text) == "" {
return "", "", fmt.Errorf("text is required")
}
if utf8.RuneCountInString(title) > maxFlowTemplateTitleLen {
diff --git a/backend/pkg/graph/context_test.go b/backend/pkg/graph/context_test.go
index 98c7eae4..0b7d4aea 100644
--- a/backend/pkg/graph/context_test.go
+++ b/backend/pkg/graph/context_test.go
@@ -427,7 +427,7 @@ func TestContext_ValidateKnowledgeSearch_BoundsTheLimitAndValidatesTheQuery(t *t
}
}
-func TestContext_ValidateFlowTemplateFields_TrimsAndBoundsTitleAndText(t *testing.T) {
+func TestContext_ValidateFlowTemplateFields_TrimsTheTitleAndKeepsTheTextAsSent(t *testing.T) {
t.Parallel()
longTitle, longText := strings.Repeat("я", 255), strings.Repeat("я", 65536)
@@ -440,13 +440,15 @@ func TestContext_ValidateFlowTemplateFields_TrimsAndBoundsTitleAndText(t *testin
wantText string
wantErr string
}{
- {name: "padded values are stored trimmed", title: " Padded ", text: " body ", wantTitle: "Padded", wantText: "body"},
+ {name: "a padded title is trimmed and a padded text is not", title: " Padded ", text: " body ", wantTitle: "Padded", wantText: " body "},
+ {name: "the line break a text ends with is kept", title: "title", text: "# Plan\n\n- scan\n", wantTitle: "title", wantText: "# Plan\n\n- scan\n"},
{name: "whitespace-only title", title: "\t\n ", text: "body", wantErr: "title is required"},
{name: "whitespace-only text", title: "title", text: " \t\n", wantErr: "text is required"},
{name: "multibyte title at the limit", title: longTitle, text: "body", wantTitle: longTitle, wantText: "body"},
{name: "title one over the limit", title: strings.Repeat("a", 256), text: "body", wantErr: "title must not exceed 255 characters"},
{name: "multibyte text at the limit", title: "title", text: longText, wantTitle: "title", wantText: longText},
{name: "text one over the limit", title: "title", text: strings.Repeat("a", 65537), wantErr: "text must not exceed 65536 characters"},
+ {name: "a final line break counts toward the limit", title: "title", text: longText + "\n", wantErr: "text must not exceed 65536 characters"},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
diff --git a/backend/pkg/graph/schema.resolvers_test.go b/backend/pkg/graph/schema.resolvers_test.go
index 0346c0de..326c66eb 100644
--- a/backend/pkg/graph/schema.resolvers_test.go
+++ b/backend/pkg/graph/schema.resolvers_test.go
@@ -345,12 +345,13 @@ var graphFlowTemplateCases = []struct {
wantErr string
}{
{
- name: "padding is stripped before the row is written",
+ name: "the title is trimmed and the text is written as it was sent",
title: " My template ",
text: "\n scan the host \n",
wantTitle: "My template",
- wantText: "scan the host",
+ wantText: "\n scan the host \n",
},
+ {name: "a blank text is refused before the row is written", title: "My template", text: " \n\t", wantErr: "text is required"},
{name: "a blank title is refused before the row is written", title: " ", text: "scan the host", wantErr: "title is required"},
}
@@ -426,14 +427,17 @@ func TestSchemaResolvers_AMissingFlowTemplateIsNotFound(t *testing.T) {
}
}
-func TestSchemaResolvers_CreateKnowledgeDocument_RefusesABlankQuestion(t *testing.T) {
+func TestSchemaResolvers_CreateKnowledgeDocument_RefusesABlankQuestionOrContent(t *testing.T) {
for _, tt := range []struct {
name string
question string
+ content string
wantErr string
}{
- {name: "an ordinary question is stored", question: "which ports are open?"},
- {name: "a blank question is refused", question: " ", wantErr: "question is required"},
+ {name: "an ordinary question is stored", question: "which ports are open?", content: "nmap reports 22 and 443"},
+ {name: "a blank question is refused", question: " ", content: "nmap reports 22 and 443", wantErr: "question is required"},
+ {name: "a blank content is refused", question: "which ports are open?", content: " \n\t", wantErr: "content is required"},
+ {name: "a padded content reaches the store as it was sent", question: "which ports are open?", content: " 22\n443\n"},
} {
t.Run(tt.name, func(t *testing.T) {
store := &graphKnowledgeStore{}
@@ -441,7 +445,7 @@ func TestSchemaResolvers_CreateKnowledgeDocument_RefusesABlankQuestion(t *testin
r.Knowledge = store
input := model.CreateKnowledgeDocumentInput{
DocType: model.KnowledgeDocTypeAnswer,
- Content: "nmap reports 22 and 443",
+ Content: tt.content,
Question: tt.question,
}
@@ -449,42 +453,47 @@ func TestSchemaResolvers_CreateKnowledgeDocument_RefusesABlankQuestion(t *testin
if tt.wantErr != "" {
require.EqualError(t, err, tt.wantErr)
- assert.Empty(t, store.created, "a refused question reached the store")
+ assert.Empty(t, store.created, "a refused document reached the store")
return
}
require.NoError(t, err)
require.Len(t, store.created, 1)
assert.Equal(t, tt.question, store.created[0].Question)
+ assert.Equal(t, tt.content, store.created[0].Content)
})
}
}
-func TestSchemaResolvers_UpdateKnowledgeDocument_RefusesABlankQuestion(t *testing.T) {
+func TestSchemaResolvers_UpdateKnowledgeDocument_RefusesABlankQuestionOrContent(t *testing.T) {
for _, tt := range []struct {
name string
question *string
+ content string
wantErr string
}{
- {name: "an ordinary question is stored", question: strPtr("which ports are open?")},
- {name: "an omitted question leaves the stored one alone", question: nil},
- {name: "a blank question is refused", question: strPtr(" "), wantErr: "question is required"},
+ {name: "an ordinary question is stored", question: strPtr("which ports are open?"), content: "nmap reports 22 and 443"},
+ {name: "an omitted question leaves the stored one alone", question: nil, content: "nmap reports 22 and 443"},
+ {name: "a blank question is refused", question: strPtr(" "), content: "nmap reports 22 and 443", wantErr: "question is required"},
+ {name: "a blank content is refused", question: nil, content: " \n\t", wantErr: "content is required"},
+ {name: "a padded content reaches the store as it was sent", question: nil, content: " 22\n443\n"},
} {
t.Run(tt.name, func(t *testing.T) {
store := &graphKnowledgeStore{}
r := graphResolver(&graphDB{})
r.Knowledge = store
- input := model.UpdateKnowledgeDocumentInput{Content: "nmap reports 22 and 443", Question: tt.question}
+ input := model.UpdateKnowledgeDocumentInput{Content: tt.content, Question: tt.question}
_, err := r.Mutation().UpdateKnowledgeDocument(graphUserContext(1, "knowledge.edit"), "doc-1", input)
if tt.wantErr != "" {
require.EqualError(t, err, tt.wantErr)
- assert.Empty(t, store.updated, "a refused question reached the store")
+ assert.Empty(t, store.updated, "a refused document reached the store")
return
}
require.NoError(t, err)
require.Len(t, store.updated, 1)
assert.Equal(t, tt.question, store.updated[0].Question)
+ assert.Equal(t, tt.content, store.updated[0].Content)
})
}
}
diff --git a/backend/pkg/server/services/knowledge_test.go b/backend/pkg/server/services/knowledge_test.go
index 1a6893e4..3d465e0a 100644
--- a/backend/pkg/server/services/knowledge_test.go
+++ b/backend/pkg/server/services/knowledge_test.go
@@ -67,3 +67,71 @@ func knowledgeAnswerToAnUnstorableDocument(t *testing.T, handler func(*Knowledge
assert.Equal(t, http.StatusBadRequest, w.Code, "the caller sent a document the door refuses, not a server fault")
assert.Equal(t, "Knowledge.InvalidRequest", resp.Code)
}
+
+// recordingKnowledgeStore keeps what the door handed it, and hands nothing back once its context is done.
+type recordingKnowledgeStore struct {
+ knowledgepkg.KnowledgeStore
+ contents []string
+}
+
+func (s *recordingKnowledgeStore) record(ctx context.Context, content string) (*gqlmodel.KnowledgeDocument, error) {
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ s.contents = append(s.contents, content)
+ return &gqlmodel.KnowledgeDocument{ID: "doc", DocType: gqlmodel.KnowledgeDocTypeAnswer, Content: content}, nil
+}
+
+func (s *recordingKnowledgeStore) CreateDocument(
+ ctx context.Context, _ int64, input gqlmodel.CreateKnowledgeDocumentInput,
+) (*gqlmodel.KnowledgeDocument, error) {
+ return s.record(ctx, input.Content)
+}
+
+func (s *recordingKnowledgeStore) UpdateUserDocument(
+ ctx context.Context, _ int64, _ string, input gqlmodel.UpdateKnowledgeDocumentInput,
+) (*gqlmodel.KnowledgeDocument, error) {
+ return s.record(ctx, input.Content)
+}
+
+func TestKnowledge_CreateDocument_RefusesABlankContentAndStoresTheRestAsSent(t *testing.T) {
+ knowledgeContentAtTheDoor(t, (*KnowledgeService).CreateDocument, http.StatusCreated,
+ `{"doc_type":"answer","answer_type":"other","question":"q","content":%s}`)
+}
+
+func TestKnowledge_UpdateDocument_RefusesABlankContentAndStoresTheRestAsSent(t *testing.T) {
+ knowledgeContentAtTheDoor(t, (*KnowledgeService).UpdateDocument, http.StatusOK, `{"content":%s}`)
+}
+
+func knowledgeContentAtTheDoor(t *testing.T, handler func(*KnowledgeService, *gin.Context), stored int, body string) {
+ t.Helper()
+ gin.SetMode(gin.TestMode)
+
+ for _, tc := range []struct {
+ name string
+ content string
+ wantStatus int
+ wantStored []string
+ }{
+ {name: "a content of whitespace is refused before the store", content: " \n\t", wantStatus: http.StatusBadRequest},
+ {name: "a padded content reaches the store as it was sent", content: " 22\n443\n", wantStatus: stored, wantStored: []string{" 22\n443\n"}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ store := &recordingKnowledgeStore{}
+ content, err := json.Marshal(tc.content)
+ require.NoError(t, err)
+
+ w := httptest.NewRecorder()
+ c, _ := gin.CreateTestContext(w)
+ c.Set("uid", uint64(7))
+ c.Params = gin.Params{{Key: "id", Value: "doc"}}
+ c.Request = httptest.NewRequest(http.MethodPost, "/knowledge/", strings.NewReader(fmt.Sprintf(body, content)))
+ c.Request.Header.Set("Content-Type", "application/json")
+
+ handler(NewKnowledgeService(nil, store), c)
+
+ assert.Equal(t, tc.wantStatus, w.Code, w.Body.String())
+ assert.Equal(t, tc.wantStored, store.contents)
+ })
+ }
+}
diff --git a/frontend/docs/live-testing.md b/frontend/docs/live-testing.md
index c64633b8..a5779cef 100644
--- a/frontend/docs/live-testing.md
+++ b/frontend/docs/live-testing.md
@@ -576,6 +576,30 @@ was found" by comparing its answer with the current body — is comparing agains
marked-up form. A case built on the typed text instead of the stored one tests a
branch that cannot be reached.
+**What was not touched is saved as it was written.** A body that was loaded keeps its
+own layout through a save: the blank lines between its blocks and how many there were,
+the spaces on a blank line, the bullet character, the numbers and the leading spaces of
+its lists, every row of a table as the line it was (its padding, its pipes — bare or escaped
+inside a code span — and a line of text written right under the table), a code block as it
+stood (its fence, its indentation, a block written by indentation and not by a fence),
+the fence of a code span, a bare URL, the link around an image, the underline of a setext
+heading, the spaces a heading or a list ended with, a line break that is not a hard break.
+A case that types one character and compares the stored body with the original should find
+that character and nothing else outside the block it was typed into; inside that block
+the editor writes its own form — a table row with one space around each cell where the table
+was written that way and padded to its column otherwise, `\|` for a pipe in a code span of that
+row, a code block between fences or four spaces in. What is kept gives way wherever it would be
+read as something else: under a list, a table or a fenced block written a few spaces in goes
+to the margin, text goes there when the list would take it for its last item's, and inside a list
+item a code block is written between fences at the item's column. The body is stored as it was sent,
+the line break it ends with included: neither the forms nor the backend trim it, and only
+the single-line fields (title, question, description, code language) are trimmed. A Go template
+action `{{ … }}` is literal text to the editor — nothing inside it is read as markdown and a pipe
+in it is never escaped — and a line that opens with a control action (`{{ range }}`, `{{ end }}`)
+right under a table is the template's line, not a row. What still differs is not a defect of such a case:
+a link typed into the editor, as opposed to one that was loaded, is stored as `[url](url)`;
+a named HTML entity outside code and outside an action is decoded (`&` becomes `&`);
+a list item that opens with indented code is written with a fence; and a document with CRLF line endings is saved with LF.
A template's body, a knowledge document's content and both halves of a prompt are
the same component. The document tells you to "edit" those entities; this is what
diff --git a/frontend/e2e/mocks/cassettes/templates.ts b/frontend/e2e/mocks/cassettes/templates.ts
index 95e148bb..61a073d9 100644
--- a/frontend/e2e/mocks/cassettes/templates.ts
+++ b/frontend/e2e/mocks/cassettes/templates.ts
@@ -41,6 +41,7 @@ export const RICH_TEMPLATE_TEXT = [
'| Field | Value |',
'| --- | --- |',
'| Scope | {{SCOPE}} |',
+ '',
].join('\n');
export const TEMPLATE_DETAIL = makeTemplate('11', 'E2E Seed Template', RICH_TEMPLATE_TEXT);
diff --git a/frontend/e2e/specs/crud/template-detail.spec.ts b/frontend/e2e/specs/crud/template-detail.spec.ts
index 5ed581cc..5b1ae245 100644
--- a/frontend/e2e/specs/crud/template-detail.spec.ts
+++ b/frontend/e2e/specs/crud/template-detail.spec.ts
@@ -106,6 +106,7 @@ test.describe('template detail', { tag: '@coverage' }, () => {
expect(variables.templateId).toBe(TEMPLATE_DETAIL.id);
expect(variables.input.text).toContain('E2E-SAVE-MARK');
+ expect(variables.input.text, 'the line break the body ends with is sent').toMatch(/[^\n]\n$/);
for (const atom of ['# Recon', '{{TARGET}}', 'nmap -sV']) {
expect(variables.input.text, `"${atom}" survived the save`).toContain(atom);
diff --git a/frontend/package.json b/frontend/package.json
index 9987f4f3..ce36b4b2 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -47,16 +47,16 @@
"@react-pdf/renderer": "^4.5.1",
"@tanstack/react-table": "^8.21.3",
"@tanstack/react-virtual": "^3.14.3",
- "@tiptap/core": "^3.27.1",
- "@tiptap/extension-code-block-lowlight": "^3.27.1",
- "@tiptap/extension-image": "^3.27.1",
- "@tiptap/extension-list": "^3.27.1",
- "@tiptap/extension-table": "^3.27.1",
- "@tiptap/extensions": "^3.27.1",
- "@tiptap/markdown": "^3.27.1",
- "@tiptap/pm": "^3.27.1",
- "@tiptap/react": "^3.27.1",
- "@tiptap/starter-kit": "^3.27.1",
+ "@tiptap/core": "3.31.3",
+ "@tiptap/extension-code-block-lowlight": "3.31.3",
+ "@tiptap/extension-image": "3.31.3",
+ "@tiptap/extension-list": "3.31.3",
+ "@tiptap/extension-table": "3.31.3",
+ "@tiptap/extensions": "3.31.3",
+ "@tiptap/markdown": "3.31.3",
+ "@tiptap/pm": "3.31.3",
+ "@tiptap/react": "3.31.3",
+ "@tiptap/starter-kit": "3.31.3",
"@xterm/addon-fit": "^0.11.0",
"@xterm/addon-search": "^0.16.0",
"@xterm/addon-unicode11": "^0.9.0",
@@ -75,7 +75,7 @@
"lowlight": "^3.3.0",
"lru-cache": "^11.5.1",
"lucide-react": "^1.20.0",
- "marked": "^18.0.5",
+ "marked": "18.0.5",
"motion": "^12.40.0",
"react": "^19.2.7",
"react-day-picker": "^10.0.1",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index 14351a9b..8c05fef2 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -6,7 +6,7 @@ settings:
overrides:
js-yaml: ^4.3.1
- marked: ^18.0.5
+ marked: 18.0.5
importers:
@@ -88,35 +88,35 @@ importers:
specifier: ^3.14.3
'@tiptap/core':
- specifier: ^3.27.1
- version: 3.27.1(@tiptap/[email protected])
+ specifier: 3.31.3
+ version: 3.31.3(@tiptap/[email protected])
'@tiptap/extension-code-block-lowlight':
- specifier: ^3.27.1
+ specifier: 3.31.3
'@tiptap/extension-image':
- specifier: ^3.27.1
- version: 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
+ specifier: 3.31.3
+ version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
'@tiptap/extension-list':
- specifier: ^3.27.1
- version: 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ specifier: 3.31.3
+ version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/extension-table':
- specifier: ^3.27.1
- version: 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ specifier: 3.31.3
+ version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/extensions':
- specifier: ^3.27.1
- version: 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ specifier: 3.31.3
+ version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/markdown':
- specifier: ^3.27.1
- version: 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ specifier: 3.31.3
+ version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/pm':
- specifier: ^3.27.1
- version: 3.27.1
+ specifier: 3.31.3
+ version: 3.31.3
'@tiptap/react':
- specifier: ^3.27.1
+ specifier: 3.31.3
'@tiptap/starter-kit':
- specifier: ^3.27.1
- version: 3.27.1
+ specifier: 3.31.3
+ version: 3.31.3
'@xterm/addon-fit':
specifier: ^0.11.0
version: 0.11.0
@@ -172,7 +172,7 @@ importers:
specifier: ^1.20.0
version: 1.20.0([email protected])
marked:
- specifier: ^18.0.5
+ specifier: 18.0.5
version: 18.0.5
motion:
specifier: ^12.40.0
@@ -2197,180 +2197,181 @@ packages:
peerDependencies:
'@testing-library/dom': '>=7.21.4'
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-rV6Qn4wmC6BxfF+4mu6bqGWj9vA4oXXhsrpXaJL2uhjxeHAGofjwcHof2X84VYzeyXgdlsGmqKie4TAppVXZUQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-Cz50pvciQrxdSxgTkHOVz0uD0Yl/8Xt0QatGD6ILm47jW8EzyHR9RkUGs/D5IqzXKuVPntfw1ttaT926vXfiRg==}
peerDependencies:
- '@tiptap/pm': 3.27.1
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-VMF7xJx6qEGiX6DTKNiL31NLqypOcd/4sNjFSe8rb41PwejBJh/nOqVIbBvWkiT6NMGFLxMhj7zJ8/zPo1hXeg==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-fyY2XMbyDDDfOTQ1Qdrnqa1qwC9DWE4n7AfE0EKQI0G8MfLV8RaDlLDcOZDJ9JbMPY7/Gx7EjyK4NKxSW9n2hQ==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-TlC5bsS+pqETTrlz4CZz9RO/cKBYtELGIxwtKeivUn3eNfnOxQbbu4WDsiwIfzRFyd0OMnKl6BPM2KnYEehoEQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-dIuYhKk8TitKU/FeDpoTeWZhU42YgDN5npgWNjAmMmRktPdoxnH3/wGSiwXlqZgJWjehN7kPWDePWpJeAImGpQ==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-j/j8Qp9Z5nViade2m7zjrO/CYH/Ca80Qj7aqo0eUaei6FZQ5izlF9o4XQU5EFMAutV6mwynsPUp8FVo5sCuYfw==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-EV6ZnwKc++2OM/OcD54n8s1B7C9LP7GKAtdEPwu0t3BYJf3sG8Ueoinf7SgGPO9oMPg96GneOkDNm1urMV167g==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-faCUHnRP47o9Zh9VZZX6EX/569udw9Vopm2PgEKPWuKLE2qaS5WBuUVU0iItdJmKUqaWiOZkpoW4jvnDmj0dfg==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-qEyyoPapPef4LO8XKaN83bxtaNzkJ4kFn/IxLnEKd4BJ3Mvi4MH2yJYlyDqwFnMoev4pMA1zBHRAt/C0THRmZw==}
peerDependencies:
- '@tiptap/extension-list': 3.27.1
+ '@tiptap/extension-list': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-rDZYDwKpV3Uw8xjI5pzSQTLJ7pEyJkDrVhnzdh9mBY3sHVwP1maYh+0kRUrHi5N3EhiNp/GXG1kPjysPIQ4nqQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-DN21CYEL4bm01vyB/wiyDPKrpiyalI5okTWx90jRuTqRzbnkPlnng1Vdo9L220w3GXct5Exe6iI01E7F19bC5A==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/extension-code-block': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/extension-code-block': 3.31.3
+ '@tiptap/pm': 3.31.3
highlight.js: ^11
lowlight: ^2 || ^3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-pHlzmZx2OlHfyQ0yRlT5UL4mGokz947DthZuYefN1OleVqOkHpWBG+2JQwqoNq6bmzMne92zbH32rhcJUEYSjA==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-nvknt4FhyJQjYcvxptmeUlFsIAc8ibua3E5BN4Pim374/9RWepH4cdE9X0/qUTtguHElLx0iKtSIY3rW2qGTFA==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-epOUpFfEmBzjvnqvjv2qHX7NAuLo5dlOGV690lWu+sAYMjibuJBeVvAiKPyFCfRCCTUxdbDB3jbaOA1yEcEJ7w==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-SzxOqchrD2AcN3uT67PjKmRFEMOU3vNNiwNaamZJUbZrI6Hmy+bvdHJrc3jrIddyyCrAtsnAfRI0cmorW1jGfg==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-8FbBTkfnRP4iVaoj+2h3iWa+H0eGDD3yTyVCwrmue/sQTkqUNUoSuAZa3GDG4Sd41xdPwTJxl9nUWGgM1qDCnw==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-EexgmqnyDNyGlISxo7SMrp5MygpJYmqD+0cY5jB6L1U6L4CpKKRWUt8OO1sWzEHDE1+TTvwt+WIFoIWAziOtEA==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-blFf9x9RG0Qr7P3FoAH/033ffa+mMLZn34trVs8Vi0Ppk6FmJAg5HpYFOtmYoeREdNDJ5rHJKV7SoACbOHgskQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-NWomSfu5CSC7VacnMSDzKT8qm66SzMfZwVPEtwY5bPpRTJgTiT1rNK0neDrrzfMN27MfylGyKWWf7Q5Qf8w/fg==}
peerDependencies:
- '@tiptap/extensions': 3.27.1
+ '@tiptap/extensions': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-BmJF1VqB7dSJkgAalrpVFj88WLhxKjcWPuWHOqf2ITrUU2832BhKLXKmxjWUy1gqV8PfNNVWtGfIERy7I0y0+Q==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-rd4VJ9PGSP9Eop8ZTEwaLZcMzMXLuJKe3hUNf58rq+zANpwM+9fI+vB7g9MAc3eXwUNDxNDVACFIL2oeBqpQ3A==}
peerDependencies:
'@floating-ui/dom': ^1.0.0
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-QoezN0wdvXIwLQ4ee2ccWDaX3RG0lzgQpIMpMz55oPDhpUVax1+19ApsS53LkcktpS4EbnPL4xO4DaJk0Vp7PQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-EBXKb1FrVStsNYCcRGtd9jmzveCvR+eqgg1rVqoONrqFK6U7bga6LN+1dMKroP1kliDVgveYkP3vRYxqw+rFqg==}
peerDependencies:
- '@tiptap/extensions': 3.27.1
+ '@tiptap/extensions': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-iv/m9hzl6jfSj9Q8UEjAxONvCoUDaP7M9SRCPx3PaLNxA230TTD6RE0Ye4zFJ8ze7ZVoJJMAqg9Qpq1iYg2JOQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-QAdCvNO4+yW9ATwsrej11NTkDYFqPLIEQr3ARNrKOK1qaiS7A0fia2SEukb/hrkP3A6mbozhoQt2r2RGUf/DpQ==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-SrC4l1kEIyv9ZXFaI/8LQqU2MyMmjczw7XXsWUQOTN4YXv0JyVgMNR3cI/wz0d2xsTfBdZ1N85Tdng+Ga1t0Sg==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-rk5VHMAeQcg06SLauN6EGdD2jc0O2qY8QkZYPd0LxNvLblw2BxBx+lxUQSYwLAT9Ie5914gKIK2YbRyO2Ts3ig==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-QlKE7qn5qMnIGVGhXQlvYedvLtNJ9z0dmit5w8vPb8tKzW4Spk6M7N2kruprrDA8GBwHfeR5wmF+njfUm34qxg==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-YnHGy2KShRwvCseAmmxl9VP7R0qaj8QMp3DA6DJWZqp7r5gLGvDkAqhedxqqefqsE4Y43hjkBdjtB9Ce78LkIw==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-+JTahgQT+NxiGjduaB3qJVyhU/wh4m3pVkht1Earioku2bm/apj5Lb8rSowa/NJYP3B+oQgV/V4YLw5dtDgBoA==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-wWNG9BOtx2Cg4vwxPVGDIJ5DGX+ETkldeoaFJLB1NXUL4OPUiVTf8cHZ+hdYgJWP704BEB7jQgjlpvdi6VigTg==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-jGGeyn9uRUnNjSTHpbqhiGsp6KaYTSbV09jDXPJI9cDwfV9hpugLvpaCZd0BMBbhU1B1W6kOfX0BE15qX/HQfA==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-ibGvdvAPyfxBMUVNRI43eb9h2/Jka1MRG5GtnGqbcCX/2+Y/y0EOfFrPQPkuYphiWQYyu9+FzPKMB/pjuaLuKQ==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-/2jBfsxBZUDGJmpZifqRQPz7f1E5qpS1BckTZ39TADzUJX+feKy7RJ3DtQ02+8y6SSMzvP9loGVjrk6zEMTk4g==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-986wOQzTL9Zr5lf84LCLpm+YOms8A0K39/8DVoqRfebqcOe0/eq4bnztmAlfabOd+kJY92g3AgZERFUx/w+dcw==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-zwRl01ETfCkWUvtvK5fw9bXtAajMPkvlkE3Cq6JvH3LF7XXJwDtNj5Tj7exacMpCaSZmlNc43vFb2rAYnrnwMA==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-4QlKOriJJMvg95QJTEsy9BUYPBQ6UvJyb8WURRwdUtQUkkqb8h32lg/eyQUv2FzW9IT1AdUyNZfVaxH64kRfQA==}
peerDependencies:
- '@tiptap/extension-list': 3.27.1
+ '@tiptap/extension-list': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-OIMZNlzPSO8WRd4ic73Fxckzl4N1tesjjLL2XApaNA/uMpO0LoF6WSRPAWv+Z24Wp92ARRJAnRP7iZoI5+Jxig==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-If8UOEdDZbPJU6iYTvLtH6DOp2KBy6BKxg9UELL1AevVetGHEF/7lW8hP50Gn1tHMVpPRqmhSzVrJRpEJJgb/Q==}
peerDependencies:
- '@tiptap/extension-list': 3.27.1
+ '@tiptap/extension-list': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-c2Upru7lj0/ZV/Ibww6cNz6sUS8m6Dp/9uygFhYcZOd3X8M0xBIEk42c6m6SQehkPziVA8QOgNJz7sMqsbz1OQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-LoveGnC0FVdCV4jUNBaG1ZA+KWE07+adzV3kGy6uUYFcJEjbVUHTnPDrBOob3IwOSO3sCwIkvQb6EVYeXn/4yg==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-GYrKqD//9nHJ2r80uXqbDMzRnFpGzbaEQRTSGaO/SH7DvXWFMow8evkOdjQ7PCQO07jNjJo75+A85Jwu3Ov3AA==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-mp3g11NgA/PYu8rj7J7Ez3l4qBy6WfTSmHIG4PZvEGG5w2oUAIkgb9DU7nPPzjmeme27oazFYZw+AtZA0+u4tw==}
peerDependencies:
- '@tiptap/extension-list': 3.27.1
+ '@tiptap/extension-list': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-7K7eo1gruOgAsnbK+GCV23AUVUI0cL1bTig8HaPneoFMVbig7vddk8jNLKBWO8TXVbG7TuHdnDN4F98vdtwh5Q==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-+iPku7wJfy5hbNDNLX8dveFtYVsZMmh7vztjuq8hT3mipSC4IByDehDbU8fzUCjfXiEzmI7mQn7c8LGmHULuzA==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-Y3DW1jlSlCNCyMGHP3+3qBNNPS83wuFz4RTYGjZtvRRTCRh7apZme9XRWMq1rN5mJ2Cr7fKocA2/5Bs13KgN6Q==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-G29bhKttYwcKHT+BI6emWVFol3RO/gUXxQVcmr/iT8LXXy7j8J6HFUnsKM+Kg5YlP1rxMRgsa65dbrQVahZi0A==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-tNB8kjxo0+XPremnWkd6NUikpeJ+JQrss7HntL8GgIxX4t0gkdnLn9yUWb0JzcaYP7Y8iTZZHdkPs0P154DG8Q==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-7cnVPHhdiGGeauYqca6JVyPLTqZbqFEEk9nn2e2E8+fBo6zVtV07AktJBqth/XEzjZxcUmGxjoeuWYAisWjUHg==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-6ZwaZwSrDh+KFFv6V1J79oO37yPs7y1bFxvk1/9Ih2rn3Xr5AWz+eMS+n8RpH3djBVVAQpdIAeYQgcn+VCSsTg==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-gdsWtF+taeaCu6V+5Ct10fGo0ACUy1GnYtbb+mcathBt8OqbT+Ws60p/yEmKesBDz2Hn+B5IcWy6+2BBZl5ZTg==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-N889J4nXN/TPfVt8uF9N1A0SY82E90zwc1y26lqOcw6KWNLmQrlhMh/9OD4ikLDbekmFpOBq/UicpHf/6S8hbQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-HghdJaOwRqYzsAxqSyNyb+IWyOMcdCl8IoiBETA9BZCJAqdXzFLcuWp7CqCqJPam9dgkWosSoHqTCAO4nTBfpw==}
peerDependencies:
- '@tiptap/core': 3.27.1
+ '@tiptap/core': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-1Tdx9faw8k0/83V6X+xCDVhV8yElGt95JxeW3YMkKQJI56QdlPz0xOdJPlMiSGJKinPyVier+x9LJD/YZUZIaw==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-8sJNPGGUe8f3aDojcOW5cfVL7I5NrBbE0UWxG08qoi9Tea6qWbvQJsCR9tsrOapr/DaLr3kpbGZ9s1gEEfcNcA==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-4m2LZcj/uN0uLlGnXr3i7sfdxbQNNmeMn4wFyFrP2xpshcKPL5WujUAcqT2rgKfaDjKQ8gIK/GpgSQKuRENFwg==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-rBbYSxasseUoaBo15C8Yoaqafo7mJJzxwAwV9Z1OpLBvOroW9nQ0EbOdqaSYRw3/d9pP71B/3LNSzKyfyy+dpQ==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-Ffjx+vimmBU7zH/KrpXzJid3+pziCe/VL2aexSTP63cyQwKQ65LkFkCKaIsSpFdQQuakVZBGWjCA5RoBV852pw==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-sZime0SWsz/k62W2WvHx5Ig7G2h7kVhrrmnqy+wEgIHfDwEfOlelRjaWCiBCFlF7dxGUntJusCh9FxlLhni0Ag==}
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-/Wn2fc9zMtX08MXYScDFsm4wJ8lzfhfPEdbtls7WCDlbtrop48PWlkHDBBJrywARfAQTB2mFs9KiFy9yrQm5Lg==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-QiwQqvaLFLm5EMFu5tg7nAgXJxCUiUTLD8EsK+TqVV5P4bqOoMOCM39khbhXTJyahCuYpiFWx5YOSDtC/JiPtg==}
peerDependencies:
- '@tiptap/core': 3.27.1
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3
+ '@tiptap/pm': 3.31.3
'@types/react': ^17.0.0 || ^18.0.0 || ^19.0.0
'@types/react-dom': ^17.0.0 || ^18.0.0 || ^19.0.0
react: ^17.0.0 || ^18.0.0 || ^19.0.0
react-dom: ^17.0.0 || ^18.0.0 || ^19.0.0
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-vfxRsqW8rCc0k4pzo0ilU3wobVi2wqVj88VZI2SlgZlNnUAkrDGDIAph7CTa9k9fshV+O1ivpEgPC5yC046jow==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-WKof9RewdmGHvWJ1wn0/HVNG2mV+HOgVRyJkKekuM9fgr6BZAAH/xZsWE1eon+94JnQ+KtK2ThydXQM/qc6b2A==}
resolution: {integrity: sha512-RoBvJ2X0wuKlWFIjrwffGw1IqZHKQqzIchKaadZZfnNpsAYp2mM0h36JtPCjNDAHGgYez/15uMBpfGwchhiMgg==}
@@ -4694,8 +4695,8 @@ packages:
resolution: {integrity: sha512-4HucRlpiLd1IPQQXNqeo81BGtkY8Ai5smHhKW9jjPKRc2wQIxksg7Hl1tTI2IfT2B/LgX6bfYvXxEpJl7aKYKw==}
- resolution: {integrity: sha512-pRTklkDDMMRopyoAcrr9wV/8g/RYgrLHBuJAb5hlEuYZRdm5yqmPjWId83fpBwPpSFqEdja0H7Dfd7z1X/npcA==}
+ resolution: {integrity: sha512-Ue2gTmXMa7EhpLNhC7J+h4+ykD8ha12K6rrZFFKKJHBForfIStw5gJ6Zrf1mqqAa9NmpmEB4wp9bKmA4eUVjWg==}
resolution: {integrity: sha512-927lFx/uwyQaGwJxLWCZRkjXG0p48KpMj6ueoYiu4JX05GGuGcgzAy62dfiV8eFZftgyBUvLx76RsMe20fJl+Q==}
@@ -4709,8 +4710,8 @@ packages:
resolution: {integrity: sha512-GxboyN4AMIsoHNtz5uf2r2Ru551i5hWeCMD6E2Ib4Eogqoub0NflniaBPVQ4MrGE5yZ8JV9tUHg9qcZTTrcN4w==}
- resolution: {integrity: sha512-clTunTX+eaLbr87L1V1QPheRlEQJyTlL3gXe9x3jQIk3rL0RVWxviDGz8tFaydwIVm+hKhYCyr+R/zBtWr9s6A==}
+ resolution: {integrity: sha512-3g25f6hKV2bBxo3puH3EcYWmmVNSH0iWmNsMVT7sugDSpA025jgpOLarcDWr39SdzPjYMaiYtRdNtXialPSjEQ==}
resolution: {integrity: sha512-cJ+oHTW1VAEa8cJslgmUZrc+sjRKgAKl3Zyse6+PV38hZe/V6Z14TbCuXcan9F9ghlz4QrFr2c92TNF82UkYHA==}
@@ -7595,142 +7596,143 @@ snapshots:
dependencies:
'@testing-library/dom': 10.4.1
dependencies:
- '@tiptap/pm': 3.27.1
+ '@tiptap/pm': 3.31.3
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
'@floating-ui/dom': 1.7.6
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
optional: true
dependencies:
- '@tiptap/extension-list': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/extension-code-block': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/extension-code-block': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
highlight.js: 11.11.1
lowlight: 3.3.0
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/extensions': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extensions': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
dependencies:
'@floating-ui/dom': 1.7.6
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
optional: true
dependencies:
- '@tiptap/extensions': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extensions': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
linkifyjs: 4.3.3
dependencies:
- '@tiptap/extension-list': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
dependencies:
- '@tiptap/extension-list': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
dependencies:
- '@tiptap/extension-list': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
marked: 18.0.5
- '@tiptap/[email protected]':
+ '@tiptap/[email protected]':
dependencies:
prosemirror-changeset: 2.4.1
prosemirror-commands: 1.7.1
@@ -7739,17 +7741,17 @@ snapshots:
prosemirror-history: 1.5.0
prosemirror-inputrules: 1.5.1
prosemirror-keymap: 1.2.3
- prosemirror-model: 1.25.9
+ prosemirror-model: 1.25.12
prosemirror-schema-list: 1.5.1
prosemirror-state: 1.4.4
prosemirror-tables: 1.8.5
prosemirror-transform: 1.12.0
- prosemirror-view: 1.41.9
+ prosemirror-view: 1.42.6
dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
'@types/react': 19.2.17
'@types/react-dom': 19.2.3(@types/[email protected])
'@types/use-sync-external-store': 0.0.6
@@ -7758,37 +7760,37 @@ snapshots:
react-dom: 19.2.7([email protected])
use-sync-external-store: 1.6.0([email protected])
optionalDependencies:
- '@tiptap/extension-bubble-menu': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
- '@tiptap/extension-floating-menu': 3.27.1(@floating-ui/[email protected])(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-bubble-menu': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-floating-menu': 3.31.3(@floating-ui/[email protected])(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
transitivePeerDependencies:
- '@floating-ui/dom'
- '@tiptap/[email protected]':
- dependencies:
- '@tiptap/core': 3.27.1(@tiptap/[email protected])
- '@tiptap/extension-blockquote': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-bold': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-bullet-list': 3.27.1(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
- '@tiptap/extension-code': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-code-block': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
- '@tiptap/extension-document': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-dropcursor': 3.27.1(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
- '@tiptap/extension-gapcursor': 3.27.1(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
- '@tiptap/extension-hard-break': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-heading': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-horizontal-rule': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
- '@tiptap/extension-italic': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-link': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
- '@tiptap/extension-list': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
- '@tiptap/extension-list-item': 3.27.1(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
- '@tiptap/extension-list-keymap': 3.27.1(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
- '@tiptap/extension-ordered-list': 3.27.1(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
- '@tiptap/extension-paragraph': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-strike': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-text': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-underline': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extensions': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
- '@tiptap/pm': 3.27.1
+ '@tiptap/[email protected]':
+ dependencies:
+ '@tiptap/core': 3.31.3(@tiptap/[email protected])
+ '@tiptap/extension-blockquote': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-bold': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-bullet-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
+ '@tiptap/extension-code': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-code-block': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-document': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-dropcursor': 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
+ '@tiptap/extension-gapcursor': 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
+ '@tiptap/extension-hard-break': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-heading': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-horizontal-rule': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-italic': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-link': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-list-item': 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
+ '@tiptap/extension-list-keymap': 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
+ '@tiptap/extension-ordered-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
+ '@tiptap/extension-paragraph': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-strike': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-text': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-underline': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extensions': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/pm': 3.31.3
dependencies:
@@ -10405,7 +10407,7 @@ snapshots:
dependencies:
- prosemirror-model: 1.25.9
+ prosemirror-model: 1.25.12
prosemirror-state: 1.4.4
prosemirror-transform: 1.12.0
@@ -10413,20 +10415,20 @@ snapshots:
dependencies:
prosemirror-state: 1.4.4
prosemirror-transform: 1.12.0
- prosemirror-view: 1.41.9
+ prosemirror-view: 1.42.6
dependencies:
prosemirror-keymap: 1.2.3
- prosemirror-model: 1.25.9
+ prosemirror-model: 1.25.12
prosemirror-state: 1.4.4
- prosemirror-view: 1.41.9
+ prosemirror-view: 1.42.6
dependencies:
prosemirror-state: 1.4.4
prosemirror-transform: 1.12.0
- prosemirror-view: 1.41.9
+ prosemirror-view: 1.42.6
rope-sequence: 1.3.4
@@ -10439,37 +10441,37 @@ snapshots:
prosemirror-state: 1.4.4
w3c-keyname: 2.2.8
dependencies:
orderedmap: 2.1.1
dependencies:
- prosemirror-model: 1.25.9
+ prosemirror-model: 1.25.12
prosemirror-state: 1.4.4
prosemirror-transform: 1.12.0
dependencies:
- prosemirror-model: 1.25.9
+ prosemirror-model: 1.25.12
prosemirror-transform: 1.12.0
- prosemirror-view: 1.41.9
+ prosemirror-view: 1.42.6
dependencies:
prosemirror-keymap: 1.2.3
- prosemirror-model: 1.25.9
+ prosemirror-model: 1.25.12
prosemirror-state: 1.4.4
prosemirror-transform: 1.12.0
- prosemirror-view: 1.41.9
+ prosemirror-view: 1.42.6
dependencies:
- prosemirror-model: 1.25.9
+ prosemirror-model: 1.25.12
dependencies:
- prosemirror-model: 1.25.9
+ prosemirror-model: 1.25.12
prosemirror-state: 1.4.4
prosemirror-transform: 1.12.0
diff --git a/frontend/pnpm-workspace.yaml b/frontend/pnpm-workspace.yaml
index 1935081e..966110ad 100644
--- a/frontend/pnpm-workspace.yaml
+++ b/frontend/pnpm-workspace.yaml
@@ -1,6 +1,6 @@
overrides:
js-yaml: ^4.3.1
- marked: ^18.0.5
+ marked: 18.0.5
allowBuilds:
'@swc/core': true
esbuild: true
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-code-fence.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-code-fence.test.ts
index d90dfe77..9285200b 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-code-fence.test.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-code-fence.test.ts
@@ -1,8 +1,10 @@
import { Editor } from '@tiptap/core';
import { beforeAll, describe, expect, it } from 'vitest';
+import { GROWTH_IF_QUADRATIC, slowdownWhenInputQuadruples } from '@/test-utils/cost-growth';
+
import { createMarkdownExtensions } from './markdown-editor-extensions';
-import { roundTrip, setupEditorJsdom } from './markdown-editor-test-setup';
+import { markdownCodec, roundTrip, setupEditorJsdom } from './markdown-editor-test-setup';
beforeAll(setupEditorJsdom);
@@ -24,6 +26,19 @@ describe('code-block fence lengthening — a block documenting a ``` fence stays
expect(out).toContain('nested fence');
});
+ it('sizes the fence in time that grows with a line of backticks, not with the square of it', () => {
+ const { destroy, parse, serialize } = markdownCodec();
+ const growth = slowdownWhenInputQuadruples(
+ (size: number) => parse(`\`\`\`\nstart\n${'`'.repeat(size)}x\nend\n\`\`\``),
+ serialize,
+ 16_000,
+ );
+
+ destroy();
+
+ expect(growth).toBeLessThan(GROWTH_IF_QUADRATIC / 2);
+ });
+
it('leaves an ordinary code block on a 3-backtick fence', () => {
expect(roundTrip('```js\nconst x = 1;\n```')).toBe('```js\nconst x = 1;\n```');
});
@@ -32,6 +47,13 @@ describe('code-block fence lengthening — a block documenting a ``` fence stays
// A tilde fence's info string may hold a backtick; a backtick fence's may not — re-emitting such a block on a
// backtick fence produces an opening line that is no longer a fence at all, and the code block is destroyed.
describe('code-block fence character — a language containing a backtick keeps its tilde fence', () => {
+ it('keeps a language that opens with a tilde apart from the fence', () => {
+ const src = '~~~ ~x\ncode\n~~~';
+
+ expect(roundTrip(src)).toBe(src);
+ expect(nodeCount(src, 'codeBlock')).toBe(1);
+ });
+
it('re-emits a tilde fence so the block survives reload', () => {
const src = '~~~`js`\nconst x = 1;\n~~~';
const out = roundTrip(src);
@@ -112,6 +134,116 @@ describe('inline code containing a backtick round-trips exactly', () => {
expect(roundTrip(out)).toBe(out);
});
+ it.each([
+ ['one backtick around a doubled one', 'where `name = ``admin`` and 1` holds'],
+ ['more backticks than the code needs', 'x ```` a`b ```` y'],
+ ['one backtick, over two lines', 'chars: `< > + \\``\nAllowed: `img`, `svg`'],
+ ['five backticks', 'x `````ab````` y'],
+ ['the fence the writer would choose', 'x ``a `b` c`` y'],
+ ])('keeps the fence a span was written with: %s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each<[string, { fence?: number; text: string }[], string]>([
+ [
+ 'code without a fence of its own inside it',
+ [{ fence: 3, text: 'nmap' }, { text: 'X' }, { fence: 3, text: ' -sV' }],
+ '`nmapX -sV`',
+ ],
+ [
+ 'code with another fence beside it',
+ [
+ { fence: 3, text: 'nmap' },
+ { fence: 2, text: 'id' },
+ ],
+ '`nmapid`',
+ ],
+ [
+ 'every piece of it with the same fence',
+ [
+ { fence: 3, text: 'nmap' },
+ { fence: 3, text: ' -sV' },
+ ],
+ '```nmap -sV```',
+ ],
+ ])('fences a span once: %s', (_name, pieces, saved) => {
+ const editor = new Editor({
+ content: {
+ content: [
+ {
+ content: [
+ { text: 'Run ', type: 'text' },
+ ...pieces.map(({ fence, text }) => ({
+ marks: [{ attrs: { fence: fence ?? null }, type: 'code' }],
+ text,
+ type: 'text',
+ })),
+ { text: ' first', type: 'text' },
+ ],
+ type: 'paragraph',
+ },
+ ],
+ type: 'doc',
+ },
+ extensions: createMarkdownExtensions(),
+ });
+ const written = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(written).toBe(`Run ${saved} first`);
+ expect(roundTrip(written)).toBe(written);
+ });
+
+ it('writes a span over two lines with a fence that holds its lines where the one it had does not', () => {
+ const written = roundTrip('x ```a\nb``` y');
+
+ expect(written).toBe('x `a\nb` y');
+ expect(roundTrip(written)).toBe(written);
+ });
+
+ it.each([
+ ['a run as long as the fence is typed into the span', 'x ```` a`b ```` y', '````', 'x ````` a`b ```` ````` y'],
+ ['the same with a fence of one', 'where `a``b` holds', '`', 'where ``` a``b` ``` holds'],
+ ])('writes the fence the code needs once %s', (_name, md, typed, saved) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ let end = 0;
+
+ editor.state.doc.descendants((node, at) => {
+ if (node.marks.some((mark) => mark.type.name === 'code')) {
+ end = at + node.nodeSize;
+ }
+ });
+ editor.chain().setTextSelection(end).insertContent(typed).run();
+
+ const written = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(written).toBe(saved);
+ expect(roundTrip(written)).toBe(written);
+ });
+
+ it.each([0, -1, 1.5, 65, '1'])('writes the fence the code needs where the one it carries is %j', (fence) => {
+ const editor = new Editor({
+ content: {
+ content: [
+ {
+ content: [{ marks: [{ attrs: { fence }, type: 'code' }], text: 'a``b', type: 'text' }],
+ type: 'paragraph',
+ },
+ ],
+ type: 'doc',
+ },
+ extensions: createMarkdownExtensions(),
+ });
+ const written = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(written).toBe('```a``b```');
+ });
+
it.each([
'a ``` `XSS` ``` b',
'x ``` `key` = value ``` y',
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-content-integrity.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-content-integrity.test.ts
index a6ea9ccf..e0163b39 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-content-integrity.test.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-content-integrity.test.ts
@@ -2,7 +2,6 @@ import { Editor } from '@tiptap/core';
import { beforeAll, describe, expect, it } from 'vitest';
import { createMarkdownExtensions } from './markdown-editor-extensions';
-import { escapeCellPipes } from './markdown-editor-marked';
import { roundTrip, setupEditorJsdom, structuralCounts } from './markdown-editor-test-setup';
beforeAll(setupEditorJsdom);
@@ -171,10 +170,10 @@ describe('generative content-integrity — atoms survive load↔serialize across
expect(out.includes(sentinel), `cell "${sentinel}" dropped (i=${i}):\n${doc}\n-->\n${out}`).toBe(true);
}
- // The atom itself must survive as a code span with its structural pipes escaped for the table — the
- // sentinel guards the row's cell count, this guards the pipe-bearing content from silent corruption.
+ // The atom itself must survive as the code span it was written as — the sentinel guards the row's
+ // cell count, this guards the pipe-bearing content from silent corruption.
for (const atom of atoms) {
- const span = `\`${escapeCellPipes(atom)}\``;
+ const span = `\`${atom}\``;
expect(out.includes(span), `atom ${span} corrupted (i=${i}):\n${doc}\n-->\n${out}`).toBe(true);
}
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
index 810fb579..72851034 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
@@ -1,10 +1,15 @@
+import type { JSONContent } from '@tiptap/core';
+
import { Editor } from '@tiptap/core';
import { OrderedList, TaskList } from '@tiptap/extension-list';
+import { Table } from '@tiptap/extension-table';
import { TextSelection } from '@tiptap/pm/state';
import { beforeAll, describe, expect, it } from 'vitest';
+import { GROWTH_IF_QUADRATIC, slowdownWhenInputQuadruples } from '@/test-utils/cost-growth';
+
import { createMarkdownExtensions, isBlockApplied } from './markdown-editor-extensions';
-import { roundTrip, setupEditorJsdom, structuralCounts } from './markdown-editor-test-setup';
+import { markdownCodec, roundTrip, setupEditorJsdom, structuralCounts } from './markdown-editor-test-setup';
import { findVariableOccurrences } from './markdown-editor-variable-highlight';
beforeAll(setupEditorJsdom);
@@ -167,20 +172,96 @@ describe('Underline disabled — ++ never parses, C++/++flags prose survives', (
);
});
-describe('bare URLs / emails / <autolinks> become links and converge; explicit [links] still work', () => {
+describe('bare URLs / emails / <autolinks> are links and are saved as written; explicit [links] still work', () => {
it.each([
- ['see https://example.com/path now', 'see [https://example.com/path](https://example.com/path) now'],
- ['contact [email protected] today', 'contact [[email protected]](mailto:[email protected]) today'],
- ['a <https://example.com> ref', 'a [https://example.com](https://example.com) ref'],
- ])('links %s then stays stable', (src, expected) => {
- const out = roundTrip(src);
- expect(out).toBe(expected);
- expect(roundTrip(out)).toBe(out);
+ ['see https://example.com/path now', 'https://example.com/path'],
+ ['contact [email protected] today', 'mailto:[email protected]'],
+ ['a <https://example.com> ref', 'https://example.com'],
+ ])('%s holds a link and comes back as written', (src, href) => {
+ const editor = new Editor({ content: src, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const links = JSON.stringify(editor.getJSON()).match(/"href":"[^"]*"/g);
+
+ editor.destroy();
+
+ expect(links).toEqual([`"href":"${href}"`]);
+ expect(roundTrip(src)).toBe(src);
});
it('still round-trips an explicit [link](url)', () => {
expect(roundTrip('[docs](https://example.com)')).toContain('[docs](https://example.com)');
});
+
+ it.each([
+ ['a bare URL in parentheses', '(see https://a.b/c).'],
+ ['a bold bare URL', '**https://a.b/e** is up'],
+ ['a URL that takes the backticks after it', 'at https://a.b/d`x` now'],
+ ['a link whose text is its target', '[https://a.b/c](https://a.b/c)'],
+ ['a relative link whose text is its target', '[docs/x.md](docs/x.md)'],
+ ['a www link written out', '[www.x.com](www.x.com)'],
+ ['bare URLs in a table', '| a | b |\n|---|---|\n| https://a.b/c | [email protected] |'],
+ ['a bare URL in a heading', '## See https://a.b/c'],
+ ])('%s comes back as written', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it('writes out only the link whose text is no longer its target, not the others on its line', () => {
+ expect(roundTrip('see https://a.b/c?x=1&amp;y=2 and https://a.b/d')).toBe(
+ 'see [https://a.b/c?x=1&y=2](https://a.b/c?x=1&amp;y=2) and https://a.b/d',
+ );
+ });
+
+ // CommonMark takes `<scheme:…>` for a link whatever the scheme; a Flask route is not one.
+ it.each(['route <int:order_id> here', 'pip install <git+https://github.com/x/y> now', '<sip:[email protected]>'])(
+ '%s is text, brackets included',
+ (md) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const hasLink = JSON.stringify(editor.getJSON()).includes('"link"');
+
+ editor.destroy();
+
+ expect(hasLink).toBe(false);
+ expect(roundTrip(md)).toBe(md);
+ },
+ );
+
+ // Where a bare URL ends depends on what follows it, and whether the bold before it ends depends on the URL.
+ it.each([
+ [
+ 'text is typed right against it',
+ 'See https://a.b/c next',
+ { from: 18, to: 19 },
+ 'See [https://a.b/c](https://a.b/c)next',
+ ],
+ [
+ 'bold ends right against it',
+ '**Note:** https://a.b/c',
+ { from: 6, to: 7 },
+ '**Note:**[https://a.b/c](https://a.b/c)',
+ ],
+ [
+ 'text is typed right after a bold one',
+ '**<https://a.b/c>** alpha',
+ { from: 14, to: 15 },
+ '[**https://a.b/c**](https://a.b/c)alpha',
+ ],
+ [
+ 'text ends right before a bold one',
+ 'see **<https://a.b/c>** now',
+ { from: 4, to: 5 },
+ 'see[**https://a.b/c**](https://a.b/c) now',
+ ],
+ ])('writes a bare URL as an explicit link once %s', (_name, md, range, saved) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+
+ editor.commands.deleteRange(range);
+
+ const out = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(out).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ });
});
describe('inline marks round-trip', () => {
@@ -216,11 +297,30 @@ describe('TunedTable — cell pipes escaped + alignment preserved, idempotent',
expect(save2).toContain(marker);
});
+ it.each([
+ ['a hash column header', '| # | Name |\n| --- | ---- |\n| 1 | a |'],
+ ['a cell that opens with a quote marker', '| Threshold | Note |\n| --------- | ---- |\n| > 5 hosts | scan |'],
+ [
+ 'cells that open with a backslash the author wrote',
+ '| Token | Meaning |\n| ----- | ----------- |\n| \\> | end of word |\n| \\# x | literal |',
+ ],
+ ['a row-like block with a short delimiter row', '| abc | def |\n| --- |\n| bar |'],
+ ])('comes back byte-identical: %s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
it('preserves per-column alignment (left :--- / center :---: / right ---:)', () => {
- const save1 = roundTrip('| L | C | R |\n| :-- | :-: | --: |\n| a | b | c |');
+ const md = '| L | C | R |\n| :-- | :-: | --: |\n| a | b | c |';
+ const { destroy, parse, serialize } = markdownCodec();
+ const [table] = parse(md).content ?? [];
+ // Without the lines it was written as, which a table made in the editor does not have.
+ const made = serialize({ content: [{ ...table, attrs: {} }], type: 'doc' });
- expect(save1).toContain('| :--- | :---: | ---: |');
- expect(roundTrip(save1)).toBe(save1);
+ destroy();
+
+ expect(roundTrip(md)).toBe(md);
+ expect(made).toContain('| :--- | :---: | ---: |');
+ expect(roundTrip(made)).toBe(made);
});
// A doc-side cell text (typed/pasted in rich mode) can hold a backslash run right before a pipe — a
@@ -303,9 +403,77 @@ describe('ordered > bullet > code — indented nested code survives', () => {
});
});
+describe('an indented code block holds its lines and nothing under them', () => {
+ it.each([
+ ['a line of text right under it', ' id\nIt prints the uid.'],
+ ['a list right under it', ' id\n whoami\n- next'],
+ ['a heading right under it', ' id\n# Next'],
+ ])('with %s', (_name, md) => {
+ const { destroy, parse } = markdownCodec();
+ const [block] = parse(md).content ?? [];
+
+ destroy();
+
+ expect(block?.type).toBe('codeBlock');
+ expect(block?.content?.[0]?.text).toBe(md.includes('whoami') ? 'id\nwhoami' : 'id');
+ });
+});
+
+describe('what a heading and an image carry beside their text', () => {
+ const saved = (block: JSONContent): string => {
+ const { destroy, serialize } = markdownCodec();
+ const written = serialize({ content: [block], type: 'doc' });
+
+ destroy();
+
+ return written;
+ };
+
+ it.each<[string, string, unknown, string]>([
+ ['the spaces after a heading', 'Run', ' ', '## Run '],
+ ['nothing after a heading where what stood there is not spaces', 'Run', 'x', '## Run'],
+ ['nothing after a heading where what stood there holds a tab', 'Run', ' \t', '## Run'],
+ ['nothing more after a heading whose text ends with white space', 'Run ', ' ', '## Run '],
+ ])('writes %s', (_name, text, trail, written) => {
+ expect(saved({ attrs: { level: 2, trail }, content: [{ text, type: 'text' }], type: 'heading' })).toBe(written);
+ });
+
+ it('writes no link around what does not read as an image', () => {
+ const link = { href: 'https://h.test/', title: null };
+
+ expect(saved({ attrs: { alt: 'a', link, src: 's p.png' }, type: 'image' })).toBe('![a](s p.png)');
+ });
+
+ it.each<[string, unknown, string]>([
+ ['a link around an image', { href: 'https://h.test/', title: null }, '[![a](s.png)](https://h.test/)'],
+ [
+ 'a link with a title around it',
+ { href: 'https://h.test/', title: 'open' },
+ '[![a](s.png)](https://h.test/ "open")',
+ ],
+ [
+ 'a target with a space between angle brackets',
+ { href: 'https://h.test/a b', title: null },
+ '[![a](s.png)](<https://h.test/a b>)',
+ ],
+ ['no link whose target cannot be written', { href: 'https://h.test/a b<c', title: null }, '![a](s.png)'],
+ ['no link whose target reads back as another', { href: 'https://h.test/a\\(b', title: null }, '![a](s.png)'],
+ ['no link whose title reads back as another', { href: 'https://h.test/', title: 'wow\\!' }, '![a](s.png)'],
+ ['no link that has no target', { href: '', title: 'open' }, '![a](s.png)'],
+ ['no link that is not one', 'https://h.test/', '![a](s.png)'],
+ ])('writes %s', (_name, link, written) => {
+ expect(saved({ attrs: { alt: 'a', link, src: 's.png' }, type: 'image' })).toBe(written);
+ expect(roundTrip(written)).toBe(written);
+ });
+});
+
describe('findVariableOccurrences — doc spans for the Available-variables cycle', () => {
- const docOf = (md: string) => {
- const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const docOf = (content: JSONContent[] | string) => {
+ const editor = new Editor({
+ content: typeof content === 'string' ? content : { content: [{ content, type: 'paragraph' }], type: 'doc' },
+ contentType: typeof content === 'string' ? 'markdown' : 'json',
+ extensions: createMarkdownExtensions(),
+ });
const { doc } = editor.state;
editor.destroy();
@@ -336,10 +504,13 @@ describe('findVariableOccurrences — doc spans for the Available-variables cycl
});
it('finds a {{.Var}} split across text nodes by a mark (mark-boundary fix)', () => {
- const doc = docOf('**{{**.Foo}} tail');
+ // What bolding one brace from the toolbar leaves in the document.
+ const doc = docOf([
+ { marks: [{ type: 'bold' }], text: '{{', type: 'text' },
+ { text: '.Foo}} tail', type: 'text' },
+ ]);
- // sanity — the variable really is split: bolding a brace gives the textblock >1 inline child
- expect(doc.firstChild?.childCount ?? 0).toBeGreaterThan(1);
+ expect(doc.firstChild?.childCount).toBe(2);
const foo = findVariableOccurrences(doc, 'Foo');
@@ -351,27 +522,41 @@ describe('findVariableOccurrences — doc spans for the Available-variables cycl
});
it('spans the full token when a hard break sits inside it (non-text-node off-by-N)', () => {
- const doc = docOf('{{.Foo\\\n}}');
+ const doc = docOf([{ text: '{{.Foo', type: 'text' }, { type: 'hardBreak' }, { text: '}}', type: 'text' }]);
- // sanity — the hard break really is inside the token (text, hardBreak, text)
- expect(doc.firstChild?.childCount ?? 0).toBeGreaterThan(1);
+ expect(doc.firstChild?.childCount).toBe(3);
const foo = findVariableOccurrences(doc, 'Foo');
expect(foo).toHaveLength(1);
for (const hit of foo) {
- expect(doc.textBetween(hit.from, hit.to)).toBe('{{.Foo}}');
+ expect(doc.textBetween(hit.from, hit.to)).toBe('{{.Foo\n}}');
}
});
+
+ // The panel counts uses on the markdown, where a line break stands between two words.
+ it.each([
+ ['a line break after the name', 'x {{ .Name\nY }} z', 1],
+ ['a hard break after the name', 'x {{ .Name\\\nY }} z', 1],
+ ['a line break inside the name', 'a {{.Na\nme}} b', 0],
+ ])('reads %s as the markdown has it', (_name, md, uses) => {
+ expect(findVariableOccurrences(docOf(md), 'Name')).toHaveLength(uses);
+ });
});
-describe('typing matches load — underscore emphasis literal, bare URL autolinks like load', () => {
+describe('typing matches load — underscore emphasis literal, a bare URL is a link', () => {
const typeString = (input: string): { html: string; md: string } => {
const editor = new Editor({ content: '', contentType: 'markdown', extensions: createMarkdownExtensions() });
const { view } = editor;
for (const ch of input) {
+ // A newline in the input stands for Shift+Enter.
+ if (ch === '\n') {
+ editor.commands.setHardBreak();
+ continue;
+ }
+
const { from } = view.state.selection;
const handled = view.someProp('handleTextInput', (handler) =>
handler(view, from, from, ch, () => view.state.tr),
@@ -397,18 +582,85 @@ describe('typing matches load — underscore emphasis literal, bare URL autolink
expect(md).toContain('_word_');
});
- it('typed bare URL autolinks — matches load', () => {
+ it('a typed bare URL is a link, saved with its target written out', () => {
const { html, md } = typeString('see https://evil.example.com/x done');
expect(html).toContain('<a ');
expect(md).toContain('[https://evil.example.com/x](https://evil.example.com/x)');
});
+ // Typed at the end of a loaded document, where an action is literal text and holds its own delimiters. A
+ // mark writes its delimiters back, so the bytes alone would not show one that took in half an action.
+ it.each([
+ ['a backtick after a raw string', 'x {{ printf `%s` .A }} y', ' `z`', 'x {{ printf `%s` .A }} y `z`', ['code']],
+ ['an asterisk after a comment', '{{/* a */}} b', ' c*', '{{/* a */}} b c*', []],
+ ['a tilde pair after one in a string', '{{ print " ~~" }} b', ' c~~', '{{ print " ~~" }} b c~~', []],
+ [
+ 'a URL that runs into an action',
+ 'see',
+ ' https://e.x/a?b={{.B}}&c=1 now',
+ 'see https://e.x/a?b={{.B}}&c=1 now',
+ [],
+ ],
+ ['asterisks around a whole action', 'the', ' **{{ .Name }}** one', 'the **{{ .Name }}** one', ['bold']],
+ ])('%s pairs with nothing inside a Go action', (_name, md, typed, saved, marks) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const { view } = editor;
+
+ editor.commands.focus('end');
+
+ for (const ch of typed) {
+ const { from } = view.state.selection;
+ const handled = view.someProp('handleTextInput', (handler) =>
+ handler(view, from, from, ch, () => view.state.tr),
+ );
+
+ if (!handled) {
+ view.dispatch(view.state.tr.insertText(ch));
+ }
+ }
+
+ const out = editor.getMarkdown();
+ const found = (editor.getJSON().content?.[0]?.content ?? []).flatMap((node: JSONContent) =>
+ (node.marks ?? []).map((mark) => mark.type),
+ );
+
+ editor.destroy();
+
+ expect(out).toBe(saved);
+ expect(found).toEqual(marks);
+ });
+
it('typed *italic* / **bold** / ~~strike~~ still convert (star + double-tilde kept)', () => {
expect(typeString('a **bold** b').html).toContain('<strong>');
expect(typeString('a *ital* b').html).toContain('<em>');
expect(typeString('a ~~del~~ b').html).toContain('<s>');
});
+
+ it.each([
+ ['bold first on the line', 'foo\n**bar** x', '<p>foo<br><strong>bar</strong> x</p>', 'foo \n**bar** x'],
+ ['italic first on the line', 'foo\n*bar* x', '<p>foo<br><em>bar</em> x</p>', 'foo \n*bar* x'],
+ ['strike first on the line', 'foo\n~~bar~~ x', '<p>foo<br><s>bar</s> x</p>', 'foo \n~~bar~~ x'],
+ ['bold across the break', '**a\nb**', '<p><strong>a<br>b</strong></p>', '**a** \n**b**'],
+ ['code across the break', '`a\nb`', '<p><code>a<br>b</code></p>', '`a` \n`b`'],
+ ])('a mark typed around a hard break converts as it loads: %s', (_name, typed, html, md) => {
+ const result = typeString(typed);
+
+ expect(result.html).toBe(html);
+ expect(result.md).toBe(md);
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it('only the hard break reports text to the input-rule guard', () => {
+ const editor = new Editor({ extensions: createMarkdownExtensions() });
+ const withLeafText = Object.values(editor.schema.nodes)
+ .filter((type) => type.spec.leafText)
+ .map((type) => type.name);
+
+ editor.destroy();
+
+ expect(withLeafText).toEqual(['hardBreak']);
+ });
});
describe('line-leading # / > in a paragraph stay body text on round-trip (ENCODER-LEADING)', () => {
@@ -458,7 +710,7 @@ describe('line-leading # / > in a paragraph stay body text on round-trip (ENCODE
});
});
-describe('multi-paragraph table cell does not persist a raw control byte on save', () => {
+describe('a break inside a table cell is saved as a space', () => {
const header = (text: string) => ({
content: [{ content: [{ text, type: 'text' }], type: 'paragraph' }],
type: 'tableHeader',
@@ -468,13 +720,26 @@ describe('multi-paragraph table cell does not persist a raw control byte on save
type: 'tableCell',
});
- it('collapses two cell paragraphs to a space instead of joining with U+001F', () => {
+ const brokenCell = (first: string, second: string) => ({
+ content: [
+ {
+ content: [{ text: first, type: 'text' }, { type: 'hardBreak' }, { text: second, type: 'text' }],
+ type: 'paragraph',
+ },
+ ],
+ type: 'tableCell',
+ });
+
+ it.each([
+ ['two cell paragraphs', cell('one', 'two')],
+ ['a hard break in a cell', brokenCell('one', 'two')],
+ ])('collapses %s', (_name, broken) => {
const doc = {
content: [
{
content: [
{ content: [header('h'), header('i')], type: 'tableRow' },
- { content: [cell('one', 'two'), cell('z')], type: 'tableRow' },
+ { content: [broken, cell('z')], type: 'tableRow' },
],
type: 'table',
},
@@ -486,9 +751,7 @@ describe('multi-paragraph table cell does not persist a raw control byte on save
editor.destroy();
- expect(md).not.toContain('\u001f');
- expect(md).toContain('one two');
- expect(md).toContain('z');
+ expect(md).toBe('| h | i |\n| ------- | --- |\n| one two | z |');
});
});
@@ -497,9 +760,9 @@ type MarkdownTokenizerShape = {
tokenize: (src: string, tokens: unknown[], lexer: unknown) => unknown;
};
-describe('list tokenizers are guarded against the upstream per-block full-source split', () => {
+describe("list tokenizers — numbered lists are marked's own, the task tokenizer is guarded", () => {
type Layer = {
- config: { markdownTokenizer?: MarkdownTokenizerShape };
+ config: { markdownTokenizer?: MarkdownTokenizerShape | null };
parent?: Layer;
};
@@ -533,12 +796,9 @@ describe('list tokenizers are guarded against the upstream per-block full-source
return extension ? rootOf(extension).config.markdownTokenizer : undefined;
};
- it.each([
- ['orderedList', OrderedList],
- ['taskList', TaskList],
- ])('%s tokenize is wrapped, not the upstream function', (name, upstream) => {
- const ours = tokenizerOf(name);
- const theirs = (upstream.config as { markdownTokenizer?: { tokenize: unknown } }).markdownTokenizer;
+ it('taskList tokenize is wrapped, not the upstream function', () => {
+ const ours = tokenizerOf('taskList');
+ const theirs = (TaskList.config as { markdownTokenizer?: { tokenize: unknown } }).markdownTokenizer;
expect(theirs?.tokenize).toBeTypeOf('function');
expect(ours?.tokenize).toBeTypeOf('function');
@@ -546,59 +806,82 @@ describe('list tokenizers are guarded against the upstream per-block full-source
expect(ours?.tokenize).not.toBe(theirs?.tokenize);
});
- // The guard must stay at least as wide as ORDERED_LIST_MARKER_PATTERN
- // (`\d+|[ivxlcdmIVXLCDM]+|[a-zA-Z]{1,2}`); a marker form it rejects is silently demoted to a paragraph.
it.each([
- ['single digit', '1. alpha\n2. beta'],
- ['multi digit', '10. alpha\n11. beta'],
- ['lower roman', 'i. alpha\nii. beta'],
- ['upper roman', 'IX. alpha\nX. beta'],
- ['single alpha', 'a. alpha\nb. beta'],
- ['two-letter alpha', 'aa. alpha\nab. beta'],
- ['two-letter upper alpha', 'AB. alpha\nAC. beta'],
- ])('ordered list with a %s marker survives the guard', (_name, md) => {
- // Byte equality alone is blind here: a demoted paragraph holding the same two lines re-serialises
- // to identical bytes, so the node type is the only assertion that can see the marker being rejected.
- expect(structuralCounts(roundTrip(md)).orderedList).toBe(1);
- expect(roundTrip(md)).toBe(md);
+ ['orderedList', OrderedList],
+ ['table', Table],
+ ])('%s ships a tokenizer upstream and the editor registers none', (name, upstream) => {
+ const theirs = (upstream.config as { markdownTokenizer?: { tokenize: unknown } }).markdownTokenizer;
+
+ expect(theirs?.tokenize).toBeTypeOf('function');
+ expect(tokenizerOf(name)).toBeNull();
});
- // Upstream normalises `)` to `.` and drops a leading indent (verified identical without the guard), so
- // these two assert only what the guard governs: the block is still a list, not demoted to a paragraph.
it.each([
- ['paren delimiter', 'aa) alpha\nab) beta'],
- ['indented', ' 1. alpha\n 2. beta'],
- ])('ordered list with a %s marker is not demoted to a paragraph', (_name, md) => {
+ ['single digit', '1. alpha\n2. beta', '1. alpha\n2. beta'],
+ ['multi digit', '10. alpha\n11. beta', '10. alpha\n11. beta'],
+ ['paren delimiter', '1) alpha\n2) beta', '1) alpha\n2) beta'],
+ ['indented', ' 1. alpha\n 2. beta', ' 1. alpha\n 2. beta'],
+ ])('a numbered list with a %s marker is a list', (_name, md, saved) => {
expect(structuralCounts(roundTrip(md)).orderedList).toBe(1);
+ expect(roundTrip(md)).toBe(saved);
+ });
+
+ // Byte equality alone passes for a list that re-serialises its own marker, hence the node count.
+ it.each([
+ ['lower roman', 'i. alpha\nii. beta', undefined],
+ ['upper roman', 'IX. alpha\nX. beta', undefined],
+ ['single alpha', 'a. alpha\nb. beta', undefined],
+ ['two-letter alpha', 'aa. alpha\nab. beta', undefined],
+ ['alpha paren', 'aa) alpha\nab) beta', undefined],
+ ['an honorific', 'Mr. Smith arrived at noon.', undefined],
+ ['an abbreviation before a number', 'No. 5 is alive.', undefined],
+ ['a figure reference', 'Fig. 3 shows the result.', undefined],
+ ['a question and its answer', 'Q. What is it?\nA. A scanner.', undefined],
+ ['letter sub-steps under a numbered step', '1. Step\n a. sub one\n b. sub two\n2. Next', 1],
+ ])('text that opens with %s stays as written', (_name, md, orderedLists) => {
+ expect(structuralCounts(roundTrip(md)).orderedList).toBe(orderedLists);
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it('a lettered list pasted as HTML is the numbered list it is saved as', () => {
+ const editor = new Editor({
+ content: '<ol type="a"><li><p>alpha</p></li><li><p>beta</p></li></ol>',
+ extensions: createMarkdownExtensions(),
+ });
+ const list = editor.getJSON().content?.[0];
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(list?.attrs).toMatchObject({ start: 1 });
+ expect(list?.attrs).not.toHaveProperty('type');
+ expect(md).toBe('1. alpha\n2. beta');
});
// Identity alone would also hold for a pass-through wrapper, which would silently restore the O(n^2)
// load. This is the assertion that the short-circuit itself is wired.
- it.each(['orderedList', 'taskList'])(
- '%s never reaches upstream tokenize when the source does not open a list',
- (name) => {
- const upstream = upstreamTokenizerOf(name);
- const wrapped = tokenizerOf(name);
+ it('taskList never reaches upstream tokenize when the source does not open a list', () => {
+ const upstream = upstreamTokenizerOf('taskList');
+ const wrapped = tokenizerOf('taskList');
- expect(upstream?.tokenize).toBeTypeOf('function');
+ expect(upstream?.tokenize).toBeTypeOf('function');
- const real = upstream!.tokenize;
- let calls = 0;
+ const real = upstream!.tokenize;
+ let calls = 0;
- upstream!.tokenize = (...args: Parameters<typeof real>) => {
- calls += 1;
+ upstream!.tokenize = (...args: Parameters<typeof real>) => {
+ calls += 1;
- return real(...args);
- };
+ return real(...args);
+ };
- try {
- expect(wrapped?.tokenize('plain paragraph line\n'.repeat(500), [], undefined)).toBeUndefined();
- expect(calls).toBe(0);
- } finally {
- upstream!.tokenize = real;
- }
- },
- );
+ try {
+ expect(wrapped?.tokenize('plain paragraph line\n'.repeat(500), [], undefined)).toBeUndefined();
+ expect(calls).toBe(0);
+ } finally {
+ upstream!.tokenize = real;
+ }
+ });
it.each([
['dash unchecked', '- [ ] open'],
@@ -611,6 +894,336 @@ describe('list tokenizers are guarded against the upstream per-block full-source
});
});
+describe('what is written under a list item, and right after a list, comes back as written', () => {
+ it.each([
+ [
+ 'code under a numbered step',
+ '1. Create the config:\n ```yaml\n steps:\n - name: scan\n ```\n2. Run it.',
+ ],
+ [
+ 'code holding list-like lines under a step',
+ '1. Example:\n ```text\n 1. not a list\n - not a bullet\n # not a heading\n ```\n2. Done.',
+ ],
+ ['a fence opened on a numbered marker line', '1. ```bash\n nmap -sV host\n ```\n2. next'],
+ ['a fence opened on a bullet marker line', '- ```bash\n nmap -sV host\n ```\n- next'],
+ ['a child of a two-digit item', '9. nine\n10. ten\n - child of ten\n11. eleven'],
+ ['a wrapped bullet with a hanging indent', '- first line of the item\n second line of the item\n- next'],
+ ['a wrapped numbered item with a hanging indent', '1. first line\n second line\n2. next'],
+ ['a closing tag right under a bullet list', '<rules>\n\n- one\n- two\n</rules>\n\nafter'],
+ ['a sentence right under a numbered list', '1. one\n2. two\nThen report back.'],
+ ['a line at the margin in a quoted list', '> - one\n> closing words'],
+ ['a second paragraph in a numbered item', '1. first\n\n more about first\n2. second'],
+ ['a quote in a numbered item', '1. item\n > quoted\n2. next'],
+ ['a plain bullet after a checkbox child', '- [ ] parent\n - [x] child task\n - plain child\n- [ ] next'],
+ ['a numbered checklist', '1. [ ] open the dashboard\n2. [X] export the report\n3. [ ]'],
+ ['a front-matter block', '---\n\ntitle: Nightly job runbook\ntags: [ops, cron]\n---\n\nBody'],
+ ['a two-line heading over a double rule', 'Totals for\nthe week\n===\n\ntext'],
+ ['an empty numbered item', '1. foo\n2.\n3. bar'],
+ ['an empty quote on a numbered marker line', '1. >'],
+ ['an empty quote on a bullet marker line', '- >\n- b'],
+ [
+ 'text after code in a tight step',
+ '1. Run the scan:\n ```bash\n nmap -sV 10.0.0.5\n ```\n Then review **the** output.\n2. Next step',
+ ],
+ ['a heading and its text in one item', '- ### Title\n body text\n- next'],
+ ['a rule inside an item', '- alpha\n\n ---\n- beta'],
+ ['a dashed rule on a numbered marker line', '1. ---\n2. beta'],
+ ['a heading right after a numbered item', '1. step one\n## Next section\ntext'],
+ ['a rule right after a numbered list', '1. one\n2. two\n---\nafter'],
+ ['a fence right after a numbered item', '3. step\n```bash\necho hi\n```'],
+ ['a loose numbered checklist', '1. [ ] one\n\n2. [x] two'],
+ ['front matter written tight', '---\ntitle: Nightly job runbook\ntags: [ops, cron]\n---\nBody'],
+ [
+ 'two tables in one item',
+ '- Results:\n | a | b |\n | --- | --- |\n | 1 | 2 |\n\n | c | d |\n | --- | --- |\n | 3 | 4 |',
+ ],
+ ['two quotes in one item', '- intro\n > one\n\n > two'],
+ ['tabs in code under a numbered step', '1. Build:\n ```make\n all:\n \tgcc main.c\n ```\n2. Run it.'],
+ [
+ 'tabs in code under a bullet',
+ '- Build:\n ```go\n func main() {\n \tfmt.Println("hi")\n }\n ```\n- Run it.',
+ ],
+ ])('%s', (_name, md) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const saved = editor.getMarkdown();
+ const check = () => editor.state.doc.check();
+
+ // An item that opens with a fence has no paragraph of its own; the schema has to hold all the same.
+ expect(check).not.toThrow();
+ editor.destroy();
+ expect(saved).toBe(md);
+ });
+
+ it.each([
+ [
+ 'a bullet holding a pipe in code right after a table',
+ '| a | b |\n| --- | --- |\n| 1 | 2 |\n- item `x | y` end',
+ '| a | b |\n| --- | --- |\n| 1 | 2 |\n\n- item `x | y` end',
+ ],
+ [
+ 'code in a nested item indented with tabs',
+ '- parent\n\t- child\n\n\t ```go\n\t if x {\n\t \treturn\n\t }\n\t ```\n- next',
+ '- parent\n - child\n\n ```go\n if x {\n \treturn\n }\n ```\n- next',
+ ],
+ ])('%s keeps its blocks apart and settles on the first save', (_name, md, saved) => {
+ expect(roundTrip(md)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('reads the markup of text that follows a code block in an item', () => {
+ const editor = new Editor({
+ content: '1. Run:\n ```\n x\n ```\n Then **bold** text.',
+ contentType: 'markdown',
+ extensions: createMarkdownExtensions(),
+ });
+ const html = editor.getHTML();
+
+ editor.destroy();
+
+ expect(html).toContain('<p>Then <strong>bold</strong> text.</p>');
+ });
+
+ it('keeps code inside its item once the text written at the margin above it is deleted', () => {
+ const editor = new Editor({
+ content: '- intro\n</x>\n ```sh\n one\n two\n ```\n- next',
+ contentType: 'markdown',
+ extensions: createMarkdownExtensions(),
+ });
+
+ editor.commands.deleteRange({ from: 3, to: 3 + 'intro\n</x>'.length });
+
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(saved).toBe('- ```sh\n one\n two\n ```\n- next');
+ });
+});
+
+describe('a heading that holds a line break', () => {
+ const text = (value: string) => ({ text: value, type: 'text' });
+ const hard = (marker?: string) => ({ attrs: { marker }, type: 'hardBreak' });
+ const soft = hard('');
+
+ // Setext has two levels only; below them the ATX form is all there is, and it is one line. Setext cannot
+ // hold a break at either end of the heading, or one above a line that reads as a block of its own.
+ it.each([
+ ['between two lines at level 1', 1, [text('a'), hard(), text('b')], 'a \nb\n==='],
+ ['between two lines at level 2', 2, [text('a'), hard(), text('b')], 'a \nb\n---'],
+ ['between two lines and at the end', 1, [text('a'), hard(), text('b'), hard()], 'a \nb\n==='],
+ ['between two lines at level 3', 3, [text('a'), hard(), text('b')], '### a b'],
+ ['written with a backslash at level 3', 3, [text('a'), hard('\\'), text('b')], '### a b'],
+ ['above an empty line', 3, [text('a'), hard(), hard(), text('b')], '### a b'],
+ ['at its end', 1, [text('Title'), hard()], '# Title'],
+ ['written with a backslash at its end', 2, [text('Title'), hard('\\')], '## Title'],
+ ['at its start', 1, [hard(), text('Title')], '# Title'],
+ ['written as nothing at its end', 1, [text('Totals for'), soft], '# Totals for'],
+ ['above a list item', 2, [text('Title'), hard(), text('- x')], '## Title - x'],
+ ['above an underline', 1, [text('Title'), soft, text('---')], '# Title ---'],
+ ['above a fence', 2, [text('Title'), soft, text('```sh')], '## Title ```sh'],
+ ])('%s', (_name, level, content, saved) => {
+ const editor = new Editor({
+ content: { content: [{ attrs: { level }, content, type: 'heading' }], type: 'doc' },
+ extensions: createMarkdownExtensions(),
+ });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe(saved);
+ expect(structuralCounts(saved)).toEqual({ heading: 1 });
+ });
+
+ it.each([
+ ['a one-line heading keeps the underline it was written with', 1, '=====', [text('Title')], 'Title\n====='],
+ ['a heading of two lines keeps it too', 2, '-----', [text('a'), soft, text('b')], 'a\nb\n-----'],
+ ["an underline of another level is not the heading's", 2, '=====', [text('Title')], '## Title'],
+ ['nor is it for a heading of two lines', 2, '=====', [text('a'), soft, text('b')], 'a\nb\n---'],
+ ['an underline of anything else is not one', 1, '=-=', [text('Title')], '# Title'],
+ ['a level without an underline has none', 3, '---', [text('Title')], '### Title'],
+ ])('%s', (_name, level, underline, content, saved) => {
+ const editor = new Editor({
+ content: { content: [{ attrs: { level, underline }, content, type: 'heading' }], type: 'doc' },
+ extensions: createMarkdownExtensions(),
+ });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe(saved);
+ expect(structuralCounts(saved)).toEqual({ heading: 1 });
+ });
+
+ it('is written in time that grows with a run of spaces, not with the square of it', () => {
+ const { destroy, parse, serialize } = markdownCodec();
+ const growth = slowdownWhenInputQuadruples(
+ (size: number) => parse(`### a${' '.repeat(size)}b`),
+ serialize,
+ 64_000,
+ );
+
+ destroy();
+
+ expect(growth).toBeLessThan(GROWTH_IF_QUADRATIC / 2);
+ });
+
+ it('stays a heading once the text of its last line is deleted', () => {
+ const editor = new Editor({
+ content: 'Totals for\nthe week\n===\n\ntext',
+ contentType: 'markdown',
+ extensions: createMarkdownExtensions(),
+ });
+
+ editor.commands.deleteRange({ from: 12, to: 20 });
+
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(saved).toBe('Totals for\n===\n\ntext');
+ });
+});
+
+describe('a rule on a marker line is not drawn with the marker', () => {
+ const ruleItem = (list: Record<string, unknown>, rule?: string): JSONContent => ({
+ content: [
+ {
+ attrs: list,
+ content: [
+ {
+ content: [{ type: 'paragraph' }, { attrs: { rule }, type: 'horizontalRule' }],
+ type: 'listItem',
+ },
+ ],
+ type: 'bulletList',
+ },
+ ],
+ type: 'doc',
+ });
+
+ it.each([
+ ['a dashed rule in a dashed item', {}, undefined, '- ***'],
+ ['a spaced dashed rule in a dashed item', {}, '- - -', '- ***'],
+ ['a starred rule in a starred item', { marker: '*' }, '***', '* ---'],
+ ['an underscored rule in a starred item', { marker: '*' }, '___', '* ___'],
+ ])('%s', (_name, list, rule, saved) => {
+ const editor = new Editor({ content: ruleItem(list, rule), extensions: createMarkdownExtensions() });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe(saved);
+ expect(structuralCounts(saved)).toMatchObject({ bulletList: 1, horizontalRule: 1 });
+ });
+});
+
+describe('text typed after four columns of space is not code', () => {
+ const paragraph = (value: string) => ({ content: [{ text: value, type: 'text' }], type: 'paragraph' });
+
+ it.each([
+ ['four spaces', [paragraph(' nmap -sV host')], 'nmap -sV host'],
+ ['a tab', [paragraph('\tnmap -sV host')], 'nmap -sV host'],
+ ['three spaces, which marked drops itself', [paragraph(' nmap')], ' nmap'],
+ [
+ 'four spaces in an item',
+ [{ content: [{ content: [paragraph(' nmap')], type: 'listItem' }], type: 'bulletList' }],
+ '- nmap',
+ ],
+ ['four spaces on a later line', [paragraph('scan\n nmap')], 'scan\n nmap'],
+ ])('%s', (_name, content, saved) => {
+ const editor = new Editor({ content: { content, type: 'doc' }, extensions: createMarkdownExtensions() });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe(saved);
+ expect(structuralCounts(saved).codeBlock).toBeUndefined();
+ });
+});
+
+describe('the blocks of a quote are written the way they stand', () => {
+ it.each([
+ ['a list right under its lead-in', '> Checked:\n> - one\n> - two'],
+ ['text right under a heading', '> # Title\n> text'],
+ ['text right under a fence', '> ```sh\n> id\n> ```\n> It prints the uid.'],
+ ['a blank quoted line between two paragraphs', '> one\n>\n> two'],
+ ["a template's range around a list", '> {{ range .Notes }}\n> - {{ . }}\n> {{ end }}'],
+ ['a quote in a quote', '> outer\n> > inner\n> > more'],
+ ['a quote that holds nothing', '>'],
+ ])('%s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ expect(structuralCounts(md).blockquote).toBeGreaterThan(0);
+ });
+
+ it('puts an empty quoted line between two blocks made in the editor', () => {
+ const paragraph = (value: string) => ({ content: [{ text: value, type: 'text' }], type: 'paragraph' });
+ const editor = new Editor({
+ content: { content: [{ content: [paragraph('one'), paragraph('two')], type: 'blockquote' }], type: 'doc' },
+ extensions: createMarkdownExtensions(),
+ });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe('> one\n>\n> two');
+ });
+});
+
+describe('a hard break keeps the marker it was written with', () => {
+ it.each([
+ ['a backslash', 'Question:\\\nanswer'],
+ ['two spaces', 'one \ntwo'],
+ ['three spaces', 'line \nnext'],
+ ['spaces at the end of an item', '- one \n- two \n- three'],
+ ])('%s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ ['a break written as nothing', 'one\ntwo'],
+ ['a backslash break', 'one\\\ntwo'],
+ ['a two-space break', 'one \ntwo'],
+ ])('%s is the same break after its HTML is read back', (_name, md) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const html = editor.getHTML();
+
+ editor.destroy();
+
+ const reread = new Editor({ content: html, extensions: createMarkdownExtensions() });
+ const saved = reread.getMarkdown();
+
+ reread.destroy();
+
+ expect(saved).toBe(md);
+ });
+});
+
+describe('a navigation key does not edit the document', () => {
+ it.each([
+ ['Tab on the block under a list', '- one\n\nafter', 'paragraph', 'Tab'],
+ ['ArrowUp at the top of a leading code block', '```\ncode\n```\n\nafter', 'codeBlock', 'ArrowUp'],
+ ])('%s', (_name, md, at, key) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ let caret = -1;
+
+ editor.state.doc.forEach((node, offset) => {
+ if (caret < 0 && node.type.name === at) {
+ caret = offset + 1;
+ }
+ });
+ editor.commands.setTextSelection(caret);
+
+ editor.commands.keyboardShortcut(key);
+
+ const after = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(caret).toBeGreaterThan(0);
+ expect(after).toBe(md);
+ });
+});
+
describe('block toggles are reversible under a whole-document selection', () => {
const CONTROLS = [
['blockquote', 'toggleBlockquote'],
@@ -705,14 +1318,14 @@ describe('block toggles are reversible under a whole-document selection', () =>
});
describe('a task checkbox is announced with its own text, not its subtree', () => {
- const labelsFor = (markdown: string) => {
+ const labelsFor = (content: JSONContent | string) => {
const element = document.createElement('div');
document.body.appendChild(element);
const editor = new Editor({
- content: markdown,
- contentType: 'markdown',
+ content,
+ contentType: typeof content === 'string' ? 'markdown' : 'json',
element,
extensions: createMarkdownExtensions(),
});
@@ -738,6 +1351,21 @@ describe('a task checkbox is announced with its own text, not its subtree', () =
it('an empty task still gets a name', () => {
expect(labelsFor('- [ ] \n')).toEqual(['Task item checkbox for empty task item']);
});
+
+ it.each([
+ [
+ 'text around a hard break',
+ [{ text: 'foo', type: 'text' }, { type: 'hardBreak' }, { text: 'bar', type: 'text' }],
+ 'foo\nbar',
+ ],
+ ['nothing but a hard break', [{ type: 'hardBreak' }], 'empty task item'],
+ ])('a task holding %s is named by its text', (_name, inline, label) => {
+ const task = { attrs: { checked: false }, content: [{ content: inline, type: 'paragraph' }], type: 'taskItem' };
+
+ expect(labelsFor({ content: [{ content: [task], type: 'taskList' }], type: 'doc' })).toEqual([
+ `Task item checkbox for ${label}`,
+ ]);
+ });
});
describe('whole-document block toggles across document shapes', () => {
@@ -845,7 +1473,7 @@ describe('whole-document block toggles across document shapes', () => {
return;
}
- expect(out.trimEnd()).toBe(original.trimEnd());
+ expect(out).toBe(original);
},
);
@@ -874,7 +1502,7 @@ describe('whole-document block toggles across document shapes', () => {
editor.destroy();
- expect(out.trimEnd()).toBe('one\n\ntwo');
+ expect(out).toBe('one\n\ntwo');
});
it.each(TOGGLES)('%s reports the same state to the toolbar as its own click performs', (node, command) => {
@@ -1084,7 +1712,7 @@ describe('a setext underline after a hard break does not turn the paragraph into
const result = hardBreakParagraph('foo', underline);
expect(result.counts).toEqual({});
- expect(result.text).toBe(`foo${underline}`);
+ expect(result.text).toBe(`foo\n${underline}`);
});
it('leaves a real setext heading in stored markdown alone', () => {
@@ -1157,6 +1785,15 @@ describe('brackets in a link label or an image alt do not break the node on relo
});
// An unbalanced bracket in alt broke `![alt](src)` outright: the reload parsed ZERO image nodes.
+ it('leaves the brackets of a Go action in an image alt to the template', () => {
+ const md = '![{{ index .M "[k]" }} of [x]](http://x.test/i.png)';
+ const saved = roundTrip(md);
+
+ expect(saved).toBe('![{{ index .M "[k]" }} of \\[x\\]](http://x.test/i.png)');
+ expect(roundTrip(saved)).toBe(saved);
+ expect(structuralCounts(saved).image).toBe(1);
+ });
+
it.each(['a]b', 'a[b'])('an image alt of %s keeps the image node', (alt) => {
const saved = saveOf(imageDoc(alt));
@@ -1200,8 +1837,7 @@ describe('a heading applied over the whole document', () => {
editor.commands.selectAll();
editor.commands.toggleHeading({ level: level as 1 | 2 | 3 | 4 | 5 | 6 });
- // selectAll materialises TrailingNode's empty paragraph, which serialises as a trailing blank line.
- const out = editor.getMarkdown().trimEnd();
+ const out = editor.getMarkdown();
editor.destroy();
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts
index 7a770266..2b4227fb 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts
@@ -24,8 +24,31 @@ import StarterKit from '@tiptap/starter-kit';
import { common, createLowlight } from 'lowlight';
import { HeadingAutoformat } from './markdown-editor-heading-autoformat';
-import { createMarkdownLayer, TunedTable } from './markdown-editor-marked';
+import {
+ blankLineOf,
+ indentItemBlock,
+ isRule,
+ isWrittenEmpty,
+ itemMarker,
+ joinBlocks,
+ lineBreaksAtEnd,
+ MarkdownLayout,
+ renderDocument,
+ renderList,
+ withinItem,
+ withLayoutFacts,
+} from './markdown-editor-layout';
+import {
+ createMarkdownLayer,
+ readsAsHeading,
+ readsAsLinkedImage,
+ renderInline,
+ TunedTable,
+ withMarkedBreaks,
+ withoutMarkdownTokenizer,
+} from './markdown-editor-marked';
import { MarkdownPaste } from './markdown-editor-paste';
+import { TEMPLATE_ACTION } from './markdown-editor-table-pipes';
import { TagHighlight } from './markdown-editor-tag-highlight';
import { VariableHighlight } from './markdown-editor-variable-highlight';
@@ -338,13 +361,11 @@ type MarkdownTokenizer = {
};
// marked runs every block extension's `tokenize` at EVERY block boundary, handing it the whole remaining
-// document, and @tiptap/extension-list's ordered/task tokenizers open with `src.split('\n')` before bailing
-// on a first line that isn't a list item — so each boundary materialises all remaining lines, O(n²).
+// document, and @tiptap/extension-list's task tokenizer opens with `src.split('\n')` before bailing on a
+// first line that isn't a task item — so each boundary materialises all remaining lines, O(n²).
//
-// The gate must stay WIDER than the marker grammar upstream accepts: narrower silently demotes a list to a
-// paragraph. Upstream's own `start` cannot serve as it — @tiptap/extension-list ships `start: () => -1` for
-// orderedList, which would gate the tokenizer off entirely.
-const OPENS_ORDERED_LIST = /^[ \t]*[0-9A-Za-z]{1,8}[.)][ \t]/;
+// The gate must accept every line upstream's item pattern accepts: a task line it rejects falls through to
+// marked's own list rule and loads as a plain bullet list.
const OPENS_TASK_LIST = /^[ \t]*[-+*][ \t]+\[[ xX]\][ \t]/;
const guardBlockTokenizer = <T extends AnyExtension>(extension: T, opens: RegExp): T => {
@@ -363,29 +384,229 @@ const guardBlockTokenizer = <T extends AnyExtension>(extension: T, opens: RegExp
}) as T;
};
+// @tiptap/extension-list tokenizes numbered lists with a line scanner of its own. It does not know a fence, so
+// a code block under a step is re-read as list lines; it strips one column too few from an item's continuation
+// lines, so that code gains a space of indentation on every load; and it takes a letter or roman marker, so
+// `Mr. Smith` is a list item and is saved as `1. Smith`. marked's own list rule is CommonMark's: without the
+// custom tokenizer a numbered list reaches parseMarkdown as the same native `list` token a bullet list does,
+// and `a.` or `IV.` at the start of a line is the text it was written as.
+// parseMarkdown is replaced with it: upstream reads `token.start || 1`, renumbering a list that opens at 0, and
+// builds the items itself, past listItem's parseMarkdown. `type` goes too — nothing loaded sets it, and a
+// lettered list pasted as HTML would show `a.` while renderListItem saves `1.`.
+const withNativeListTokens = <T extends AnyExtension>(extension: T): T =>
+ withoutMarkdownTokenizer(extension).extend({
+ addAttributes() {
+ const inherited = (this as unknown as { parent?: () => Record<string, object> }).parent?.() ?? {};
+ const { type: _markerType, ...attributes } = inherited;
+
+ return attributes;
+ },
+ // Upstream's plugin turns pasted lines that open with numbers, letters or roman numerals into a list
+ // by itself. MarkdownPaste already parses a paste the way a load is parsed, and declines one inside
+ // code — where this plugin would split the code block to insert its list.
+ addProseMirrorPlugins: () => [],
+ parseMarkdown(token: MarkdownToken, helpers: MarkdownParseHelpers): MarkdownParseResult {
+ if (token.type !== 'list' || !token.ordered) {
+ return [];
+ }
+
+ const start = typeof token.start === 'number' ? token.start : 1;
+
+ return {
+ ...(start === 1 ? {} : { attrs: { start } }),
+ content: helpers.parseChildren(token.items ?? []),
+ type: 'orderedList',
+ };
+ },
+ }) as T;
+
+type ParseMarkdown = (token: MarkdownToken, helpers: MarkdownParseHelpers) => MarkdownParseResult;
+
+// The schema wants a paragraph first in every item, and tiptap builds the document without checking it.
+// `1. ```sh` puts a code block first, so the item gets the empty paragraph the schema implies; renderListItem
+// writes the pair back on the marker's line.
+const withLeadingParagraph = (parsed: MarkdownParseResult): MarkdownParseResult => {
+ if (
+ Array.isArray(parsed) ||
+ 'mark' in parsed ||
+ parsed.type !== 'listItem' ||
+ parsed.content?.[0]?.type === 'paragraph'
+ ) {
+ return parsed;
+ }
+
+ return { ...parsed, content: [{ content: [], type: 'paragraph' }, ...(parsed.content ?? [])] };
+};
+
+// marked lifts a task marker out of an item's text into a `checkbox` token: a block token ahead of the text in
+// a tight list, the first inline token of the paragraph in a loose one. A task list is tokenized upstream and
+// never gets here, so in these items the marker is part of what was written: dropped, `1. [ ] step` is saved
+// as `1. step`.
+const withLiteralCheckbox = (token: MarkdownToken): MarkdownToken => {
+ const [first, second, ...rest] = token.tokens ?? [];
+ const asText = (checkbox: MarkdownToken): MarkdownToken => ({
+ raw: checkbox.raw,
+ text: checkbox.raw,
+ type: 'text',
+ });
+
+ if (first?.type === 'checkbox' && second) {
+ const literal = asText(first);
+ const merged = {
+ ...second,
+ raw: literal.raw + (second.raw ?? ''),
+ text: literal.text + (second.text ?? ''),
+ tokens: [literal, ...(second.tokens ?? [])],
+ };
+
+ return { ...token, tokens: [merged, ...rest] };
+ }
+
+ const [lead, ...inline] = first?.tokens ?? [];
+
+ if (first && lead?.type === 'checkbox') {
+ return {
+ ...token,
+ tokens: [{ ...first, tokens: [asText(lead), ...inline] }, ...(second ? [second] : []), ...rest],
+ };
+ }
+
+ return token;
+};
+
+// marked types a tight item's text as bare `text` tokens, and upstream wraps only the first one in a paragraph:
+// text that follows another block reaches the document as a text node outside any paragraph, with its inline
+// markup unread. Typed as paragraphs they all take the path a loose item's text takes.
+const withParagraphs = (token: MarkdownToken): MarkdownToken => ({
+ ...token,
+ tokens: token.tokens?.map((block) => (block.type === 'text' ? { ...block, type: 'paragraph' } : block)),
+});
+
+const renderWith = (helpers: MarkdownRendererHelpers) => (child: JSONContent, index: number) =>
+ helpers.renderChild?.(child, index) ?? helpers.renderChildren([child]);
+
+const renderListNode = (node: JSONContent, helpers: MarkdownRendererHelpers): string =>
+ renderList(node, renderWith(helpers));
+
+// Upstream writes an item's first block after the marker and indents only the blocks that follow it, so a
+// first block of several lines (a fence opened on the marker's line) loses its indentation and swallows the
+// rest of the list on the next load. Here every line after the marker's is aligned to the content column — the
+// marker's own width, not a fixed two spaces — which is what makes a block under `10.` a child for every other
+// markdown reader, and keeps a wrapped item's hanging indent as it was written.
+const renderListItem = (node: JSONContent, helpers: MarkdownRendererHelpers): string => {
+ const marker = itemMarker(node);
+ const pad = ' '.repeat(marker.length);
+ const children = node.content ?? [];
+ // An empty paragraph ahead of another block is the one withLeadingParagraph added: the block shares the
+ // marker's line again, as it was written. A rule there is drawn with the character the bullet is not —
+ // `- ---` and `* ***` are themselves rules, not items.
+ const firstIndex = isWrittenEmpty(children[0]) && children.length > 1 && children[1]!.type !== 'paragraph' ? 1 : 0;
+ const isRuleFirst = firstIndex && children[1]!.type === 'horizontalRule';
+ const { lazyDepth, lazyFrom } = node.attrs ?? {};
+ const isLazy = typeof lazyFrom === 'number' && !firstIndex && children[0]?.type === 'paragraph';
+ const render = renderWith(helpers);
+
+ return withinItem(() =>
+ joinBlocks(
+ children.slice(firstIndex),
+ (child, at) => {
+ const written = render(child, firstIndex + at);
+
+ return isRuleFirst && !at && written[0] === marker[0] ? (marker[0] === '*' ? '---' : '***') : written;
+ },
+ true,
+ (written, at, child) => {
+ const blank = blankLineOf(child, pad);
+
+ if (at) {
+ return indentItemBlock(written, pad, 0, Infinity, 1, blank);
+ }
+
+ const depth = Math.min(Math.max(Math.trunc(Number(lazyDepth)) || 1, 1), 64);
+ const block = indentItemBlock(written, pad, 1, isLazy ? lazyFrom : Infinity, depth, blank);
+
+ // A marker with nothing after it on its line is written bare; text keeps even the spaces its
+ // line ends with.
+ return (!block || block.startsWith('\n') ? marker.trimEnd() : marker) + block;
+ },
+ ),
+ );
+};
+
+// An ATX heading is one line. A setext heading may run over several, and it is what a paragraph with `---`
+// straight under it parses as — a front-matter block, a separator typed without a blank line. Written as ATX
+// its later lines fall out of the heading on the next load; from level 3 on there is no other form, so there
+// a line break is written as the space it reads as. So is one an edit has left where setext cannot hold it:
+// at either end of the heading, or above a line that would be read as a block of its own.
+const renderHeading = (node: JSONContent, helpers: MarkdownRendererHelpers): string => {
+ const level = Number(node.attrs?.level) || 1;
+ const written = (node.content ? renderInline(node.content, helpers) : '').split('\n');
+ // Each line without what a hard break is written as at its end.
+ const lines = written.map((line, at) =>
+ at === written.length - 1 ? line : line.endsWith('\\') ? line.slice(0, -1) : line.trimEnd(),
+ );
+ const from = Math.max(
+ 0,
+ lines.findIndex((line) => line.trim()),
+ );
+ const to = lines.findLastIndex((line) => line.trim()) + 1;
+ // The underline the heading was written with, while the heading is still of its level.
+ const row = level === 1 ? '=' : '-';
+ const underline: unknown = node.attrs?.underline;
+ const isUnderlined =
+ typeof underline === 'string' && underline !== '' && underline === row.repeat(underline.length);
+
+ if (level <= 2 && to > from && (to - from > 1 || isUnderlined)) {
+ const setext = [...written.slice(from, to - 1), lines[to - 1], isUnderlined ? underline : row.repeat(3)].join(
+ '\n',
+ );
+
+ if (readsAsHeading(setext, level)) {
+ return setext;
+ }
+ }
+
+ const line = lines
+ .slice(from, to)
+ .map((part, at) => (at ? part.trimStart() : part))
+ .filter(Boolean)
+ .join(' ');
+
+ // Not after text that ends with white space of its own: read back, the two would be one trail.
+ const trail: unknown = line === line.trimEnd() ? node.attrs?.trail : '';
+
+ return line ? `${'#'.repeat(level)} ${line}${typeof trail === 'string' && /^ +$/.test(trail) ? trail : ''}` : '';
+};
+
const longestRun = (text: string, runs: RegExp): number =>
(text.match(runs) ?? []).reduce((max, run) => Math.max(max, run.length), 0);
// @tiptap/extension-code-block's renderMarkdown always emits a 3-backtick fence, so a code block whose content
// contains a ``` line (a doc demonstrating fenced markdown — common in knowledge/prompt examples) re-parses as
-// TWO blocks on the next load: the inner fence closes the outer one. CommonMark requires the fence to be longer
-// than any backtick run inside — widen it. CommonMark also forbids a backtick in a BACKTICK fence's info
-// string while allowing one in a tilde fence's, so a language holding a backtick must ride a `~~~` fence.
-// Otherwise identical to upstream.
+// TWO blocks on the next load: the inner fence closes the outer one. Only a line that is nothing but a fence
+// run closes a block — ` ```bash ` inside one does not — so the fence is one longer than the longest such
+// line, and never shorter than the one the block was written with. CommonMark also forbids a backtick in a
+// BACKTICK fence's info string while allowing one in a tilde fence's, so a language holding a backtick rides
+// a `~~~` fence, as does a block that was written with one.
const renderTunedCodeBlock = (node: JSONContent, helpers: MarkdownRendererHelpers): string => {
const language: string = node.attrs?.language || '';
- const [marker, runs] = language.includes('`') ? (['~', /~+/g] as const) : (['`', /`+/g] as const);
+ const written = typeof node.attrs?.fence === 'string' ? node.attrs.fence : '';
+ const marker = language.includes('`') || written.startsWith('~') ? '~' : '`';
const content = node.content ? helpers.renderChildren(node.content) : '';
- const fence = marker.repeat(Math.max(3, longestRun(content, runs) + 1));
+ // Each run is of one character, so a line of a thousand backticks is matched once, not from every length.
+ const closers =
+ content.match(marker === '~' ? /^ {0,3}~{3,}(?:`[~`]*)? *$/gm : /^ {0,3}`{3,}(?:~[~`]*)? *$/gm) ?? [];
+ const longest = closers.reduce((max, line) => Math.max(max, longestRun(line, marker === '~' ? /~+/g : /`+/g)), 0);
+ const fence = marker.repeat(Math.max(3, longest + 1, written.startsWith(marker) ? written.length : 0));
- return [`${fence}${language}`, content, fence].join('\n');
+ return [`${fence}${language.startsWith('~') ? ' ' : ''}${language}`, content, fence].join('\n');
};
// @tiptap/extension-code-block's own parseMarkdown gates on `token.raw.startsWith('```')`, but CommonMark
// lets a fenced code block's opening fence be indented up to 3 spaces — marked then emits a valid `code`
// token whose `raw` starts with that whitespace, the gate rejects it, and the block is dropped on load. When
// a document mixes fences at different indents the mis-detection cascades and everything after the first
-// dropped fence vanishes too. Trim the leading indent before the gate; otherwise identical to upstream.
+// dropped fence vanishes too. Trim the leading indent before the gate.
const parseTunedCodeBlock = (token: MarkdownToken, helpers: MarkdownParseHelpers): MarkdownParseResult => {
const fence = token.raw?.trimStart() ?? '';
@@ -393,10 +614,15 @@ const parseTunedCodeBlock = (token: MarkdownToken, helpers: MarkdownParseHelpers
return [];
}
+ // marked leaves the last line break in the text of an indented block that no blank line follows, which
+ // a fence would then hold as an empty line of code.
+ const code = token.text ?? '';
+ const text = token.codeBlockStyle === 'indented' ? code.slice(0, code.length - lineBreaksAtEnd(code).length) : code;
+
return helpers.createNode(
'codeBlock',
{ language: token.lang || null },
- token.text ? [helpers.createTextNode(token.text)] : [],
+ text ? [helpers.createTextNode(text)] : [],
);
};
@@ -435,7 +661,9 @@ const TunedCodeBlock = CodeBlockLowlight.extend({
renderMarkdown: renderTunedCodeBlock,
// Without defaultLanguage an info-string-less fence falls to highlightAuto, which re-scans all 37 `common`
// grammars over every code block in the document on each keystroke inside one.
-}).configure({ defaultLanguage: 'plaintext', HTMLAttributes: { class: 'hljs' }, lowlight });
+ // exitOnArrowUp adds an empty paragraph above a code block that opens the document: a navigation key that
+ // edits the document and dirties the form, on exactly the documents agents store as code.
+}).configure({ defaultLanguage: 'plaintext', exitOnArrowUp: false, HTMLAttributes: { class: 'hljs' }, lowlight });
// A paragraph line that literally starts with `# ` or `> ` re-parses as a heading / blockquote on the next load
// (an ATX heading interrupts a paragraph; `>` opens a quote), silently changing the block TYPE of body text —
@@ -463,70 +691,280 @@ const escapeSetextUnderlines = (markdown: string): string =>
// marked unescapes `\[`/`\]` inside a label through its own outputLink pass, independent of the neutralised
// inline escape tokenizer, so escaping just those two closes the round trip. A lone `\` is NOT escaped —
// marked would not unescape it back.
-const escapeBracketsInLabel = (text: string): string => text.replace(/[[\]]/g, (bracket) => `\\${bracket}`);
+// A bracket inside a Go action is the template's — escaped, `{{ index .M "[k]" }}` no longer parses.
+const LABEL_BRACKET = new RegExp(`${TEMPLATE_ACTION.source}|[[\\]]`, 'g');
+
+const escapeBracketsInLabel = (text: string): string =>
+ text.replace(LABEL_BRACKET, (match) => (match.length > 1 ? match : `\\${match}`));
// StarterKit's Bold/Italic register BOTH `**`/`*` and `__`/`_` input+paste rules. The marked layer keeps
// `_`-emphasis literal on load/paste, so leaving the underscore TYPING rules on would diverge — typed
// `__init__`/`_word_` would emphasize (→ `**init**`/`*word*`) while the same text loaded stays literal,
// breaking identifiers. Drop only the underscore rules (their `find` regex mentions `_`; the `*` rules stay)
// so typing matches load. (codeBlock is replaced by TunedCodeBlock below; underline off below.)
-const TunedStarterKit = StarterKit.extend({
- addExtensions() {
- return (this.parent?.() ?? []).map((extension) => {
- if (extension.name === 'bold' || extension.name === 'italic') {
- return extension.extend({
- addInputRules() {
- return dropUnderscoreRules(this.parent?.() ?? []);
- },
- addPasteRules() {
- return dropUnderscoreRules(this.parent?.() ?? []);
- },
- });
- }
+// A Go action is literal text, so a delimiter inside one is not a delimiter: a backtick typed after
+// {{ printf `%s` .A }} must not pair with the one that closes the raw string and take it out of the action.
+// A match that holds a whole action — `**{{ .Name }}**` — cuts nothing.
+const crossesAction = (match: { 0: string; index?: number; input?: string }): boolean => {
+ const from = match.index ?? 0;
+ const to = from + match[0].length;
- if (extension.name === 'orderedList') {
- return withWholeDocumentToggle(
- guardBlockTokenizer(extension, OPENS_ORDERED_LIST),
- 'toggleOrderedList',
- listFamily,
- );
- }
+ return [...(match.input ?? '').matchAll(TEMPLATE_ACTION)].some((action) => {
+ const end = action.index + action[0].length;
- if (extension.name === 'bulletList') {
- return withWholeDocumentToggle(extension, 'toggleBulletList', listFamily);
- }
+ return action.index < to && end > from && !(from <= action.index && to >= end);
+ });
+};
- if (extension.name === 'blockquote') {
- return withWholeDocumentToggle(extension, 'toggleBlockquote', wrapFamily);
- }
+type MatchHandler = (props: { match: RegExpMatchArray }) => unknown;
- if (extension.name === 'heading') {
- return withWholeDocumentToggle(extension, 'toggleHeading', blockTypeFamily);
- }
+const outsideActions = <Rule extends object>(rules: Rule[]): Rule[] =>
+ rules.map((rule) => {
+ const matched = rule as { handler: MatchHandler };
+ const handle = matched.handler;
- if (extension.name === 'paragraph') {
- return extension.extend({
- renderMarkdown(node: JSONContent, helpers: MarkdownRendererHelpers) {
- return escapeSetextUnderlines(
- escapeLineLeadingBlockMarkers(helpers.renderChildren(node.content ?? [])),
- );
- },
- });
- }
+ matched.handler = (props) => (crossesAction(props.match) ? null : handle(props));
+
+ return rule;
+ });
+
+type WithParentRules = { parent?: () => object[] };
+
+const tuneStarterKitExtension = (extension: AnyExtension): AnyExtension => {
+ if (extension.name === 'bold' || extension.name === 'italic') {
+ return extension.extend({
+ addInputRules() {
+ return outsideActions(dropUnderscoreRules(this.parent?.() ?? []));
+ },
+ addPasteRules() {
+ return outsideActions(dropUnderscoreRules(this.parent?.() ?? []));
+ },
+ });
+ }
+
+ if (extension.name === 'strike') {
+ return extension.extend({
+ addInputRules(this: WithParentRules) {
+ return outsideActions(this.parent?.() ?? []) as never;
+ },
+ addPasteRules(this: WithParentRules) {
+ return outsideActions(this.parent?.() ?? []) as never;
+ },
+ });
+ }
+
+ if (extension.name === 'orderedList') {
+ return withWholeDocumentToggle(
+ withNativeListTokens(extension).extend({ renderMarkdown: renderListNode }),
+ 'toggleOrderedList',
+ listFamily,
+ );
+ }
+
+ // ListKeymap's Tab pulls the block under a list into its last item. Tab is how the keyboard leaves
+ // the editor, so from that one caret position it would edit the document instead.
+ if (extension.name === 'listKeymap') {
+ return extension.extend({
+ addKeyboardShortcuts() {
+ const { Tab: _sinkIntoList, ...shortcuts } = this.parent?.() ?? {};
+
+ return shortcuts;
+ },
+ });
+ }
+
+ if (extension.name === 'listItem') {
+ const parseItem = (extension.config as { parseMarkdown?: ParseMarkdown }).parseMarkdown;
+
+ return extension.extend({
+ parseMarkdown: (token: MarkdownToken, helpers: MarkdownParseHelpers) =>
+ withLeadingParagraph(parseItem?.(withParagraphs(withLiteralCheckbox(token)), helpers) ?? []),
+ renderMarkdown: renderListItem,
+ });
+ }
+
+ if (extension.name === 'doc') {
+ return extension.extend({
+ renderMarkdown: (node: JSONContent, helpers: MarkdownRendererHelpers) =>
+ renderDocument(node, renderWith(helpers)),
+ });
+ }
+
+ if (extension.name === 'bulletList') {
+ return withWholeDocumentToggle(
+ extension.extend({ renderMarkdown: renderListNode }),
+ 'toggleBulletList',
+ listFamily,
+ );
+ }
+
+ // `>` with nothing after it is a quote that holds nothing, which the schema does not allow and upstream
+ // writes as nothing: it holds an empty paragraph, as a quote made in the editor does.
+ if (extension.name === 'blockquote') {
+ const parse = (extension.config as { parseMarkdown?: (...args: unknown[]) => JSONContent }).parseMarkdown;
+
+ return withWholeDocumentToggle(
+ extension.extend({
+ parseMarkdown(...args: unknown[]) {
+ const quote = parse?.apply(this, args) ?? { type: 'blockquote' };
+
+ return quote.content?.length ? quote : { ...quote, content: [{ type: 'paragraph' }] };
+ },
+ // Upstream puts an empty quoted line between every two blocks; the blocks of a quote are
+ // joined the way the blocks of the document are, a list right under its lead-in included.
+ renderMarkdown: (node: JSONContent, helpers: MarkdownRendererHelpers) =>
+ joinBlocks(node.content ?? [], renderWith(helpers), false)
+ .split('\n')
+ .map((line) => (line.trim() ? `> ${line}` : '>'))
+ .join('\n'),
+ }),
+ 'toggleBlockquote',
+ wrapFamily,
+ );
+ }
- // Emit the code mark's content with NO backtick fence: the real content-sized fence comes from
- // serializeCodeSpan in the text encoder (markdown-editor-marked.ts). The two are paired — restoring a
- // fence here double-wraps every code span.
- if (extension.name === 'code') {
- return extension.extend({
- renderMarkdown(node: JSONContent, helpers: MarkdownRendererHelpers) {
- return node.content ? helpers.renderChildren(node.content) : '';
+ if (extension.name === 'heading') {
+ return withWholeDocumentToggle(
+ extension.extend({ renderMarkdown: renderHeading }),
+ 'toggleHeading',
+ blockTypeFamily,
+ );
+ }
+
+ // tiptap's input-rule runner skips a match whose text differs from `textBetween` over the same span.
+ // A hard break is "\n" to the matcher but "" to textBetween unless its spec has leafText, so a mark
+ // typed right after or across Shift+Enter stays literal while the same text loads formatted.
+ // extendNodeSchema runs for EVERY node of the schema, hence the name check: without it horizontalRule
+ // and image would report the text too.
+ if (extension.name === 'hardBreak') {
+ return extension.extend({
+ // How the break was written: nothing but the line's end, two or more spaces, or a backslash. It
+ // has to be in the DOM too — ProseMirror reads an edited paragraph back from there.
+ addAttributes() {
+ return {
+ marker: {
+ default: null,
+ keepOnSplit: false,
+ parseHTML: (element: HTMLElement) => element.getAttribute('data-break'),
+ renderHTML: ({ marker }: { marker?: unknown }) =>
+ typeof marker === 'string' ? { 'data-break': marker } : {},
},
- });
- }
+ };
+ },
+ extendNodeSchema(node: { name: string }) {
+ return node.name === 'hardBreak' ? { leafText: () => '\n' } : {};
+ },
+ parseMarkdown: (token: MarkdownToken) => {
+ const marker = (token.raw ?? '').replace(/\n$/, '');
+
+ return { type: 'hardBreak', ...(marker === ' ' ? {} : { attrs: { marker } }) };
+ },
+ renderMarkdown: (node: JSONContent) => {
+ const marker: unknown = node.attrs?.marker;
+
+ return `${typeof marker === 'string' && /^(?: {2,}|\\)?$/.test(marker) ? marker : ' '}\n`;
+ },
+ });
+ }
- return extension;
+ // The form a link was written in, for renderInline to write it in again.
+ if (extension.name === 'link') {
+ return extension.extend({
+ // linkify ends a URL inside the action it runs into, and the link then cuts the action in two:
+ // `https://host/{{ .Path }}` typed or pasted stays text, as it does when it is loaded.
+ addOptions(this: { parent?: () => unknown }) {
+ const options = this.parent?.() as { shouldAutoLink: (url: string) => boolean };
+
+ return {
+ ...options,
+ shouldAutoLink: (url: string) => !/\{\{|\}\}/.test(url) && options.shouldAutoLink(url),
+ } as never;
+ },
+ parseMarkdown: (token: MarkdownToken, helpers: MarkdownParseHelpers) => {
+ const opener = (token.raw ?? '[')[0];
+ const form = opener === '[' ? null : opener === '<' ? 'angle' : 'bare';
+
+ const target = { href: token.href, title: token.title || null };
+ // An image takes no mark: the link around one is kept on the image, and written from there.
+ const content = helpers
+ .parseInline(token.tokens ?? [])
+ .map((node) =>
+ node.type === 'image' ? { ...node, attrs: { ...node.attrs, link: target } } : node,
+ );
+
+ return helpers.applyMark('link', content, { ...target, ...(form ? { form } : {}) });
+ },
+ });
+ }
+
+ // `***`, `___` and `- - -` are rules as much as `---` is, and are written back as they were.
+ if (extension.name === 'horizontalRule') {
+ return extension.extend({
+ renderMarkdown: (node: JSONContent) => {
+ const rule: unknown = node.attrs?.rule;
+
+ return typeof rule === 'string' && isRule(rule) ? rule : '---';
+ },
+ });
+ }
+
+ if (extension.name === 'paragraph') {
+ return extension.extend({
+ // Four columns of space before the first word make the line code, here and after an item's
+ // marker. marked drops fewer than that, so text typed after them is written without them.
+ renderMarkdown(node: JSONContent, helpers: MarkdownRendererHelpers) {
+ const text = renderInline(node.content ?? [], helpers);
+
+ return escapeSetextUnderlines(
+ escapeLineLeadingBlockMarkers(/^(?: {4}| {0,3}\t)/.test(text) ? text.trimStart() : text),
+ );
+ },
});
+ }
+
+ // Emit the code mark's content with NO backtick fence: the real content-sized fence comes from
+ // serializeCodeSpan in the text encoder (markdown-editor-marked.ts). The two are paired — restoring a
+ // fence here double-wraps every code span.
+ // A span's line breaks are break nodes like a paragraph's (withLineBreaks).
+ if (extension.name === 'code') {
+ return extension.extend({
+ addInputRules(this: WithParentRules) {
+ return outsideActions(this.parent?.() ?? []) as never;
+ },
+ addPasteRules(this: WithParentRules) {
+ return outsideActions(this.parent?.() ?? []) as never;
+ },
+ parseMarkdown: (token: MarkdownToken, helpers: MarkdownParseHelpers) => {
+ const text = token.text ?? '';
+ // The fence the span was written with, where it is not the one the writer would choose.
+ const fence = /^`+/.exec(token.raw ?? '')?.[0].length ?? 0;
+ const longest = (text.match(/`+/g) ?? []).reduce((max, run) => Math.max(max, run.length), 0);
+
+ return helpers.applyMark(
+ 'code',
+ text
+ .split('\n')
+ .flatMap((line, at) => [
+ ...(at ? [{ attrs: { marker: '' }, type: 'hardBreak' }] : []),
+ ...(line ? [helpers.createTextNode(line)] : []),
+ ]),
+ fence && fence !== longest + 1 ? { fence } : undefined,
+ );
+ },
+ renderMarkdown(node: JSONContent, helpers: MarkdownRendererHelpers) {
+ return node.content ? helpers.renderChildren(node.content) : '';
+ },
+ });
+ }
+
+ return extension;
+};
+
+const TunedStarterKit = StarterKit.extend({
+ addExtensions() {
+ return (this.parent?.() ?? []).map((extension) =>
+ withMarkedBreaks(withLayoutFacts(tuneStarterKitExtension(extension))),
+ );
},
});
@@ -546,30 +984,48 @@ export const createMarkdownExtensions = (placeholder?: string) => [
link: { autolink: true, linkOnPaste: true, openOnClick: false },
underline: false,
}),
- withWholeDocumentToggle(TunedCodeBlock, 'toggleCodeBlock', blockTypeFamily),
+ withWholeDocumentToggle(withLayoutFacts(TunedCodeBlock), 'toggleCodeBlock', blockTypeFamily),
HeadingAutoformat,
- TunedTable.configure({ resizable: true }),
+ withLayoutFacts(TunedTable).configure({ resizable: true }),
TableRow,
TableHeader,
TableCell,
- withWholeDocumentToggle(guardBlockTokenizer(TaskList, OPENS_TASK_LIST), 'toggleTaskList', listFamily),
+ withWholeDocumentToggle(
+ withLayoutFacts(guardBlockTokenizer(TaskList, OPENS_TASK_LIST)),
+ 'toggleTaskList',
+ listFamily,
+ ),
TaskItem.configure({
a11y: {
- checkboxLabel: (node) => `Task item checkbox for ${node.firstChild?.textContent || 'empty task item'}`,
+ checkboxLabel: (node) =>
+ `Task item checkbox for ${node.firstChild?.textContent.trim() || 'empty task item'}`,
},
nested: true,
}),
Image.extend({
renderMarkdown(node: JSONContent) {
- const { alt = '', src = '', title = '' } = node.attrs ?? {};
+ const { alt = '', link, src = '', title = '' } = node.attrs ?? {};
const label = escapeBracketsInLabel(String(alt));
+ const image = title ? `![${label}](${src} "${title}")` : `![${label}](${src})`;
+ const { href, title: linkTitle } = (link ?? {}) as { href?: unknown; title?: unknown };
+
+ if (typeof href !== 'string' || !href) {
+ return image;
+ }
+
+ // A target with a space or a parenthesis stands between angle brackets and a quote in the title
+ // behind a backslash; where even that is read as another link, the image is written without one.
+ const caption = typeof linkTitle === 'string' && linkTitle ? linkTitle : null;
+ const target = /[\s()]/.test(href) ? `<${href}>` : href;
+ const linked = `[${image}](${target}${caption === null ? '' : ` "${caption.replaceAll('"', '\\"')}"`})`;
- return title ? `![${label}](${src} "${title}")` : `![${label}](${src})`;
+ return readsAsLinkedImage(linked, href, caption) ? linked : image;
},
}),
VariableHighlight,
TagHighlight,
Placeholder.configure({ emptyEditorClass: 'is-editor-empty', placeholder }),
...createMarkdownLayer(),
+ MarkdownLayout,
MarkdownPaste,
];
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.test.ts
index 755d12d5..a2977249 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.test.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.test.ts
@@ -14,6 +14,7 @@ const para = (...content: JSONContent[]): JSONContent => ({ content, type: 'para
const t = (text: string, marks?: JSONContent['marks']): JSONContent =>
marks ? { marks, text, type: 'text' } : { text, type: 'text' };
const hb: JSONContent = { type: 'hardBreak' };
+const lineBreak: JSONContent = { attrs: { marker: '' }, type: 'hardBreak' };
const newEditor = (content: JSONContent | string) =>
new Editor({ content: content as string, extensions: createMarkdownExtensions() });
@@ -192,8 +193,11 @@ describe('multi-line blocks (hardBreak / Shift+Enter) stay body text', () => {
expect(roundTrip(md)).toBe(md);
});
- it('a multi-line block "# a"⏎"body" stays a paragraph and round-trips', () => {
- const { heading, md, reloadHeading } = promote(doc(para(t('z# A'), hb, t('body'))), 'z# A');
+ it.each([
+ ['a hard break', hb],
+ ['a line break written as nothing', lineBreak],
+ ])('a multi-line block "# a"⏎"body" over %s stays a paragraph and round-trips', (_name, br) => {
+ const { heading, md, reloadHeading } = promote(doc(para(t('z# A'), br, t('body'))), 'z# A');
expect(heading).toBe(0);
expect(reloadHeading).toBe(0);
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.ts
index f203e7b2..2d010460 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.ts
@@ -27,9 +27,10 @@ const containsHardBreak = (node: PMNode, hardBreak: null | PMNode['type']): bool
// Two guards keep the promotion sound:
// • it keys off the FIRST child, not textContent: a `# ` AFTER a hardBreak (Shift+Enter) must stay body text,
// and there the first child is the pre-break text, so the block does not read as starting with `#`;
-// • it skips a paragraph that CONTAINS a hardBreak: a heading is single-line, so promoting a multi-line block
-// (e.g. `# a`⏎`# b`) would emit `# a \n# b`, which re-parses as TWO headings on the next load. Such a
-// block stays a paragraph; escapeLineLeadingBlockMarkers then escapes its leading `# ` so it round-trips.
+// • it skips a paragraph that CONTAINS a line break, hard or soft: a heading is single-line, so promoting a
+// multi-line block (e.g. `# a`⏎`# b`) would emit `# a \n# b`, which re-parses as TWO headings on the next
+// load. Such a block stays a paragraph; escapeLineLeadingBlockMarkers then escapes its leading `# ` so it
+// round-trips.
export const HeadingAutoformat = Extension.create({
addProseMirrorPlugins() {
return [
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-inline-scan.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-inline-scan.ts
index 541744da..b39d721e 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-inline-scan.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-inline-scan.ts
@@ -10,9 +10,10 @@ export interface InlineMatch {
// scan, this reunites a token split across text nodes by a mark (e.g. a user styles one brace of `{{.Var}}`,
// so ProseMirror splits it) — the brace and the rest sit in one block string again. ProseMirror positions are
// one per UTF-16 unit and mark-independent; `from`/`to` are read from the per-character position map (NOT
-// `from + length`) so a token that also contains a non-text inline node — a hard break from Shift+Enter —
-// still spans the right range. `regex` MUST be global (`/g`). Scanning per textblock — NOT over
-// `doc.textContent` — keeps positions aligned across blocks.
+// `from + length`) so a token that also contains a non-text inline node still spans the right range. A line
+// break is one such node and one character of the scanned text, the newline it is written as: `{{ if\n.X }}`
+// reads here the way it reads in the markdown. `regex` MUST be global (`/g`). Scanning per textblock — NOT
+// over `doc.textContent` — keeps positions aligned across blocks.
export const collectInlineMatches = (doc: PMNode, regex: RegExp): InlineMatch[] => {
const matches: InlineMatch[] = [];
@@ -31,6 +32,9 @@ export const collectInlineMatches = (doc: PMNode, regex: RegExp): InlineMatch[]
text += child.text[i];
positions.push(inlinePos + i);
}
+ } else if (child.type.name === 'hardBreak') {
+ text += '\n';
+ positions.push(inlinePos);
}
inlinePos += child.nodeSize;
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-layout.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-layout.test.ts
new file mode 100644
index 00000000..cfe4df24
--- /dev/null
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-layout.test.ts
@@ -0,0 +1,1685 @@
+import type { JSONContent } from '@tiptap/core';
+
+import { Editor } from '@tiptap/core';
+import { beforeAll, describe, expect, it } from 'vitest';
+
+import { GROWTH_IF_QUADRATIC, slowdownWhenInputQuadruples } from '@/test-utils/cost-growth';
+
+import { createMarkdownExtensions } from './markdown-editor-extensions';
+import { markdownCodec, roundTrip, setupEditorJsdom } from './markdown-editor-test-setup';
+
+beforeAll(setupEditorJsdom);
+
+const load = (content: JSONContent | string) =>
+ new Editor({
+ content,
+ ...(typeof content === 'string' ? { contentType: 'markdown' as const } : {}),
+ extensions: createMarkdownExtensions(),
+ });
+
+const kindsOf = (markdown: string): string[] => {
+ const editor = load(markdown);
+ const kinds = (editor.getJSON().content ?? []).map((block) => block.type ?? '');
+
+ editor.destroy();
+
+ return kinds;
+};
+
+const bullets = (text: string): JSONContent => ({
+ content: [{ content: [{ content: [{ text, type: 'text' }], type: 'paragraph' }], type: 'listItem' }],
+ type: 'bulletList',
+});
+
+// What the document is after `edit`, and what it is saved as.
+const afterEdit = (content: JSONContent | string, edit: (editor: Editor) => void): string => {
+ const editor = load(content);
+
+ edit(editor);
+
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ return saved;
+};
+
+describe('a document is saved the way it was written', () => {
+ it.each([
+ ['text right under a heading', '# Title\ntext\n\nmore'],
+ ['a list right under a heading', '## Findings\n- one\n- two'],
+ ['a numbered list right under a heading', '## Steps\n3. three\n4. four'],
+ ['a fence right under a heading', '### Run\n```sh\nid\n```'],
+ ['a fence right under a label', '**Request:**\n```http\nGET / HTTP/1.1\n```'],
+ ['text right under a fence', '```sh\nid\n```\nIt prints the uid.'],
+ ['a heading right under text', 'intro\n## Next'],
+ ['a list right under text', 'Checked:\n- one\n- two'],
+ ['a numbered list right under text', 'Steps:\n1. one\n2. two'],
+ ['a table right under a heading', '## Ports\n| p | s |\n|---|---|\n| 22 | ssh |'],
+ ['a table right under text', 'Ports:\n| p | s |\n|---|---|\n| 22 | ssh |'],
+ ['a quote right under text', 'He said:\n> no'],
+ ['a starred rule right under text', 'text\n***\nmore'],
+ ['a rule right under an image', '![a](http://x.test/i.png)\n***'],
+ ['a list right under a list of another bullet', '- a\n* b\n+ c'],
+ ['a numbered list right under a bullet list', '- a\n2. b'],
+ ['a quote right under a list', '- a\n> q'],
+ ['a dashed rule right under a quote', '> q\n---\nafter'],
+ ['a dashed rule right under a table', '| a |\n|---|\n| 1 |\n---\nafter'],
+ ['a task list right under a heading', '# H\n- [ ] a\n- [x] b'],
+ ['a comment right under a list', '- one\n<!-- note -->\ntext'],
+ ['a list with an empty item under a starred rule under text', 'text\n***\n- a\n-'],
+ ['a blank line written with spaces in it', 'a\n \nb'],
+ ['two blank lines between two blocks', '# Title\n\n\ntext\n\n\n- a\n- b'],
+ ['four and six blank lines between two blocks', 'a\n\n\n\n\nb\n\n\n\n\n\n\nc'],
+ ['two blank lines between the blocks of a quote', '> a\n>\n>\n> b'],
+ ['two blank lines between the blocks of a list item', '- a\n\n\n b\n- c'],
+ ['a blank line written with a tab in it', '# T\n \t\n- x'],
+ ['a tab on the line under text, which is a line of it', 'para\n\t\n# H'],
+ ['a blank line written with a tab in it, under a list', '- a\n\t\ntext'],
+ ['a numbered list of checkboxes written two spaces in', ' 1. [ ] a\n 2. [ ] b'],
+ ['a list written two spaces in', 'Checked:\n\n - one\n - two\n\nafter'],
+ ['a blank line in a list item written with more spaces than the item is indented by', '- b\n \n c'],
+ [
+ "a blank line between the blocks of an item, written with the item's indentation",
+ '1. Run:\n ```\n id\n ```\n \n It prints the uid.\n2. next',
+ ],
+ ['a blank line between two items, written with spaces', '1. one\n - nested\n \n2. two'],
+ ['a blank line between two nested items, written with spaces', '- a\n - b\n \n - c\n- d'],
+ [
+ 'a list written two spaces in, with spaces past them on the blank line between its items',
+ 'Steps:\n\n - a\n \n - b',
+ ],
+ [
+ "an empty line of code in a list item, written with the item's indentation",
+ '1. Run:\n ```sh\n id\n \n whoami\n ```\n2. next',
+ ],
+ ['the same in a nested item', '- a\n - b\n ```sh\n x\n \n y\n ```\n- c'],
+ ['the same with fewer spaces than the item is indented by', '1. step\n ```\n a\n \n b\n ```'],
+ [
+ 'the same in a list written two spaces in',
+ 'Steps:\n\n 1. Run:\n ```\n a\n \n b\n ```\n \n Done.',
+ ],
+ ["the same under a fence on the marker's line", '1. ```\n a\n \n b\n ```'],
+ ['a fence written three spaces in, with its code', 'Run:\n\n ```sh\n id\n ```\n\nDone.'],
+ ['a closing fence longer than the opening one, with spaces after it', '```\nid\n````` \n\nDone.'],
+ ['a fence written three spaces in, with an empty last line of code', ' ```\n id\n\n ```'],
+ ['a fence with spaces before its language and after it', '``` sh \nid\n```'],
+ ['a tilde fence one space in, right under text', 'Run:\n ~~~\n id\n ~~~'],
+ ['a fence in a quote, two spaces in', '> Run:\n>\n> ```\n> id\n> ```'],
+ ['code written by indentation', 'Run:\n\n nmap -sV host\n \n curl host \n\nDone.'],
+ ['code indented with a tab', 'Run:\n\n\tnmap -sV host\n\tcurl host\n\nDone.'],
+ ['indented code in a quote', '> Run:\n>\n> nmap -sV host\n\nDone.'],
+ ['indented code right under a heading and right above text', '# Run\n nmap -sV host\nDone.'],
+ ['indented code under a quote', '> Run:\n\n nmap -sV host'],
+ ['two indented blocks that a comment keeps apart', ' one\n\n<!-- c -->\n\n two'],
+ [
+ 'bullets written two spaces in under numbered steps, which is not deep enough to nest them',
+ '1. Identify the source:\n - which sensor\n - which rule\n2. Check the host:\n - look it up',
+ ],
+ ['a numbered list written three spaces in, with a wrapped item', ' 1. one\n more\n 2. two'],
+ ['a nested list written one space past its item', '- a\n - b\n - c\n- d'],
+ ['a one-line heading over a row of equals signs', 'Title\n=====\n\ntext'],
+ ['a list with spaces after its last line', '- one\n- **Payload**: \n```\nid\n```\n\n1. a\n - b \n\ntext'],
+ ['a heading with spaces after it', '### Pattern C: `os.popen()` \n\ntext\n\n## Next \n- a'],
+ ['a link around an image beside a bare address', '[![a](https://img.test/s.png)](http://h.test) http://x.test'],
+ ['a link whose title holds a quote, around an image', '[![a](s.png)](http://h.test "say \\"hi\\"")'],
+ ['a link whose target holds a space, around an image', '[![a](s.png)](<http://h.test/a b>)'],
+ ['a link whose target holds a parenthesis, around an image', '[![a](s.png)](<http://h.test/a)b>)'],
+ ["text two spaces in under a numbered list, which is not deep enough to be its item's", '1. step\n\n Note.'],
+ ['text under text two spaces in under a numbered list', '1. step\n\n Note.\n\n More.'],
+ ['a tag two spaces in under a numbered list', '1. step\n\n <b>Note</b> under the list.'],
+ ['a link definition two spaces in under a numbered list', '1. step\n\n [ref]: http://x.test'],
+ ['two blank lines between a list of stars and a task list', '* item\n\n\n- [ ] task'],
+ ['five blank lines under a task list', '- [ ] task\n\n\n\n\n\npara'],
+ ['seven blank lines under a task list in a quote', '> - [ ] task\n\n\n\n\n\n\n\npara'],
+ ['three blank lines between two task lists', '- [ ] t\n\n\n\n- [ ] u'],
+ ['text two spaces in under an item that opens with code', '- foo\n\n bar'],
+ ['text two spaces in under an empty item', '-\n\n foo'],
+ ['text three spaces in under an item numbered with two digits', '10. step\n\n Note.'],
+ ['text one space in under a bullet list', '- one\n\n two'],
+ ['text as far in as the markers of the list above it', ' 1. a\n 2. b\n\n Return ONLY the JSON.'],
+ ['four blank lines between a task list and a bullet list', '- [ ] a\n\n\n\n\n- b'],
+ ['two blank lines between a bullet list and a numbered one', '- a\n\n\n1. b'],
+ ['two blank lines between lists of two bullets', '* item\n\n\n- item'],
+ ['a table whose header was written without a pipe', 'Name\n|---|\nvalue\n| `x|y` |'],
+ ['a last paragraph of one ideographic space', 'x\n\n\u3000'],
+ ['a link around an image', '[![build](https://img.test/b.svg)](https://ci.test/run) and text'],
+ [
+ 'a link with a title around an image with one',
+ 'see [![a](https://img.test/a.png "shot")](https://h.test/ "open")',
+ ],
+ ['a one-line heading over a row of dashes', 'text\n\nSub\n---'],
+ ['front matter of one key', '---\ntitle: Nightly job\n---\n\nBody'],
+ ['a closing tag right under a table', '| a | b |\n|---|---|\n| 1 | 2 |\n</details>\ntext'],
+ [
+ 'a line of text over a row of dashes and pipes, right under text',
+ '{{ range .R }}\n| {{ .A }} | {{ .B }} |\n{{ else }}\n| - | - |\n{{ end }}',
+ ],
+ ['a tag line right under a tag line', '<id>{{.ID}}</id>\n<title>{{.Title}}</title>'],
+ ['a closing tag line right under text', 'text\n</details>\nmore'],
+ ['an opening tag right under a list', '- one\n- two\n<steps>\ntext'],
+ ['blank lines kept where they were written', '# Title\n\ntext\n\n- one\n\n```\nx\n```'],
+ ['a loose list', '1. one\n\n2. two\n\n3. three'],
+ ['a loose task list', '- [ ] one\n\n- [ ] two'],
+ ['a list loose in one place', '- a\n- b\n\n- c'],
+ ['a blank line inside an item', '1. Run:\n\n ```sh\n id\n ```\n2. next'],
+ ['two blank lines between items', '- a\n\n\n- b'],
+ ['three blank lines between blocks', 'a\n\n\n\nb'],
+ ])('%s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ ['a row of three dashes', '| a | b |\n|---|---|\n| 1 | 2 |'],
+ ['dashes as wide as the header', '| Tool | Purpose |\n|------|---------|\n| nmap | scan |'],
+ ['dashes of any width', '| a | b |\n|--|-------|\n| 1 | 2 |'],
+ ['alignment colons', '| L | C | R |\n|:--|:-:|--:|\n| a | b | c |'],
+ ['an empty cell', '| a | b |\n|---|---|\n| 1 | |\n| | 2 |'],
+ [
+ 'a table in a list item, its rows padded each in its own way',
+ '- Ports:\n\n | p | s |\n |---|:-:|\n |22|ssh|\n | 80 | http |',
+ ],
+ [
+ 'rows padded each in its own way',
+ '| Name | Value |\n|------|:-----:|\n| a | 1 |\n| longer name | 2 |\n|c|3|',
+ ],
+ ['a row without its outer pipes and one with spaces after it', '| a | b |\n| --- | --- |\n1 | 2\n| 3 | 4 | '],
+ ['a table written three spaces in', 'Ports:\n\n | p | s |\n |---|---|\n | 22 | ssh |'],
+ ['a line of text among the rows', '| a | b |\n|---|---|\n| 1 | 2 |\nValues above are reserved.\n| 3 | 4 |'],
+ ['two rows that hold the same cells', '| a | b |\n|---|---|\n|1|2|\n| 1 | 2 |\n| 1 | 2 |'],
+ ['two spaces in an empty cell', '| a | b |\n|---|---|\n| 1 | |\n| | 2 |'],
+ ['a cell that ends with a backslash', '| a | b |\n|---|---|\n| C:\\dir\\ | x |'],
+ ['a pipe escaped in a cell', '| a | b |\n|---|---|\n| `x \\| y` | 2 |'],
+ ['a pipe written bare in a code span', '| a | b |\n|---|---|\n| `x | y` | 2 |'],
+ ['a Go action with a pipe', '| a | b |\n|---|---|\n| {{.X|upper}} | 2 |'],
+ ['a padded table', '| a | b |\n| --- | --- |\n| 1 | 2 |'],
+ ])('a table with %s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ ['starred bullets', '* a\n* b'],
+ ['plus bullets', '+ a\n+ b'],
+ ['a paren delimiter', '3) a\n4) b'],
+ ['every item numbered one', '1. a\n1. b\n1. c'],
+ ['numbers that are data', '22. ssh\n80. http\n443. https'],
+ ['a count that jumps', '1. a\n2. b\n5. c\n6. d'],
+ ['four spaces after a bullet', '- four spaces\n- b'],
+ ['three spaces after a bullet', '* a\n ```sh\n id\n ```\n* b'],
+ ['numbers padded to one column', '9. nine\n10. ten'],
+ ['a starred rule in a dashed item', '- ***\n- b'],
+ ['a dashed rule in a starred item', '* ---\n* b'],
+ ])('a list with %s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ ['aligned to its parent', '1. step\n - detail\n This confirms it.\n2. next'],
+ ['at the margin under a nested item', '1. step\n - detail\n</steps>'],
+ ['aligned to the middle of three levels', '- a\n - b\n - c\n aligned to a'],
+ ['four levels deep', '- a\n - b\n - c\n - d\n aligned to c'],
+ ['at the margin under three levels', '- a\n - b\n - c\ntail'],
+ ['after an indented one', '- first line\n second line\nthird line'],
+ ['followed by an indented one', '- **Repo**: x\ndef f(order_id):\n user_id = 1\n # deeper'],
+ ])('a line left of its item, %s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ ['a tilde fence', '~~~python\nprint(1)\n~~~'],
+ ['a four-backtick fence around fences', '````md\n```\nx\n```\n````'],
+ ['a four-backtick fence around text', '````\ncode\n````'],
+ ['a fence opener inside a fence', '```md\n```bash\nid\n```'],
+ ['a rule of underscores', 'a\n\n___\n\nb'],
+ ['a spaced rule', 'a\n\n- - -\n\nb'],
+ ])('%s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ ['one line break', '# Title\n\n- a\n- b\n'],
+ ['two line breaks', '# Big\n\n'],
+ ['a closed fence and one line break', 'text\n\n```bash\necho hi\n```\n'],
+ ['a fence nothing closes', 'text\n\n```bash\necho hi\n'],
+ ['a fence nothing closes, without a line break', 'text\n\n```bash\necho hi'],
+ ['an empty fence nothing closes', '```\n'],
+ ['a fence nothing closes, with blank lines in it', '```\ncode\n\n\n'],
+ ['a code span that holds one space', 'text\n\n` `'],
+ ])('a document that ends with %s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+});
+
+describe('what was written gives way to an edit', () => {
+ it('keeps the empty paragraph put above a block that stood right under text', () => {
+ const saved = afterEdit('Label:\n```\ncode\n```\n\nnext', (editor) =>
+ editor.commands.insertContentAt(7, { type: 'paragraph' }),
+ );
+
+ expect(saved).toBe('Label:\n\n\n\n```\ncode\n```\n\nnext');
+ expect(kindsOf(saved)).toEqual(['paragraph', 'paragraph', 'codeBlock', 'paragraph']);
+ });
+
+ it('keeps the paragraph that stood right under a heading once it is empty', () => {
+ const saved = afterEdit('# H\ntext\n\nnext', (editor) => editor.commands.deleteRange({ from: 4, to: 8 }));
+
+ expect(saved).toBe('# H\n\n\n\nnext');
+ expect(kindsOf(saved)).toEqual(['heading', 'paragraph', 'paragraph']);
+ });
+
+ it('puts a blank line above a dashed rule once the heading above it is text', () => {
+ const saved = afterEdit('# H\n---\nafter', (editor) => editor.chain().setTextSelection(1).setParagraph().run());
+
+ expect(saved).toBe('H\n\n---\nafter');
+ expect(kindsOf(saved)).toEqual(['paragraph', 'horizontalRule', 'paragraph']);
+ });
+
+ it.each([
+ [
+ 'a list whose first item is emptied under a starred rule that stands right under text',
+ 'Intro line\n***\n- one\n- two',
+ (editor: Editor) => editor.commands.deleteRange({ from: 16, to: 19 }),
+ 'Intro line\n***\n\n-\n- two',
+ ['paragraph', 'horizontalRule', 'bulletList'],
+ ],
+ [
+ 'a two-line heading made under a starred rule that stands right under text',
+ 'Intro line\n***\nTitle line\nsecond line\n\nBody',
+ (editor: Editor) => editor.chain().setTextSelection(14).setHeading({ level: 1 }).run(),
+ 'Intro line\n***\n\nTitle line\nsecond line\n===\n\nBody',
+ ['paragraph', 'horizontalRule', 'heading', 'paragraph'],
+ ],
+ [
+ 'a block under a definition that has lost its target',
+ '[ref]: http://x\n```\ncode\n```',
+ (editor: Editor) => editor.commands.deleteRange({ from: 7, to: 16 }),
+ '[ref]:\n\n```\ncode\n```',
+ ['paragraph', 'codeBlock'],
+ ],
+ [
+ 'the fence of a block nothing closes once its code is deleted',
+ 'Label:\n```sh\nls -la',
+ (editor: Editor) => editor.commands.deleteRange({ from: 9, to: 15 }),
+ 'Label:\n```sh\n\n```',
+ ['paragraph', 'codeBlock'],
+ ],
+ ])('keeps %s a block of its own', (_name, md, edit, saved, kinds) => {
+ expect(afterEdit(md, edit)).toBe(saved);
+ expect(kindsOf(saved)).toEqual(kinds);
+ });
+
+ // marked reads text, a `***` rule right under it and a row of dashes as one heading.
+ it.each([
+ ['text', { content: [{ text: 'x', type: 'text' }], type: 'paragraph' }, 'x\n***\n\n---', '- x\n ***\n\n ---'],
+ [
+ 'an image',
+ { attrs: { alt: 'a', src: 'http://x.test/i.png' }, type: 'image' },
+ '![a](http://x.test/i.png)\n***\n\n---',
+ '- ![a](http://x.test/i.png)\n ***\n\n ---',
+ ],
+ ])('puts a blank line above a dashed rule under a starred one that stands right under %s', (...row) => {
+ const [, first, saved, savedInItem] = row;
+ const rule = (attrs: Record<string, unknown>) => ({ attrs, type: 'horizontalRule' });
+ const blocks = [first, rule({ gap: 0, rule: '***' }), rule({ gap: 0 })];
+ const item = { content: [{ content: blocks, type: 'listItem' }], type: 'bulletList' };
+
+ expect(afterEdit({ content: blocks, type: 'doc' }, () => {})).toBe(saved);
+ expect(kindsOf(saved).slice(1)).toEqual(['horizontalRule', 'horizontalRule']);
+ expect(afterEdit({ content: [item], type: 'doc' }, () => {})).toBe(savedInItem);
+ });
+
+ it('puts a blank line above a list once its first item is empty', () => {
+ const saved = afterEdit('Checked:\n- one\n- two', (editor) =>
+ editor.commands.deleteRange({ from: 13, to: 16 }),
+ );
+
+ expect(saved).toBe('Checked:\n\n-\n- two');
+ expect(kindsOf(saved)).toEqual(['paragraph', 'bulletList']);
+ });
+
+ it('puts a blank line above a numbered list that no longer starts at one', () => {
+ const saved = afterEdit('Steps:\n1. a\n5. b', (editor) => editor.commands.deleteRange({ from: 9, to: 14 }));
+
+ expect(saved).toBe('Steps:\n\n5. b');
+ expect(kindsOf(saved)).toEqual(['paragraph', 'orderedList']);
+ });
+
+ it('puts a blank line above a list whose first line is a delimiter row for the text above', () => {
+ const saved = afterEdit('a\n- | -\n- x', (editor) =>
+ editor.chain().setTextSelection(2).insertContent(' | b').run(),
+ );
+
+ expect(saved).toBe('a | b\n\n- | -\n- x');
+ expect(kindsOf(saved)).toEqual(['paragraph', 'bulletList']);
+ });
+
+ it.each([
+ [
+ 'a heading of two lines under text',
+ [
+ { content: [{ text: 'text', type: 'text' }], type: 'paragraph' },
+ {
+ attrs: { gap: 0, level: 1 },
+ content: [{ text: 'a', type: 'text' }, { type: 'hardBreak' }, { text: 'b', type: 'text' }],
+ type: 'heading',
+ },
+ ],
+ 'text\n\na \nb\n===',
+ ],
+ [
+ 'a quote under a quote',
+ [
+ { content: [{ content: [{ text: 'a', type: 'text' }], type: 'paragraph' }], type: 'blockquote' },
+ {
+ attrs: { gap: 0 },
+ content: [{ content: [{ text: 'b', type: 'text' }], type: 'paragraph' }],
+ type: 'blockquote',
+ },
+ ],
+ '> a\n\n> b',
+ ],
+ [
+ 'text under text',
+ [
+ { content: [{ text: 'one', type: 'text' }], type: 'paragraph' },
+ { attrs: { gap: 0 }, content: [{ text: 'two', type: 'text' }], type: 'paragraph' },
+ ],
+ 'one\n\ntwo',
+ ],
+ ])('puts a blank line above %s that claims to stand right under it', (_name, content, saved) => {
+ const editor = load({ content, type: 'doc' });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe(saved);
+ });
+
+ it.each([
+ ['with no blank line written between them', [bullets('a'), bullets('b')], '- a\n- b'],
+ ['with one', [bullets('a'), { ...bullets('b'), attrs: { gap: 1 } }], '- a\n- b'],
+ ['of two bullets', [bullets('a'), { ...bullets('b'), attrs: { marker: '*' } }], '- a\n\n* b'],
+ ])('writes two lists an edit left side by side, %s, as the lists they read as', (_name, content, saved) => {
+ const editor = load({ content, type: 'doc' });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe(saved);
+ });
+
+ it.each([
+ ['bullets', '- a\n- b\n- c', '- ab\n- c'],
+ ['numbers', '1. a\n2. b\n3. c', '1. ab\n2. c'],
+ ])('writes %s as one list once Backspace has joined an item into the one above', (_name, md, saved) => {
+ const out = afterEdit(md, (editor) => {
+ const press = () =>
+ editor.view.someProp('handleKeyDown', (handle) =>
+ handle(editor.view, new KeyboardEvent('keydown', { key: 'Backspace' })),
+ );
+
+ editor.commands.setTextSelection(8);
+ press();
+ press();
+ });
+
+ expect(out).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('writes two lists as one once the text between them is deleted', () => {
+ const saved = afterEdit('A\n\n- x\n\nmid\n\n- y', (editor) =>
+ editor.commands.deleteRange({ from: 10, to: 15 }),
+ );
+
+ expect(saved).toBe('A\n\n- x\n- y');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('writes a line of text right under a table as a row of it once the line is edited', () => {
+ const md = '| a | b |\n|---|---|\nback\\';
+ const saved = afterEdit(md, (editor) => {
+ let at = 0;
+
+ editor.state.doc.descendants((node, position) => {
+ at = node.text === 'back\\' ? position : at;
+ });
+ editor.chain().setTextSelection(at).insertContent('way ').run();
+ });
+
+ expect(roundTrip(md)).toBe(md);
+ expect(saved).toBe('| a | b |\n|---|---|\n| way back\\ | |');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('writes a list at the margin when one of its lines stood left of its markers', () => {
+ const saved = roundTrip(' 1. A paragraph\nwith two lines.\n\n > A quote.');
+
+ expect(saved).toBe('1. A paragraph\nwith two lines.\n\n > A quote.');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it.each([
+ ['two spaces under a bullet list', 'bulletList', 2, '- a\n\n* b'],
+ ['three spaces under a numbered list', 'orderedList', 3, '1. a\n\n* b'],
+ ['one space under a bullet list, which nests nothing', 'bulletList', 1, '- a\n\n * b'],
+ ['two spaces under a numbered list, which nest nothing', 'orderedList', 2, '1. a\n\n * b'],
+ ])(
+ 'carries the spaces a list was written after only where they would not nest it: %s',
+ (_name, type, indent, saved) => {
+ const indented = { ...bullets('b'), attrs: { indent, marker: '*' } };
+
+ expect(afterEdit({ content: [{ ...bullets('a'), type }, indented], type: 'doc' }, () => {})).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ },
+ );
+
+ it('carries the spaces a list was written after when the list under it is another', () => {
+ const indented = { ...bullets('b'), attrs: { indent: 2, marker: '*' } };
+
+ expect(afterEdit({ content: [indented, bullets('a')], type: 'doc' }, () => {})).toBe(' * b\n\n- a');
+ });
+
+ it.each([
+ ['two blank lines of which one holds spaces', 'a\n\n \nb', 'a\n\n\nb'],
+ ['a blank line that holds a non-breaking space', 'a\n\u00a0\nb', 'a\n\u00a0\nb'],
+ ['a list whose markers stand at different columns', ' - a\n - b', '- a\n- b'],
+ [
+ 'a link around an image and its caption, as a link around each',
+ '[![a](https://img.test/a.png) caption](https://h.test/)',
+ '[![a](https://img.test/a.png)](https://h.test/) [caption](https://h.test/)',
+ ],
+ [
+ 'a list written two spaces in, with only those on the blank line between its items',
+ 'Steps:\n\n - a\n \n - b',
+ 'Steps:\n\n - a\n\n - b',
+ ],
+ ['a tab on the blank line under a heading in a list item', '- # H\n\t\n b', '- # H\n\n b'],
+ [
+ 'a tab on the line under a table, which is an empty row of it',
+ '| a | b |\n|---|---|\n| 1 | 2 |\n\t\n# H',
+ '| a | b |\n|---|---|\n| 1 | 2 |\n| | |\n# H',
+ ],
+ [
+ 'empty lines of code in a list item, of which only one holds spaces',
+ '1. step\n ```\n a\n \n\n b\n ```',
+ '1. step\n ```\n a\n\n\n b\n ```',
+ ],
+ [
+ 'text as far in as the content of the list above it, which goes to the margin',
+ ' - a\n - b\n\n text',
+ '- a\n- b\n\ntext',
+ ],
+ [
+ 'an empty cell of a row that is written anew, with the two spaces the table writes one with',
+ '| a | b | c |\n|---|---|---|\n| x | | &lt;y&gt; |',
+ '| a | b | c |\n|---|---|---|\n| x | | <y> |',
+ ],
+ [
+ 'a list with a task item written two spaces in',
+ ' - plain item\n - [ ] task item',
+ '- plain item\n\n- [ ] task item',
+ ],
+ [
+ 'an underlined heading in a task item, which is written with a number sign',
+ '- plain\n- [ ] task\n\n note\n\n Heading\n ---',
+ '- plain\n\n- [ ] task\n\n note\n ## Heading',
+ ],
+ [
+ 'a table with a bare pipe in a code span, in a list item',
+ '10. Run the scan\nand read the result:\n| flag | meaning |\n|---|---|\n| `a|b` | either |',
+ '10. Run the scan\nand read the result:\n | flag | meaning |\n |---|---|\n | `a\\|b` | either |',
+ ],
+ ])('writes %s the way they read back', (_name, md, saved) => {
+ expect(roundTrip(md)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it.each([
+ ['a blank line that claims to hold text', { blank: 'x', gap: 1 }, 'one\n\ntwo'],
+ ['text that claims to stand right under a table', { gap: 0 }, '| a |\n| --- |\n| 1 |\n\ntwo'],
+ [
+ 'a tab on the blank line under a quote, which would keep the text in it',
+ { blank: '\t', gap: 1 },
+ '> one\n\ntwo',
+ ],
+ ['a tab on the blank line under text, which would be a line of it', { blank: '\t', gap: 1 }, 'one\n\ntwo'],
+ [
+ 'a tab on the blank line under a table, which would be a row of it',
+ { blank: '\t', gap: 1 },
+ '| a |\n| --- |\n| 1 |\n\ntwo',
+ ],
+ ])('does not write %s', (_name, attrs, saved) => {
+ const paragraph = (text: string, facts?: Record<string, unknown>) => ({
+ attrs: facts,
+ content: [{ text, type: 'text' }],
+ type: 'paragraph',
+ });
+ const cell = (type: string, text: string) => ({ content: [paragraph(text)], type });
+ const table = {
+ content: [
+ { content: [cell('tableHeader', 'a')], type: 'tableRow' },
+ { content: [cell('tableCell', '1')], type: 'tableRow' },
+ ],
+ type: 'table',
+ };
+ const quote = { content: [paragraph('one')], type: 'blockquote' };
+ const first = saved.startsWith('|') ? table : saved.startsWith('>') ? quote : paragraph('one');
+
+ expect(afterEdit({ content: [first, paragraph('two', attrs)], type: 'doc' }, () => {})).toBe(saved);
+ });
+
+ it.each<[string, (editor: Editor) => void, string]>([
+ [
+ 'the empty paragraph above it is removed',
+ (editor) => editor.commands.deleteRange({ from: 3, to: 5 }),
+ 'a\n\nb',
+ ],
+ [
+ 'another empty paragraph is put above it',
+ (editor) => editor.commands.insertContentAt(3, { type: 'paragraph' }),
+ 'a\n\n\n\n\n\nb',
+ ],
+ ])(
+ 'writes a block that stood under four blank lines under the ones its paragraphs make once %s',
+ (_name, edit, saved) => {
+ const written = afterEdit('a\n\n\n\n\nb', edit);
+
+ expect(written).toBe(saved);
+ expect(roundTrip(written)).toBe(written);
+ },
+ );
+
+ it.each<[string, unknown, JSONContent[], string]>([
+ ['the spaces after the last line of a list', ' ', [], '- a \n\ntext'],
+ ['nothing after the last line of a list where what stood there is not spaces', 'x', [], '- a\n\ntext'],
+ ['nothing after it where what stood there holds a tab', ' \t', [], '- a\n\ntext'],
+ ])('writes %s', (_name, trail, _under, saved) => {
+ const paragraph = { content: [{ text: 'text', type: 'text' }], type: 'paragraph' };
+
+ expect(afterEdit({ content: [{ ...bullets('a'), attrs: { trail } }, paragraph], type: 'doc' }, () => {})).toBe(
+ saved,
+ );
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it.each<[string, JSONContent, JSONContent[], string]>([
+ ['at the end of the document, where nothing reads them back', bullets('a'), [], '- a'],
+ [
+ 'above a list, where they would be text of the item',
+ bullets('a'),
+ [{ ...bullets('b'), attrs: { marker: '*' } }],
+ '- a\n\n* b',
+ ],
+ [
+ 'above nothing but an empty paragraph and a list',
+ bullets('a'),
+ [{ type: 'paragraph' }, { ...bullets('b'), attrs: { marker: '*' } }],
+ '- a\n\n\n\n* b',
+ ],
+ [
+ 'after a quote that ends the list',
+ {
+ content: [
+ {
+ content: [
+ { content: [{ text: 'a', type: 'text' }], type: 'paragraph' },
+ {
+ content: [{ content: [{ text: 'q', type: 'text' }], type: 'paragraph' }],
+ type: 'blockquote',
+ },
+ ],
+ type: 'listItem',
+ },
+ ],
+ type: 'bulletList',
+ },
+ [{ content: [{ text: 'text', type: 'text' }], type: 'paragraph' }],
+ '- a\n > q\n\ntext',
+ ],
+ ])('does not write the spaces a list ended with %s', (_name, list, under, saved) => {
+ const written = afterEdit({ content: [{ ...list, attrs: { trail: ' ' } }, ...under], type: 'doc' }, () => {});
+
+ expect(written).toBe(saved);
+ });
+
+ it('keeps the spaces of the blank line above a block once the block above it is another', () => {
+ const saved = afterEdit('x\n\na\n \nb\n\nc', (editor) => editor.commands.deleteRange({ from: 3, to: 6 }));
+
+ expect(saved).toBe('x\n \nb\n\nc');
+ });
+
+ it.each<[string, string, (editor: Editor) => void, string]>([
+ [
+ 'stands beside the block that had spaces above it',
+ 'a\n \nb',
+ (editor) => editor.chain().setTextSelection(2).splitBlock().run(),
+ 'a\n\n\n\nb',
+ ],
+ [
+ 'is what is left of the block that had spaces above it',
+ 'a\n \nb\n\nc',
+ (editor) => editor.commands.deleteRange({ from: 4, to: 5 }),
+ 'a\n\n\n\nc',
+ ],
+ ])('writes a plain blank line where an empty paragraph %s', (_name, md, edit, expected) => {
+ const saved = afterEdit(md, edit);
+
+ expect(saved).toBe(expected);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('does not carry the spaces of a list under a list that only an empty paragraph keeps apart', () => {
+ const saved = afterEdit('- prev\n\nSteps:\n\n 1. x\n 2. y', (editor) =>
+ editor.view.dispatch(editor.state.tr.delete(11, 17)),
+ );
+
+ expect(saved).toBe('- prev\n\n\n\n1. x\n2. y');
+ expect(kindsOf(saved)).toEqual(['bulletList', 'paragraph', 'orderedList']);
+ });
+
+ it('does not carry the spaces of a list that holds a line left of its items', () => {
+ const item = (content: JSONContent[], attrs?: Record<string, unknown>) => ({
+ attrs,
+ content: [{ content, type: 'paragraph' }],
+ type: 'listItem',
+ });
+ const lazy = item(
+ [
+ { text: 'c', type: 'text' },
+ { attrs: { marker: '' }, type: 'hardBreak' },
+ { text: 'lazy', type: 'text' },
+ ],
+ { lazyFrom: 1 },
+ );
+ const list = { attrs: { indent: 2 }, content: [item([{ text: 'a', type: 'text' }]), lazy], type: 'bulletList' };
+
+ expect(afterEdit({ content: [list], type: 'doc' }, () => {})).toBe('- a\n- c\nlazy');
+ });
+
+ it.each([
+ ['one blank line above it', { blank: ' ', gap: 1 }, '- a\n \n- b'],
+ ['no blank line above it', { blank: ' ', gap: 0 }, '- a\n- b'],
+ ['two blank lines above it', { blank: ' ', gap: 2 }, '- a\n\n- b'],
+ ['a tab on the blank line above it', { blank: '\t', gap: 1 }, '- a\n\n- b'],
+ ['text on the blank line above it', { blank: 'x', gap: 1 }, '- a\n\n- b'],
+ ])(
+ 'writes the spaces of the blank line above an item only as spaces on that one line: %s',
+ (_name, attrs, saved) => {
+ const [first] = bullets('a').content ?? [];
+ const [second] = bullets('b').content ?? [];
+
+ expect(
+ afterEdit(
+ { content: [{ content: [first!, { ...second, attrs }], type: 'bulletList' }], type: 'doc' },
+ () => {},
+ ),
+ ).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ },
+ );
+
+ it('puts text that reads as too many cells right under the table it was cut from', () => {
+ const paragraph = (text: string, attrs?: Record<string, unknown>) => ({
+ attrs,
+ content: [{ text, type: 'text' }],
+ type: 'paragraph',
+ });
+ const cell = (type: string, text: string) => ({ content: [paragraph(text)], type });
+ const table = {
+ attrs: { delimiterRow: '|---|---|' },
+ content: [{ content: [cell('tableHeader', 'a'), cell('tableHeader', 'b')], type: 'tableRow' }],
+ type: 'table',
+ };
+
+ expect(afterEdit({ content: [table, paragraph('| 1 | 2 | 3 |', { gap: 0 })], type: 'doc' }, () => {})).toBe(
+ '| a | b |\n|---|---|\n| 1 | 2 | 3 |',
+ );
+ expect(afterEdit({ content: [table, paragraph('| 1 | 2 |', { gap: 0 })], type: 'doc' }, () => {})).toBe(
+ '| a | b |\n|---|---|\n\n| 1 | 2 |',
+ );
+ });
+
+ it('puts a blank line between two paragraphs of an item, where a tag line does not end the text above it', () => {
+ const paragraph = (text: string, attrs?: Record<string, unknown>) => ({
+ attrs,
+ content: [{ text, type: 'text' }],
+ type: 'paragraph',
+ });
+ const editor = load({
+ content: [
+ {
+ content: [{ content: [paragraph('a'), paragraph('<details>', { gap: 0 })], type: 'listItem' }],
+ type: 'bulletList',
+ },
+ ],
+ type: 'doc',
+ });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe('- a\n\n <details>');
+ });
+
+ it('numbers a new item after the one above it', () => {
+ const saved = afterEdit('22. ssh\n80. http\n443. https', (editor) =>
+ editor.chain().setTextSelection(14).splitListItem('listItem').insertContent('alt').run(),
+ );
+
+ expect(saved).toBe('22. ssh\n80. http\n81. alt\n443. https');
+ });
+
+ it('leaves a number behind when its item is in a list that had none', () => {
+ const item = (text: string, attrs?: Record<string, unknown>) => ({
+ attrs,
+ content: [{ content: [{ text, type: 'text' }], type: 'paragraph' }],
+ type: 'listItem',
+ });
+ const editor = load({
+ content: [{ content: [item('a'), item('https', { number: 443 }), item('b')], type: 'orderedList' }],
+ type: 'doc',
+ });
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(saved).toBe('1. a\n2. https\n3. b');
+ });
+
+ it('writes a plain delimiter row over one that is not a row of the table', () => {
+ const cell = (type: string, text: string) => ({
+ content: [{ content: [{ text, type: 'text' }], type: 'paragraph' }],
+ type,
+ });
+ const editor = load({
+ content: [
+ {
+ attrs: { delimiterRow: '---' },
+ content: [
+ { content: [cell('tableHeader', 'a')], type: 'tableRow' },
+ { content: [cell('tableCell', '1')], type: 'tableRow' },
+ ],
+ type: 'table',
+ },
+ ],
+ type: 'doc',
+ });
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(saved).toBe('| a |\n| --- |\n| 1 |');
+ });
+
+ it('writes a plain delimiter row once the table has another column', () => {
+ const saved = afterEdit('| a | b |\n|:--|-----|\n| 1 | 2 |', (editor) =>
+ editor.chain().setTextSelection(4).addColumnAfter().run(),
+ );
+
+ expect(saved).toBe('| a | | b |\n| :--- | --- | --- |\n| 1 | | 2 |');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('closes a fence that nothing closed once a block follows it', () => {
+ const saved = afterEdit('```sh\nid\n', (editor) =>
+ editor.commands.insertContentAt(editor.state.doc.content.size, {
+ content: [{ text: 'after', type: 'text' }],
+ type: 'paragraph',
+ }),
+ );
+
+ expect(saved).toBe('```sh\nid\n```\n\nafter\n');
+ });
+
+ it.each([
+ ['a typed document', 'hello', 'hello'],
+ ['a document that ends with a line break', 'hello\n', 'hello\n'],
+ ])('ends %s the way it ended once an empty heading is its last block', (_name, md, saved) => {
+ expect(
+ afterEdit(md, (editor) => editor.chain().setTextSelection(6).splitBlock().setHeading({ level: 1 }).run()),
+ ).toBe(saved);
+ });
+
+ it.each<[string, JSONContent | string]>([
+ ['nothing but spaces', ' '],
+ ['nothing but a line break', { type: 'hardBreak' }],
+ ])('ends a document the way it ended once a paragraph of %s is its last block', (_name, content) => {
+ expect(
+ afterEdit('hello\n', (editor) =>
+ editor.chain().setTextSelection(6).splitBlock().insertContent(content).run(),
+ ),
+ ).toBe('hello\n');
+ });
+
+ it('ends a document the way it ended once a heading of one ideographic space is its last block', () => {
+ expect(
+ afterEdit('hello\n', (editor) =>
+ editor.chain().setTextSelection(6).splitBlock().setHeading({ level: 2 }).insertContent('\u3000').run(),
+ ),
+ ).toBe('hello\n');
+ });
+
+ it('drops the empty paragraph TrailingNode adds under a last block', () => {
+ expect(afterEdit('- a\n- b', (editor) => editor.chain().setTextSelection(4).insertContent('!').run())).toBe(
+ '- a!\n- b',
+ );
+ });
+});
+
+describe('code written by indentation stays that way while it reads as that code', () => {
+ const code = (text: string, attrs: Record<string, unknown> = { written: ` ${text}` }): JSONContent => ({
+ attrs,
+ content: text ? [{ text, type: 'text' }] : [],
+ type: 'codeBlock',
+ });
+ const paragraph = (text: string): JSONContent => ({ content: [{ text, type: 'text' }], type: 'paragraph' });
+ const saved = (...content: JSONContent[]) => afterEdit({ content, type: 'doc' }, () => {});
+
+ it('writes every line four spaces in once the code is edited', () => {
+ const edited = afterEdit('Run:\n\n\tnmap -sV host\n \n\tcurl host\n\nDone.', (editor) =>
+ editor.chain().setTextSelection(7).insertContent('sudo ').run(),
+ );
+
+ expect(edited).toBe('Run:\n\n sudo nmap -sV host\n\n curl host\n\nDone.');
+ expect(roundTrip(edited)).toBe(edited);
+ });
+
+ it.each<[string, JSONContent[], string]>([
+ ['a language', [code('id', { language: 'sh', written: ' id' })], '```sh\nid\n```'],
+ ['a blank first line', [code('\nid')], '```\n\nid\n```'],
+ ['a line break at its end', [code('id\n')], '```\nid\n\n```'],
+ ['no code', [paragraph('a'), code('')], 'a\n\n```\n\n```'],
+ ['a list above it', [bullets('a'), code('id')], '- a\n\n```\nid\n```'],
+ [
+ 'a list above it and an empty paragraph between',
+ [bullets('a'), { type: 'paragraph' }, code('id')],
+ '- a\n\n\n\n```\nid\n```',
+ ],
+ ['an indented block above it', [code('a'), code('b')], ' a\n\n```\nb\n```'],
+ ])('is fenced with %s', (_name, blocks, written) => {
+ expect(saved(...blocks)).toBe(written);
+ expect(roundTrip(written)).toBe(written);
+ });
+
+ it("is fenced in a list item, where indentation is the item's own", () => {
+ const item = { content: [paragraph('a'), code('id')], type: 'listItem' };
+
+ expect(saved({ content: [item], type: 'bulletList' })).toBe('- a\n ```\n id\n ```');
+ });
+
+ it('stands a blank line under text, which a line right under it would continue', () => {
+ const written = saved(paragraph('a'), code('id', { gap: 0, written: ' id' }));
+
+ expect(written).toBe('a\n\n id');
+ expect(roundTrip(written)).toBe(written);
+ });
+
+ it('stands right under a block that ends by itself', () => {
+ const heading = { attrs: { level: 2 }, content: [{ text: 'Run', type: 'text' }], type: 'heading' };
+
+ expect(saved(heading, code('id', { gap: 0, written: ' id' }))).toBe('## Run\n id');
+ });
+
+ it.each([
+ ['other code', ' whoami', ' id'],
+ ['a heading', '# Title', '```\nid\n```'],
+ ['other code between fences', '~~~\nwhoami\n~~~', '```\nid\n```'],
+ ['a fence that nothing closes', '```\nid', '```\nid\n```'],
+ ['a language the block does not have', '```sh\nid\n```', '```\nid\n```'],
+ ['text under the code', '```\nid\n```\ntext', '```\nid\n```'],
+ ])('does not write lines that hold %s', (_name, written, expected) => {
+ expect(saved(code('id', { written }))).toBe(expected);
+ });
+
+ it.each<[string, JSONContent[], string]>([
+ [
+ 'at the margin under a list',
+ [bullets('a'), code('id', { written: '````\nid\n````' })],
+ '- a\n\n````\nid\n````',
+ ],
+ [
+ "anew under a list when its fence stood past the margin, where it would be the item's",
+ [bullets('a'), code('id', { written: ' ```\n id\n ```' })],
+ '- a\n\n```\nid\n```',
+ ],
+ ])('writes a fenced block %s', (_name, blocks, expected) => {
+ expect(saved(...blocks)).toBe(expected);
+ expect(roundTrip(expected)).toBe(expected);
+ });
+
+ it.each([
+ ['with the fence it had', 'Run:\n\n ````sh\n id\n ```` ', 'Run:\n\n````sh\nid!\n````'],
+ ['between fences though it has no language', 'Run:\n\n ```\n id\n ```', 'Run:\n\n```\nid!\n```'],
+ ])('writes a fenced block anew once its code is edited, %s', (_name, md, saved) => {
+ expect(afterEdit(md, (editor) => editor.chain().setTextSelection(9).insertContent('!').run())).toBe(saved);
+ });
+});
+
+describe('a row of a table is saved as the line it was written as while it holds the same cells', () => {
+ const TABLE = '| Name | Value |\n|------|:-----:|\n| a | 1 |\n| longer name | 2 |\n|c|3|';
+
+ const positionOf = (editor: Editor, text: string): number => {
+ let at = 0;
+
+ editor.state.doc.descendants((node, position) => {
+ at = node.text === text ? position : at;
+ });
+
+ return at;
+ };
+
+ it('writes only the row that was edited anew', () => {
+ const saved = afterEdit(TABLE, (editor) =>
+ editor.chain().setTextSelection(positionOf(editor, 'longer name')).insertContent('a ').run(),
+ );
+
+ expect(saved).toBe('| Name | Value |\n|------|:-----:|\n| a | 1 |\n| a longer name | 2 |\n|c|3|');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('keeps the lines of the other rows when a row is removed, and when one is added', () => {
+ const removed = afterEdit(TABLE, (editor) =>
+ editor.chain().setTextSelection(positionOf(editor, 'a')).deleteRow().run(),
+ );
+ const added = afterEdit(TABLE, (editor) =>
+ editor.chain().setTextSelection(positionOf(editor, 'a')).addRowAfter().run(),
+ );
+
+ expect(removed).toBe('| Name | Value |\n|------|:-----:|\n| longer name | 2 |\n|c|3|');
+ expect(added).toBe(
+ '| Name | Value |\n|------|:-----:|\n| a | 1 |\n| | |\n| longer name | 2 |\n|c|3|',
+ );
+ expect(roundTrip(added)).toBe(added);
+ });
+
+ it('writes every row anew once the table has another number of columns', () => {
+ const saved = afterEdit(TABLE, (editor) =>
+ editor.chain().setTextSelection(positionOf(editor, 'a')).addColumnAfter().run(),
+ );
+
+ expect(saved).toBe(
+ [
+ '| Name | | Value |',
+ '| ----------- | --- | :-----: |',
+ '| a | | 1 |',
+ '| longer name | | 2 |',
+ '| c | | 3 |',
+ ].join('\n'),
+ );
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('writes the delimiter row anew once a column is aligned another way', () => {
+ const saved = afterEdit(TABLE, (editor) =>
+ editor
+ .chain()
+ .setTextSelection(positionOf(editor, 'Value'))
+ .updateAttributes('tableHeader', { align: 'right' })
+ .run(),
+ );
+
+ expect(saved.split('\n')[1]).toBe('| ----------- | -----: |');
+ });
+
+ it.each<[string, unknown, string]>([
+ ['is not a list', 'x', '| a |\n| --- |\n| 1 |'],
+ ['holds a line break in a row', [['| a |'], ['|---|'], ['| 1 |\n| 2 |', '1']], '| a |\n|---|\n| 1 |'],
+ ['holds an empty line for a row', [['| a |'], ['|---|'], ['', '1']], '| a |\n|---|\n| 1 |'],
+ ['holds other cells for a row', [['| a |'], ['|---|'], ['| 2 |', '2']], '| a |\n|---|\n| 1 |'],
+ ['holds other cells for the header', [['| b |', 'b'], ['|---|'], ['|1|', '1']], '| a |\n|---|\n|1|'],
+ ['holds two lines for the header', [['|a|\n|z|', 'a'], ['|---|'], ['|1|', '1']], '| a |\n|---|\n|1|'],
+ ['holds a delimiter row of two columns', [['|a|', 'a'], ['|---|---|'], ['|1|', '1']], '|a|\n| --- |\n|1|'],
+ ['holds a delimiter row with a line break', [['|a|', 'a'], ['|---|\n'], ['|1|', '1']], '|a|\n| --- |\n|1|'],
+ ['holds a row that is not a line', [[7, 'a'], ['|---|'], [null, '1']], '| a |\n|---|\n| 1 |'],
+ ])('writes a line of the table from its cells where what it was written as %s', (_name, written, saved) => {
+ const cell = (type: string, text: string) => ({
+ content: [{ content: [{ text, type: 'text' }], type: 'paragraph' }],
+ type,
+ });
+ const table = {
+ attrs: { delimiterRow: '| --- |', written },
+ content: [
+ { content: [cell('tableHeader', 'a')], type: 'tableRow' },
+ { content: [cell('tableCell', '1')], type: 'tableRow' },
+ ],
+ type: 'table',
+ };
+
+ expect(afterEdit({ content: [table], type: 'doc' }, () => {})).toBe(saved);
+ });
+
+ it('keeps the line of a row whose cell gained only a space at its end', () => {
+ const saved = afterEdit(TABLE, (editor) =>
+ editor
+ .chain()
+ .setTextSelection(positionOf(editor, 'longer name') + 'longer name'.length)
+ .insertContent(' ')
+ .run(),
+ );
+
+ expect(saved).toBe(TABLE);
+ });
+});
+
+describe('a stored line is not written where it would be read as something else', () => {
+ const shapeOf = (markdown: string): unknown => {
+ const editor = load(markdown);
+ const strip = (node: JSONContent): unknown => ({
+ ...(node.content ? { content: node.content.map(strip) } : {}),
+ ...(node.text ? { text: node.text } : {}),
+ type: node.type,
+ });
+ const shape = strip(editor.getJSON());
+
+ editor.destroy();
+
+ return shape;
+ };
+
+ const tasks = (text: string): JSONContent => ({
+ content: [
+ {
+ attrs: { checked: false },
+ content: [{ content: [{ text, type: 'text' }], type: 'paragraph' }],
+ type: 'taskItem',
+ },
+ ],
+ type: 'taskList',
+ });
+
+ it.each([
+ [
+ 'a fence line that is a row only as the last line of its quote',
+ '> | a | b |\n> |---|---|\n> | 1 | 2 |\n> ```\nlazy text\n',
+ '> | a | b |\n> |---|---|\n> | 1 | 2 |\n> | ``` | |\n>\n> lazy text\n',
+ ],
+ [
+ 'a row of nothing but a tab in a quote',
+ '> | a | b |\n> |---|---|\n> | 1 | 2 |\n> \t\n> | 3 | 4 |\n',
+ '> | a | b |\n> |---|---|\n> | 1 | 2 |\n> | | |\n> | 3 | 4 |\n',
+ ],
+ [
+ 'a table two spaces in under a list that is written at the margin',
+ '-\titem\n\n | a | b |\n |---|---|\n | 1 | 2 |\n',
+ '- item\n\n| a | b |\n|---|---|\n| 1 | 2 |\n',
+ ],
+ [
+ 'a table two spaces in under a task list',
+ 'Intro\n\n - [ ] check ports\n\n | port | state |\n |---|---|\n | 22 | open |\n\nEnd\n',
+ 'Intro\n\n- [ ] check ports\n\n| port | state |\n|---|---|\n| 22 | open |\n\nEnd\n',
+ ],
+ [
+ 'a table whose header alone is indented, under a list',
+ ' - a\n - b\n\n | a | b |\n|---|---|\n| `x|y` | 2 |\n',
+ '- a\n- b\n\n| a | b |\n|---|---|\n| `x|y` | 2 |\n',
+ ],
+ [
+ 'a table in a quote written with a tab after its marker',
+ '>\t| a | b |\n>\t|---|---|\n>\t| `x|y` | 2 |\n',
+ '> | a | b |\n> |---|---|\n> | `x|y` | 2 |\n',
+ ],
+ [
+ 'an indented block that holds a fence line, above a list with a tab in its code',
+ 'Open the block with:\n\n ```\n\n- then a recipe:\n\n all:\tdeps\n\n```\nend\n```\n',
+ 'Open the block with:\n\n````\n```\n````\n\n- then a recipe:\n\n ```\n all:\tdeps\n ```\n\n```\nend\n```\n',
+ ],
+ [
+ 'two blank lines above a list of plain and task items',
+ 'Intro\n\n\n- note\n- [ ] todo\n\nEnd\n',
+ 'Intro\n\n- note\n\n- [ ] todo\n\nEnd\n',
+ ],
+ [
+ 'a blank line with spaces above a list of plain and task items',
+ 'Intro\n \n- note\n- [ ] todo\n',
+ 'Intro\n\n- note\n\n- [ ] todo\n',
+ ],
+ [
+ 'a table in a list item whose row holds a pipeline',
+ '- item\n\n |a|b|\n |-|-|\n |{{ .X | upper }}|2|',
+ '- item\n\n |a|b|\n |-|-|\n | {{ .X | upper }} | 2 |',
+ ],
+ [
+ 'a list one space in under a task list',
+ 'Intro\n\n - [ ] todo\n - note\n\nEnd\n',
+ 'Intro\n\n- [ ] todo\n\n- note\n\nEnd\n',
+ ],
+ [
+ 'spaces after a list of plain and task items',
+ 'Intro\n\n- a\n- [ ] t\n- b \n\nEnd\n',
+ 'Intro\n\n- a\n\n- [ ] t\n\n- b\n\nEnd\n',
+ ],
+ [
+ 'a line of white space under a quoted table in a list item',
+ '- item\n > | {{ .A | f }} | c |\n > |---|---|\n > \t\n',
+ '- item\n > | {{ .A | f }} | c |\n > |---|---|\n',
+ ],
+ [
+ 'a fence closed by a run of two kinds of character, above a list with a tab in its code',
+ '```\ncode\n```~~\n\n- item\n\n a\tb\n\n```\nx\n```\n',
+ '```\ncode\n```\n\n- item\n\n ```\n a\tb\n ```\n\n```\nx\n```\n',
+ ],
+ ])('%s', (_name, md, saved) => {
+ expect(roundTrip(md)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ expect(shapeOf(saved)).toEqual(shapeOf(md));
+ });
+
+ it.each([
+ [
+ 'text two spaces in and a fence under it',
+ ' - [ ] build\n - [x] test\n\n Then install:\n\n ```sh\n sudo make install\n ```\n',
+ '- [ ] build\n- [x] test\n\nThen install:\n\n ```sh\n sudo make install\n ```\n',
+ ],
+ [
+ 'text two spaces in and indented code under it',
+ 'Steps:\n\n - [ ] build\n - [x] test\n\n Note: run as root.\n\n sudo make install\n',
+ 'Steps:\n\n- [ ] build\n- [x] test\n\nNote: run as root.\n\n sudo make install\n',
+ ],
+ ])('keeps what stands under a task list out of its last item: %s', (_name, md, saved) => {
+ expect(roundTrip(md)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ expect(kindsOf(saved)).toEqual(kindsOf(md));
+ });
+
+ it.each<[string, JSONContent[], string]>([
+ [
+ 'indented code under a list and a paragraph of one space',
+ [
+ bullets('item'),
+ { content: [{ text: ' ', type: 'text' }], type: 'paragraph' },
+ { attrs: { written: ' code' }, content: [{ text: 'code', type: 'text' }], type: 'codeBlock' },
+ ],
+ '- item\n\n \n\n```\ncode\n```',
+ ],
+ [
+ 'indented code under indented code and a paragraph of one space',
+ [
+ { attrs: { written: ' one' }, content: [{ text: 'one', type: 'text' }], type: 'codeBlock' },
+ { content: [{ text: ' ', type: 'text' }], type: 'paragraph' },
+ { attrs: { written: ' two' }, content: [{ text: 'two', type: 'text' }], type: 'codeBlock' },
+ ],
+ ' one\n\n \n\n```\ntwo\n```',
+ ],
+ [
+ 'a task list with two blank lines above it, under a bullet list',
+ [
+ bullets('item'),
+ {
+ attrs: { gap: 2 },
+ content: [
+ {
+ attrs: { checked: false },
+ content: [{ content: [{ text: 'task', type: 'text' }], type: 'paragraph' }],
+ type: 'taskItem',
+ },
+ ],
+ type: 'taskList',
+ },
+ ],
+ '- item\n\n- [ ] task',
+ ],
+ [
+ 'an empty paragraph between a task list and text',
+ [tasks('t'), { type: 'paragraph' }, { content: [{ text: 'x', type: 'text' }], type: 'paragraph' }],
+ '- [ ] t\n\n\n\n\nx',
+ ],
+ [
+ 'text that stood five blank lines under a task list, without the empty paragraphs it was read with',
+ [tasks('t'), { attrs: { gap: 5 }, content: [{ text: 'x', type: 'text' }], type: 'paragraph' }],
+ '- [ ] t\n\nx',
+ ],
+ [
+ 'a list of `-` bullets that stood three blank lines under a task list under such a list',
+ [bullets('a'), tasks('b'), { ...bullets('c'), attrs: { gap: 3 } }],
+ '- a\n\n- [ ] b\n\n- c',
+ ],
+ [
+ 'text one space in under a task list',
+ [
+ {
+ content: [
+ {
+ attrs: { checked: false },
+ content: [{ content: [{ text: 'task', type: 'text' }], type: 'paragraph' }],
+ type: 'taskItem',
+ },
+ ],
+ type: 'taskList',
+ },
+ { content: [{ text: ' note', type: 'text' }], type: 'paragraph' },
+ ],
+ '- [ ] task\n\nnote',
+ ],
+ [
+ 'a table written two spaces in as the first block of a list item',
+ [
+ {
+ content: [
+ {
+ content: [
+ {
+ attrs: {
+ delimiterRow: '|---|',
+ written: [[' | a |', 'a'], [' |---|'], [' | 1 |', '1']],
+ },
+ content: [
+ {
+ content: [
+ {
+ content: [
+ { content: [{ text: 'a', type: 'text' }], type: 'paragraph' },
+ ],
+ type: 'tableHeader',
+ },
+ ],
+ type: 'tableRow',
+ },
+ {
+ content: [
+ {
+ content: [
+ { content: [{ text: '1', type: 'text' }], type: 'paragraph' },
+ ],
+ type: 'tableCell',
+ },
+ ],
+ type: 'tableRow',
+ },
+ ],
+ type: 'table',
+ },
+ ],
+ type: 'listItem',
+ },
+ ],
+ type: 'bulletList',
+ },
+ ],
+ '- | a |\n |---|\n | 1 |',
+ ],
+ [
+ 'code with a tab in a quote in a list item',
+ [
+ {
+ content: [
+ {
+ content: [
+ { content: [{ text: 'a', type: 'text' }], type: 'paragraph' },
+ {
+ content: [
+ {
+ attrs: { written: '\tif x:\n\t\treturn 1' },
+ content: [{ text: 'if x:\n\treturn 1', type: 'text' }],
+ type: 'codeBlock',
+ },
+ ],
+ type: 'blockquote',
+ },
+ ],
+ type: 'listItem',
+ },
+ ],
+ type: 'bulletList',
+ },
+ ],
+ '- a\n > ```\n > if x:\n > \treturn 1\n > ```',
+ ],
+ ])('after an edit: %s', (_name, blocks, saved) => {
+ const written = afterEdit({ content: blocks, type: 'doc' }, () => {});
+
+ expect(written).toBe(saved);
+ expect(shapeOf(roundTrip(written))).toEqual(shapeOf(written));
+ });
+
+ it('writes no blank line of its own between a bullet list and a task list once the block between them is gone', () => {
+ const written = afterEdit('- item\n\nx\n\n\n\n\n- [ ] task\n', (editor) => {
+ let from = 0;
+ let to = 0;
+
+ editor.state.doc.forEach((node, at) => {
+ [from, to] = node.textContent === 'x' ? [at, at + node.nodeSize] : [from, to];
+ });
+ editor.view.dispatch(editor.state.tr.delete(from, to));
+ });
+
+ expect(roundTrip(written)).toBe(written);
+ expect(shapeOf(roundTrip(written))).toEqual(shapeOf(written));
+ });
+
+ it('writes no blank line of its own above a list of `-` bullets that stands under a task list under such a list', () => {
+ const written = afterEdit('- a\n\n- [x] b\n\ntext\n\n\n- c\n', (editor) => {
+ let from = 0;
+ let to = 0;
+
+ editor.state.doc.forEach((node, at) => {
+ [from, to] = node.textContent === 'text' ? [at, at + node.nodeSize] : [from, to];
+ });
+ editor.view.dispatch(editor.state.tr.delete(from, to));
+ });
+
+ expect(written).toBe('- a\n\n- [x] b\n\n- c\n');
+ expect(roundTrip(written)).toBe(written);
+ });
+
+ it('writes a row with a fence in its first cell once a block stands under the table', () => {
+ const written = afterEdit('| a | b |\n|---|---|\n| 1 | 2 |\n```', (editor) =>
+ editor.commands.insertContentAt(editor.state.doc.content.size, {
+ content: [{ text: 'typed', type: 'text' }],
+ type: 'paragraph',
+ }),
+ );
+
+ expect(written).toBe('| a | b |\n|---|---|\n| 1 | 2 |\n| ``` | |\n\ntyped');
+ expect(kindsOf(written)).toEqual(['table', 'paragraph']);
+ });
+
+ it('keeps an action typed into a row under a header that was written without a pipe', () => {
+ const written = afterEdit('Name\n|---|\nvalue\n', (editor) => {
+ let at = 0;
+
+ editor.state.doc.descendants((node, position) => {
+ at = node.text === 'value' ? position : at;
+ });
+ editor.chain().setTextSelection(at).insertContent('{{ .Z | upper }} ').run();
+ });
+
+ expect(written).toBe('Name\n|---|\n| {{ .Z | upper }} value |\n');
+ expect(JSON.stringify(shapeOf(written))).toContain('{{ .Z | upper }} value');
+ });
+
+ it('keeps the empty last line of the code under a fence nothing closes, in a document that ends with no line break', () => {
+ const codeOf = (markdown: string) => {
+ const editor = load(markdown);
+ const code = editor.state.doc.firstChild?.textContent;
+
+ editor.destroy();
+
+ return code;
+ };
+
+ const saved = roundTrip(' ```\ncode\n ');
+
+ expect(saved).toBe('```\ncode\n\n');
+ expect(roundTrip(saved)).toBe(saved);
+ expect([codeOf(' ```\ncode\n '), codeOf(saved)]).toEqual(['code\n', 'code\n']);
+ });
+
+ it('writes one more line break under a fence nothing closes once its code ends with an empty line', () => {
+ const written = afterEdit('intro\n\n```\nfirst\nlast', (editor) =>
+ editor
+ .chain()
+ .setTextSelection(editor.state.doc.content.size - 1)
+ .insertContent('\n')
+ .run(),
+ );
+
+ expect(written).toBe('intro\n\n```\nfirst\nlast\n\n');
+ expect(roundTrip(written)).toBe(written);
+ });
+});
+
+describe('the spaces an empty line of code was written with in a list item', () => {
+ const list = (type: string, blankLine: unknown): JSONContent => ({
+ content: [
+ {
+ content: [
+ { content: [{ text: 'step', type: 'text' }], type: 'paragraph' },
+ { attrs: { blankLine }, content: [{ text: 'a\n\nb', type: 'text' }], type: 'codeBlock' },
+ ],
+ type: 'listItem',
+ },
+ ],
+ type,
+ });
+
+ it.each([
+ [
+ 'are written while they fit under the marker',
+ 'orderedList',
+ ' ',
+ '1. step\n ```\n a\n \n b\n ```',
+ ],
+ ['are dropped under a marker they reach past', 'bulletList', ' ', '- step\n ```\n a\n\n b\n ```'],
+ ['are not written when they are not spaces', 'orderedList', '\t', '1. step\n ```\n a\n\n b\n ```'],
+ ['are not written when they are text', 'orderedList', 'x', '1. step\n ```\n a\n\n b\n ```'],
+ ])('%s', (_name, type, blankLine, written) => {
+ expect(afterEdit({ content: [list(type, blankLine)], type: 'doc' }, () => {})).toBe(written);
+ expect(roundTrip(written)).toBe(written);
+ });
+
+ it('are written on a line added to the code', () => {
+ const written = afterEdit('1. step\n ```\n a\n \n b\n ```', (editor) =>
+ editor.chain().setTextSelection(13).insertContent('\n\nc').run(),
+ );
+
+ expect(written).toBe('1. step\n ```\n a\n \n b\n \n c\n ```');
+ expect(roundTrip(written)).toBe(written);
+ });
+
+ it('are not written once the code stands outside a list', () => {
+ const [item] = list('orderedList', ' ').content ?? [];
+
+ expect(afterEdit({ content: [item!.content![1]!], type: 'doc' }, () => {})).toBe('```\na\n\nb\n```');
+ });
+});
+
+describe('a line left of its item goes back under it when marked would not read it there', () => {
+ const lazyItem = (lines: string[], attrs: Record<string, unknown> = { lazyFrom: 1 }): JSONContent => ({
+ content: [
+ {
+ content: [
+ {
+ attrs,
+ content: [
+ {
+ content: lines.flatMap((text, at) => [
+ ...(at ? [{ attrs: { marker: '' }, type: 'hardBreak' }] : []),
+ ...(text ? [{ text, type: 'text' }] : []),
+ ]),
+ type: 'paragraph',
+ },
+ ],
+ type: 'listItem',
+ },
+ ],
+ type: 'bulletList',
+ },
+ ],
+ type: 'doc',
+ });
+
+ it.each([
+ ['an opening tag', ['item', '<host> is up', 'last'], '- item\n <host> is up\n last'],
+ ['a hash', ['item', '#hashtag line', 'last'], '- item\n #hashtag line\n last'],
+ ['a bullet', ['item', '- not an item'], '- item\n - not an item'],
+ ['a fence', ['item', '```sh'], '- item\n ```sh'],
+ [
+ 'leading spaces, and the line under it',
+ ['item', ' two spaces in', 'last'],
+ '- item\n two spaces in\n last',
+ ],
+ ['a line whose rest opens a heading', ['item', 'x # comment', 'last'], '- item\nx # comment\n last'],
+ ['a closing tag', ['item', '</steps>'], '- item\n</steps>'],
+ ])('%s', (_name, lines, saved) => {
+ const editor = load(lazyItem(lines));
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe(saved);
+ });
+
+ it('stays at the margin whatever depth it claims', () => {
+ const editor = load(lazyItem(['item', 'lazy'], { lazyDepth: -1, lazyFrom: 1 }));
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe('- item\nlazy');
+ });
+
+ it('does not follow a line indented like code', () => {
+ const editor = load(lazyItem(['item', ' deep', 'last'], { lazyFrom: 2 }));
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe('- item\n deep\n last');
+ });
+
+ it('stays at the margin under a line that is only partly indented', () => {
+ const saved = roundTrip('- item\n partially\nflush');
+
+ expect(saved).toBe('- item\n partially\nflush');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('does not follow an empty line', () => {
+ const editor = load(lazyItem(['item\n\nlazy']));
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(md).toBe('- item\n\n lazy');
+ });
+
+ it('is not copied to the item Enter makes', () => {
+ const saved = afterEdit('- first\n- item\nlazy one\nlazy two\n- third', (editor) =>
+ editor.chain().setTextSelection(16).splitListItem('listItem').run(),
+ );
+
+ expect(saved).toBe('- first\n- item\n- lazy one\n lazy two\n- third');
+ });
+
+ it('reads back the marker it escapes on an indented line', () => {
+ const editor = load({
+ content: [
+ {
+ content: [
+ { text: 'a', type: 'text' },
+ { type: 'hardBreak' },
+ { text: ' # not a heading', type: 'text' },
+ ],
+ type: 'paragraph',
+ },
+ ],
+ type: 'doc',
+ });
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(saved).toBe('a \n \\# not a heading');
+ const reread = load(saved);
+ const texts = JSON.stringify(reread.getJSON());
+
+ reread.destroy();
+
+ expect(texts).toContain('"text":" # not a heading"');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('leaves the other private-use character, the one that shields tabs in fences, alone', () => {
+ expect(roundTrip('glyph \uE000 here')).toBe('glyph \uE000 here');
+ });
+
+ it.each([
+ [
+ 'in the text of a line',
+ '- item\ntail \uE001 here\n\n\uE001 opens a paragraph',
+ '- item\n tail \uE001 here\n\n\uE001 opens a paragraph',
+ ],
+ ['first on a line of an item', '- item\n\uE001 second', '- item\n \uE001 second'],
+ ['first in a paragraph of an item', '- item\n\n \uE001 para', '- item\n\n \uE001 para'],
+ ['in the text of an image', '![a\uE001b](http://x.test/i.png)', '![a\uE001b](http://x.test/i.png)'],
+ [
+ 'in a link target under a list',
+ '- item\nlazy [x](http://x.test/\uE001b)',
+ '- item\n lazy [x](http://x.test/\uE001b)',
+ ],
+ ])('keeps a private-use character the author wrote %s, and indents every line', (_name, md, saved) => {
+ expect(roundTrip(md)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+});
+
+describe('the line break a document ends with outlives the block that carried it', () => {
+ it.each([
+ [
+ 'joined into the block above',
+ 'A\n\nB\n',
+ (editor: Editor) => {
+ editor.commands.setTextSelection(4);
+ editor.commands.keyboardShortcut('Backspace');
+ },
+ 'AB\n',
+ ],
+ ['deleted', 'A\n\nB\n', (editor: Editor) => editor.commands.deleteRange({ from: 3, to: 6 }), 'A\n'],
+ ['emptied', 'A\n\nB\n', (editor: Editor) => editor.view.dispatch(editor.state.tr.delete(4, 5)), 'A\n'],
+ [
+ 'wrapped in a list',
+ 'A\n\nB\n',
+ (editor: Editor) => editor.chain().setTextSelection(4).toggleBulletList().run(),
+ 'A\n\n- B\n',
+ ],
+ [
+ 'made a list of another kind',
+ 'x\n\n* a\n* b\n\n',
+ (editor: Editor) => editor.chain().setTextSelection(6).toggleOrderedList().run(),
+ 'x\n\n1. a\n2. b\n\n',
+ ],
+ [
+ 'followed by a new block',
+ 'A\n',
+ (editor: Editor) => editor.chain().setTextSelection(2).splitBlock().insertContent('B').run(),
+ 'A\n\nB\n',
+ ],
+ ])('%s', (_name, md, edit, saved) => {
+ expect(afterEdit(md, edit)).toBe(saved);
+ });
+
+ it.each([
+ ['replaced', (editor: Editor) => editor.commands.setContent('new', { contentType: 'markdown' }), 'new'],
+ ['left with no text', (editor: Editor) => editor.view.dispatch(editor.state.tr.delete(1, 2)), ''],
+ ])('but not a document that is %s', (_name, edit, saved) => {
+ expect(afterEdit('A\n', edit)).toBe(saved);
+ });
+});
+
+describe('a fact read off pasted HTML is not one', () => {
+ it('ignores layout attributes on the elements', () => {
+ const editor = new Editor({
+ content:
+ '<ul marker="*" gap="0"><li number="7" lazyfrom="1"><p>a</p></li></ul><ol delimiter=")"><li><p>b</p></li></ol>',
+ extensions: createMarkdownExtensions(),
+ });
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(saved).toBe('- a\n\n1. b');
+ });
+});
+
+describe('the cost of reading the layout grows with the document, not with the square of it', () => {
+ it.each<[string, 'read' | 'saved', (size: number) => string, number]>([
+ ['blank lines between two paragraphs', 'read', (size) => `a\n${'\n'.repeat(size)}b\n`, 16_000],
+ [
+ 'a line of backticks in a fence nothing closes',
+ 'read',
+ (size) => `\`\`\`\nstart\n${'`'.repeat(size)}x`,
+ 16_000,
+ ],
+ ])('%s, %s', (_name, side, build, size) => {
+ const { destroy, parse, serialize } = markdownCodec();
+ const growth =
+ side === 'read'
+ ? slowdownWhenInputQuadruples(build, parse, size)
+ : slowdownWhenInputQuadruples((length) => parse(build(length)), serialize, size);
+
+ destroy();
+
+ expect(growth).toBeLessThan(GROWTH_IF_QUADRATIC / 2);
+ });
+});
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-layout.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-layout.ts
new file mode 100644
index 00000000..86271edb
--- /dev/null
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-layout.ts
@@ -0,0 +1,1052 @@
+import type { AnyExtension, JSONContent, MarkdownParseResult } from '@tiptap/core';
+import type { Node as PMNode } from '@tiptap/pm/model';
+
+import { Extension } from '@tiptap/core';
+import { Plugin } from '@tiptap/pm/state';
+import { ReplaceStep } from '@tiptap/pm/transform';
+import { Lexer } from 'marked';
+
+import {
+ holdsProtectedPipe,
+ isNotARow,
+ isTableBodyEnd,
+ TABLE_DELIMITER_LINE,
+ TABLE_PIPE,
+} from './markdown-editor-table-pipes';
+
+// A document's ProseMirror structure says which blocks it has, not how they were written: whether a blank line
+// stood between two of them, which character a list used, how a table was padded. Agents write the document
+// and a person edits a line of it, so the rest is saved the way it was written. The facts are read off the
+// marked tokens here, travel on the nodes as attributes that are never rendered, and every writer falls back to
+// the editor's own layout wherever the stored one would change how the text parses.
+
+type LayoutToken = {
+ header?: LayoutToken[];
+ items?: LayoutToken[];
+ layout?: Record<string, unknown>;
+ ordered?: boolean;
+ raw: string;
+ rows?: LayoutToken[][];
+ task?: boolean;
+ text?: string;
+ tokens?: LayoutToken[];
+ type: string;
+};
+
+const note = (token: LayoutToken, facts: Record<string, unknown>): void => {
+ token.layout = { ...token.layout, ...facts };
+};
+
+const newlines = (text: string): number => text.split('\n').length - 1;
+
+// `/\n+$/` starts over at every line break of a run that is not the last one.
+export const lineBreaksAtEnd = (text: string): string => {
+ let end = text.length;
+
+ while (end > 0 && text[end - 1] === '\n') {
+ end--;
+ }
+
+ return text.slice(end);
+};
+
+// The newline that ends the last line of content is not a blank line; every one after it is.
+const blankLinesAfter = (text: string): number => {
+ let count = 0;
+
+ for (let at = text.length - 1; at >= 0 && ' \t\r\n'.includes(text[at]!); at--) {
+ count += text[at] === '\n' ? 1 : 0;
+ }
+
+ return Math.max(0, count - 1);
+};
+
+const ITEM_MARKER = /^ {0,3}(?:([-*+])|(\d{1,9})([.)]))([ \t]*)(.*)$/;
+
+// A row written `| a | b |`: one space of padding, an empty cell as one space or two. This only picks a style
+// — a wrong pick is still a table — but the row marked hands over holds a stand-in for each pipe of its code
+// spans and Go actions, or `\|` where the author escaped one, and a split that took either for a separator
+// would call every such table padded.
+const COMPACT_CELL = /^(?: {1,2}| \S(?:.*\S)? )$/;
+
+const cellsOf = (row: string): string[] =>
+ row
+ .replace(/\\[\\|]/g, 'xx')
+ .replaceAll(TABLE_PIPE, 'x')
+ .split('|');
+
+const isCompactRow = (row: string): boolean => {
+ const cells = cellsOf(row);
+
+ return (
+ cells.length > 2 && !cells[0] && !cells.at(-1) && cells.slice(1, -1).every((cell) => COMPACT_CELL.test(cell))
+ );
+};
+
+// A line of text right under a table is a row to marked, written without a single pipe. Beside the style of
+// the table, every row is kept as it was written, next to the cells marked read in it: a row that still holds
+// those cells is saved as that line (withWrittenRows).
+const annotateTable = (table: LayoutToken): void => {
+ const lines = table.raw.slice(0, table.raw.length - lineBreaksAtEnd(table.raw).length).split('\n');
+ const [header = '', delimiterRow = '', ...rows] = lines.map((line) => line.trim());
+ const entry = (line = '', cells: LayoutToken[] = []): string[] => [line, ...cells.map((cell) => cell.text ?? '')];
+ // How an empty cell was written, for a row that has to be written anew.
+ const empty = [header, ...rows].flatMap((row) => cellsOf(row).slice(1, -1)).filter((cell) => !cell.trim());
+
+ note(table, {
+ written: [
+ entry(lines[0], table.header),
+ [lines[1] ?? ''],
+ ...(table.rows ?? []).map((row, at) => entry(lines[at + 2], row)),
+ ],
+ ...(isCompactRow(header) && rows.every((row) => !row.includes('|') || isCompactRow(row))
+ ? { delimiterRow, ...(empty.length && empty.every((cell) => cell === ' ') ? { emptyCell: 2 } : {}) }
+ : {}),
+ });
+};
+
+// A fence that nothing closes runs to the end of the document. Closing it adds a line the author did not
+// write, so the document renderer leaves the last block open again. Every block keeps its lines as they stood
+// (`written`), without the line break marked hands over with them when a block follows on the next line.
+const annotateCode = (code: LayoutToken): void => {
+ const lines = code.raw.split('\n');
+
+ while (lines.length && !lines.at(-1)!.trim()) {
+ lines.pop();
+ }
+
+ const fence = /^ {0,3}(`{3,}|~{3,})/.exec(lines[0] ?? '')?.[1];
+ // Each run is of one character: a line of a thousand backticks is matched once, not from every length.
+ const [, ticks, tildes] =
+ (lines.length > 1 ? /^ {0,3}(?:(`{3,})(?:~[~`]*)?|(~{3,})(?:`[~`]*)?) *$/.exec(lines.at(-1)!) : null) ?? [];
+
+ note(code, {
+ ...(fence === undefined || fence === '```' ? {} : { fence }),
+ ...(fence === undefined || (ticks ?? tildes)?.startsWith(fence) ? {} : { unclosed: true }),
+ written: lines.join('\n'),
+ });
+};
+
+// A line of an item's first paragraph written left of the content column — a closing `</tag>` or a sentence
+// straight under the list — belongs to that paragraph as much as an indented one. marked hands such a line
+// over untouched, where it strips the indentation off every other, and that is the test. `outer[at]` counts
+// the enclosing items the line is left of as well: an item's lines are its parent's lines one for one, so the
+// counts add up. A paragraph whose lines marked has merged or split is left alone, its lines no longer being
+// the ones counted here.
+const annotateItem = (item: LayoutToken, outer: number[], base: number, margin: string): void => {
+ const written = item.raw.split('\n');
+ const read = (item.text ?? '').split('\n');
+ // A line of nothing but spaces, as it stood past the margin its list is written at.
+ const blanks = written.map((line) =>
+ /^ +$/.test(line) && line.startsWith(margin) ? line.slice(margin.length) : '',
+ );
+ const depths = read.map((line, at) =>
+ at > 0 && line.trim() && line === written[at] ? 1 + (outer[base + at] ?? 0) : 0,
+ );
+ const tokens = item.tokens ?? [];
+ const paragraph = tokens.find((token) => token.type !== 'checkbox');
+
+ if (paragraph?.type === 'text' || paragraph?.type === 'paragraph') {
+ const lines = newlines(paragraph.raw.trimEnd()) + 1;
+ const from = depths.findIndex((depth, at) => depth > 0 && at < lines && /^\S/.test(read[at]!));
+
+ if (from > 0 && newlines(paragraph.text ?? '') + 1 === lines) {
+ note(item, { lazyDepth: depths[from], lazyFrom: from });
+ }
+ }
+
+ // An empty line of a code block in the item, written with spaces that marked took for the item's
+ // indentation: with the same ones on every such line of the block, they are what it is saved with.
+ let line = 0;
+
+ for (const token of tokens) {
+ if (token.type === 'code') {
+ const empty = blanks
+ .slice(line, line + newlines(token.raw.trimEnd()) + 1)
+ .filter((_, at) => read[line + at] === '');
+
+ if (empty[0] && empty.every((blank) => blank === empty[0])) {
+ note(token, { blankLine: empty[0] });
+ }
+ }
+
+ line += newlines(token.raw);
+ }
+
+ annotateBlocks(tokens, depths, 0, true, blanks);
+};
+
+const annotateList = (list: LayoutToken, outer: number[], base: number): void => {
+ const items = list.items ?? [];
+ const markers = items.map((item) => ITEM_MARKER.exec(item.raw.split('\n', 1)[0]!));
+
+ if (list.ordered) {
+ const numbers = markers.map((marker) => Number(marker?.[2]));
+
+ if (markers[0]?.[3] === ')') {
+ note(list, { delimiter: ')' });
+ }
+
+ // A list numbered the way the editor numbers it stores nothing and renumbers itself when an item is
+ // added or removed. `1. 1. 1.` is a fact about the list. Any other numbering is kept on the items that
+ // break the count — and only a list that was read with such numbers writes them, so an item carried
+ // into another list by a drag, a Tab or a list toggle does not bring its number along.
+ if (numbers.every(Number.isInteger) && numbers.some((number, at) => number !== numbers[0]! + at)) {
+ const isRepeated = numbers.every((number) => number === numbers[0]);
+
+ note(list, { numbering: isRepeated ? 'same' : 'own' });
+ items.forEach((item, at) => {
+ if (!isRepeated && at && numbers[at] !== numbers[at - 1]! + 1) {
+ note(item, { number: numbers[at] });
+ }
+ });
+ }
+ } else if (markers[0]?.[1] && markers[0][1] !== '-') {
+ note(list, { marker: markers[0][1] });
+ }
+
+ // Up to three spaces before every marker, which marked does not count as nesting — unless a line of the
+ // list stands left of them, where it would not stay once the list is written that far in.
+ const indents = items.map((item) => /^ {0,3}/.exec(item.raw)![0].length);
+ const margin = ' '.repeat(indents[0] ?? 0);
+
+ // Nor in a bullet list with a task item: those are rendered upstream, at the margin.
+ const isIndented =
+ indents[0] &&
+ indents.every((indent) => indent === indents[0]) &&
+ (list.ordered || !items.some((item) => item.task)) &&
+ list.raw.split('\n').every((row) => !row.trim() || row.startsWith(margin));
+
+ if (isIndented) {
+ note(list, { indent: indents[0] });
+ }
+
+ let line = 0;
+
+ items.forEach((item, at) => {
+ if (at) {
+ // The one blank line between two items, when it was written with spaces in it.
+ const above = items[at - 1]!.raw;
+ const gap = blankLinesAfter(above);
+ const blank = above.slice(0, -1).split('\n').at(-1) ?? '';
+ const written = isIndented && blank.startsWith(margin) ? blank.slice(margin.length) : blank;
+
+ note(item, { gap, ...(/^ +$/.test(written) ? { blank: written } : {}) });
+ }
+
+ if (markers[at]?.[5] && /^ {2,4}$/.test(markers[at][4]!)) {
+ note(item, { spaces: markers[at][4]!.length });
+ }
+
+ annotateItem(item, outer, base + line, isIndented ? margin : '');
+ line += newlines(item.raw);
+ });
+};
+
+const OWNS_ITS_LAST_LINES = new Set(['paragraph', 'table']);
+
+// @tiptap/markdown makes a list of each kind out of a list that mixes plain and task items, by this very test,
+// and hands every one of them the facts of the whole: what stood above the list is then above each of them.
+const TASK_ITEM = /^(\s*)[-+*]\s+\[([ xX])\]\s+/;
+
+const isSplitByTasks = (list: LayoutToken): boolean => {
+ const items = list.items ?? [];
+ const tasks = items.filter((item) => TASK_ITEM.test(item.raw)).length;
+
+ return tasks > 0 && tasks < items.length;
+};
+
+const annotateBlocks = (
+ tokens: LayoutToken[],
+ outer: number[] = [],
+ base = 0,
+ isInItem = false,
+ blanks: string[] = [],
+): void => {
+ let above: null | string = null;
+ let line = 0;
+
+ for (const [at, token] of tokens.entries()) {
+ if (token.type === 'space') {
+ above = above === null ? null : above + token.raw;
+ } else if (token.type !== 'checkbox') {
+ if (above !== null) {
+ // The one blank line above, when it is written with spaces or tabs in it. In a list item
+ // marked has taken the item's indentation off its lines, and what is left of that line is
+ // not what was written: there it is read off the item's own lines (`blanks`).
+ const gap = blankLinesAfter(above);
+ const written = isInItem ? (blanks[line - 1] ?? '') : (above.slice(0, -1).split('\n').at(-1) ?? '');
+ const blank = gap === 1 ? written : '';
+
+ // Of the facts of a list that is split, only that a blank line stood above it is true of
+ // every part.
+ note(
+ token,
+ token.type === 'list' && isSplitByTasks(token)
+ ? { gap: Math.min(gap, 1) }
+ : { gap, ...(blank ? { blank } : {}) },
+ );
+ }
+
+ if (token.type === 'list') {
+ // marked ends a list at the last character of its text: the spaces its last line ended with
+ // open the space under it.
+ const [trail = ''] = tokens[at + 1]?.type === 'space' ? tokens[at + 1]!.raw.split('\n', 1) : [];
+
+ note(token, trail && !isSplitByTasks(token) ? { trail } : {});
+ annotateList(token, outer, base + line);
+ } else if (token.type === 'blockquote') {
+ annotateBlocks(token.tokens ?? [], [], 0, isInItem);
+ } else if (token.type === 'table') {
+ annotateTable(token);
+ } else if (token.type === 'code') {
+ annotateCode(token);
+ } else if (token.type === 'hr' && token.raw.trim() !== '---') {
+ note(token, { rule: token.raw.trim() });
+ } else if (token.type === 'heading') {
+ // A setext heading: the row of `=` or `-` under its text. Any other is one line, and keeps the
+ // spaces that line ended with.
+ const [, underline] = /\n {0,3}(=+|-+) *$/.exec(token.raw.trimEnd()) ?? [];
+ const [first = ''] = token.raw.split('\n', 1);
+ const trail = first.slice(first.trimEnd().length);
+
+ note(token, underline ? { underline } : trail ? { trail } : {});
+ }
+
+ // A line of nothing but a tab right under text or under a table is a line of the text and a row of
+ // the table, not a blank line under them.
+ above = OWNS_ITS_LAST_LINES.has(token.type) ? `x${lineBreaksAtEnd(token.raw)}` : token.raw;
+ }
+
+ line += newlines(token.raw);
+ }
+};
+
+// The line breaks a document ends with are part of no block, so its last block carries them.
+export const annotateLayout = (tokens: unknown[], source: string): void => {
+ const blocks = (tokens as LayoutToken[]).filter((token) => token.type !== 'space');
+ const eof = lineBreaksAtEnd(source);
+
+ annotateBlocks(tokens as LayoutToken[]);
+
+ if (blocks.length && eof) {
+ note(blocks.at(-1)!, { eof });
+ }
+};
+
+const withLayout = (parsed: MarkdownParseResult, layout?: Record<string, unknown>): MarkdownParseResult => {
+ const [first, ...rest] = Array.isArray(parsed) ? parsed : [parsed];
+
+ if (!layout || !first || 'mark' in first) {
+ return parsed;
+ }
+
+ const noted = { ...first, attrs: { ...first.attrs, ...layout } };
+
+ return Array.isArray(parsed) ? [noted, ...rest] : noted;
+};
+
+const BLOCKS = [
+ 'blockquote',
+ 'bulletList',
+ 'codeBlock',
+ 'heading',
+ 'horizontalRule',
+ 'image',
+ 'listItem',
+ 'orderedList',
+ 'paragraph',
+ 'table',
+ 'taskList',
+];
+
+type ParseMarkdown = (this: unknown, token: LayoutToken, helpers: unknown) => MarkdownParseResult;
+
+// The Markdown extension parses the initial content in the hook that creates its manager, so there is no
+// moment to reach into the manager first: each block's own handler hands the facts over instead.
+export const withLayoutFacts = <T extends AnyExtension>(extension: T): T => {
+ const parse = (extension.config as { parseMarkdown?: ParseMarkdown }).parseMarkdown;
+
+ if (!parse) {
+ return extension;
+ }
+
+ return extension.extend({
+ parseMarkdown(token: LayoutToken, helpers: unknown) {
+ return withLayout(parse.call(this, token, helpers), token.layout);
+ },
+ }) as T;
+};
+
+// keepOnSplit: Enter in a paragraph makes a new block, and what was written about the old one is not true of it.
+// parseHTML: without one tiptap reads the attribute off pasted HTML, where nothing has written it.
+const fact = { default: null, keepOnSplit: false, parseHTML: () => null, rendered: false };
+
+const endOf = (doc: PMNode): null | { at: number; eof: string } => {
+ let end: null | { at: number; eof: string } = null;
+
+ doc.forEach((block, at) => {
+ end ??= typeof block.attrs.eof === 'string' ? { at, eof: block.attrs.eof } : null;
+ });
+
+ return end;
+};
+
+export const MarkdownLayout = Extension.create({
+ addGlobalAttributes() {
+ return [
+ { attributes: { blank: fact, eof: fact, gap: fact }, types: BLOCKS },
+ { attributes: { indent: fact, trail: fact }, types: ['bulletList', 'orderedList'] },
+ { attributes: { marker: fact }, types: ['bulletList'] },
+ { attributes: { delimiter: fact, numbering: fact }, types: ['orderedList'] },
+ { attributes: { trail: fact, underline: fact }, types: ['heading'] },
+ { attributes: { lazyDepth: fact, lazyFrom: fact, number: fact, spaces: fact }, types: ['listItem'] },
+ { attributes: { blankLine: fact, fence: fact, unclosed: fact, written: fact }, types: ['codeBlock'] },
+ { attributes: { rule: fact }, types: ['horizontalRule'] },
+ { attributes: { form: fact }, types: ['link'] },
+ { attributes: { fence: fact }, types: ['code'] },
+ { attributes: { link: fact }, types: ['image'] },
+ { attributes: { delimiterRow: fact, emptyCell: fact, written: fact }, types: ['table'] },
+ ];
+ },
+ // The block that carries how the document ends can be joined into the one above, deleted, wrapped in a
+ // list or retyped by a list command, none of which is an edit of the document's end: the fact moves to
+ // the last block. Replacing the whole document is one.
+ addProseMirrorPlugins() {
+ return [
+ new Plugin({
+ appendTransaction(transactions, { doc: before }, { doc, tr }) {
+ const end = endOf(before);
+ const isReplaced = transactions.some(({ steps }) =>
+ steps.some(
+ (step) => step instanceof ReplaceStep && !step.from && step.to >= before.content.size,
+ ),
+ );
+
+ if (!end || endOf(doc) || isReplaced || !transactions.some(({ docChanged }) => docChanged)) {
+ return null;
+ }
+
+ const at = doc.content.size - (doc.lastChild?.nodeSize ?? 0);
+
+ return doc.nodeAt(at)?.type.spec.attrs?.eof ? tr.setNodeAttribute(at, 'eof', end.eof) : null;
+ },
+ }),
+ ];
+ },
+ name: 'markdownLayout',
+});
+
+const CLOSED_BLOCKS = new Set(['codeBlock', 'heading', 'horizontalRule']);
+const LISTS = new Set(['bulletList', 'orderedList', 'taskList']);
+const ENDS_AT_A_TAB_LINE = new Set([...CLOSED_BLOCKS, ...LISTS]);
+const OPENS_LIST_UNDER_TEXT = /^(?:[-*+]|1[.)])[ \t]+\S/;
+const OPENS_LIST = /^(?:([-*+])|\d{1,9}([.)]))[ \t]+\S/;
+const OPENS_DEFINITION = /^ {0,3}\[(?:\\.|[^[\]\\])+\]: *$/;
+const UNDERLINE = /^ {0,3}(?:=+|-+) *$/;
+const RULE_LINE = String.raw`(?:- *){3,}$|(?:_ *){3,}$|(?:\* *){3,}$`;
+const TAG_START = String.raw`<(?:[a-z].*>|!--)`;
+const OPENS_TAG = new RegExp(`^${TAG_START}`, 'i');
+
+const RULE = new RegExp(`^(?:${RULE_LINE})`);
+
+export const isRule = (line: string): boolean => RULE.test(line);
+
+const markerKindOf = (list: JSONContent): string =>
+ list.type === 'orderedList' ? (list.attrs?.delimiter === ')' ? ')' : '.') : bulletOf(list);
+
+// marked's own rule for a paragraph: it stops at a line that opens a block, a line of a known HTML tag among
+// them, though here that tag is text. Two lines are handed over: a line over a row of dashes and pipes ends
+// the text above it by the look of a table, whether it turns out to be one or not.
+const endsText = (lines: string): boolean => Lexer.rules.block.gfm.paragraph.exec(`x\n${lines}`)?.[1] === 'x';
+
+const columnsOf = (table: JSONContent): number => table.content?.[0]?.content?.length ?? 0;
+
+// Whether `next`, written as `written`, can stand on the line right under `previous` and still be read as a
+// block of its own — each pair was run through marked, with every kind of content the editor writes. Text
+// continues any block a blank line would have closed, except that outside a list item a line marked takes
+// for the start of a block ends the text above it, and any `<tag>` ends a list. `---` under a line of text,
+// an image included, is a setext underline, and a row of dashes and pipes under one is a table's delimiter
+// row. A quote or a table swallows a second one of its kind, and a table the list under it and any text that
+// is not a line where it ends by itself or one the loader takes for no row of it (isNotARow). A list opens
+// under text only with a first item that has text, and a numbered one only from 1; under another list it is
+// a list of its own only with a different bullet or delimiter.
+const canSitUnder = (previous: JSONContent, next: JSONContent, written: string, isInItem: boolean): boolean => {
+ const above = previous.type ?? '';
+ const [line = ''] = written.split('\n', 1);
+ const isUnderText = above === 'paragraph' && !TABLE_DELIMITER_LINE.test(line);
+
+ switch (next.type) {
+ case 'blockquote':
+ return above !== 'blockquote';
+ case 'bulletList':
+ case 'orderedList': {
+ if (above === 'bulletList' || above === 'orderedList') {
+ const [, bullet, delimiter] = OPENS_LIST.exec(written) ?? [];
+
+ return (bullet ?? delimiter ?? markerKindOf(previous)) !== markerKindOf(previous);
+ }
+
+ return CLOSED_BLOCKS.has(above) || (isUnderText && OPENS_LIST_UNDER_TEXT.test(written));
+ }
+
+ case 'codeBlock':
+ return true;
+ case 'heading':
+ return CLOSED_BLOCKS.has(above) || /^#{1,6}( [^\n]*)?$/.test(written);
+ case 'horizontalRule':
+ return (
+ !/^-/.test(written) ||
+ CLOSED_BLOCKS.has(above) ||
+ LISTS.has(above) ||
+ above === 'blockquote' ||
+ above === 'table'
+ );
+ case 'paragraph':
+ return (
+ CLOSED_BLOCKS.has(above) ||
+ (above === 'table' && (isTableBodyEnd(line) || isNotARow(line, columnsOf(previous)))) ||
+ (!isInItem && above === 'paragraph' && endsText(written.split('\n', 2).join('\n'))) ||
+ (!isInItem && (above === 'bulletList' || above === 'orderedList') && OPENS_TAG.test(line))
+ );
+ case 'table':
+ return CLOSED_BLOCKS.has(above) || isUnderText;
+ case 'taskList':
+ return CLOSED_BLOCKS.has(above);
+ default:
+ return false;
+ }
+};
+
+type Render = (block: JSONContent, index: number) => string;
+
+const endsWithText = (list: JSONContent): boolean => {
+ const block = list.content?.at(-1)?.content?.at(-1);
+
+ return LISTS.has(block?.type ?? '')
+ ? endsWithText(block!)
+ : block?.type === 'paragraph' && Boolean(block.content?.length);
+};
+
+// A code block as it was written, while those lines still read as the block it is now: its code and its
+// language, closed by its own last line. One written by indentation and edited since is written with every
+// line four spaces in, where that reads as its code — not with a language, a blank first line or a line break
+// at its end. Anything else is left to the fenced form.
+const writtenCode = (block: JSONContent): null | { isIndented: boolean; text: string } => {
+ const { language, written } = block.attrs ?? {};
+ const code = (block.content ?? []).map((node) => node.text ?? '').join('');
+
+ if (block.type !== 'codeBlock' || typeof written !== 'string') {
+ return null;
+ }
+
+ const isIndented = /^(?: {4}| {0,3}\t)/.test(written);
+
+ // A line of an indented block that looks like a fence is one to the scan that shields the tabs of fenced
+ // code on the next load: between fences longer than it, it is a line of code to every reader.
+ if (isIndented && /^[ \t>]*(?:`{3,}|~{3,})/m.test(code)) {
+ return null;
+ }
+
+ // So is a closing fence that scan does not take for one: a run of one character and nothing after it.
+ if (!isIndented && !/^ {0,3}(?:`+|~+) *$/.test(written.slice(written.lastIndexOf('\n') + 1))) {
+ return null;
+ }
+
+ const typed = code
+ .split('\n')
+ .map((line) => (line ? ` ${line}` : line))
+ .join('\n');
+
+ const reads = (text: string): boolean => {
+ const [token, next] = new Lexer({ gfm: true }).blockTokens(`${text}\nx`) as (LayoutToken & {
+ lang?: string;
+ })[];
+ const read = token?.text ?? '';
+
+ // An indented block is read without the line break marked leaves at its end, as the loader takes it.
+ return (
+ token?.type === 'code' &&
+ next?.type === 'paragraph' &&
+ next.raw === 'x' &&
+ (isIndented ? read.slice(0, read.length - lineBreaksAtEnd(read).length) : read) === code &&
+ (token.lang ?? '') === (typeof language === 'string' ? language : '')
+ );
+ };
+
+ const text = [written, ...(isIndented ? [typed] : [])].find(reads);
+
+ return text === undefined ? null : { isIndented, text };
+};
+
+// Two lists of one kind next to each other, which only an edit leaves behind, are one list to every reader:
+// they are written as one, numbered through, with no blank line that would make it a loose one. Loose task
+// items are read as lists of their own, and the blank line between those was written.
+const withListsJoined = (blocks: JSONContent[]): JSONContent[] =>
+ blocks.reduce<JSONContent[]>((joined, block) => {
+ const previous = joined.at(-1);
+ const isWrittenApart = block.type === 'taskList' && Number(block.attrs?.gap) > 0;
+
+ if (
+ previous &&
+ LISTS.has(block.type ?? '') &&
+ previous.type === block.type &&
+ markerKindOf(previous) === markerKindOf(block) &&
+ !isWrittenApart
+ ) {
+ joined[joined.length - 1] = {
+ ...previous,
+ content: [...(previous.content ?? []), ...(block.content ?? [])],
+ };
+ } else {
+ joined.push(block);
+ }
+
+ return joined;
+ }, []);
+
+// `gap` is the number of blank lines the block was written under: none keeps it on the next line where that
+// is safe, any keeps the blank line. A block the user made has no gap and takes `isTightByDefault`, which is
+// how the blocks of a list item are joined and those of the document are not. A block that writes nothing —
+// an empty paragraph — is a blank line of its own, and one more on each side of it is what brings it back on
+// the next load. Two things an edit can leave reach past the block above: a `[label]:` line takes the line
+// under it for its target, and marked reads text, a `***` rule right under it and then an underline as one
+// setext heading.
+export const joinBlocks = (
+ siblings: JSONContent[],
+ render: Render,
+ isTightByDefault: boolean,
+ place: (written: string, index: number, block: JSONContent) => string = (written) => written,
+): string => {
+ const blocks = withListsJoined(siblings);
+ let above = '';
+ let aboveType = '';
+ let empty = 0;
+ let isAboveIndented = false;
+ // A list above takes a line written as far in as the content of its last item for that item's, whatever
+ // blank lines stand between: the list as it is written, until a line at the margin has ended it.
+ let listAbove: JSONContent | undefined;
+ let openList = '';
+ let isDashOpen = false;
+ let isTextOpen = false;
+
+ return blocks.reduce((output, block, index) => {
+ // The lines a code block was written as, where they stand for themselves: at the margin of the
+ // document or of a quote. In a list item — and in a quote inside one, where marked turns tabs into
+ // spaces — indentation is the item's own, so is that of a line under a list, and two indented blocks
+ // one under the other are one.
+ const kept = itemDepth > 0 ? null : writtenCode(block);
+ const isKept =
+ kept !== null && !(openList && /^[ \t]/.test(kept.text)) && !(kept.isIndented && isAboveIndented);
+ const indented = isKept && kept.isIndented ? kept.text : null;
+ const rendered = isKept ? kept.text : render(block, index);
+ // Nor do the lines of a table keep the spaces they open with there, or text its own where the list
+ // would take it: a task item takes any line that does not start at the margin, and for any other
+ // list marked is asked about a word written that far in.
+ const [lead] = /^[ \t]*/.exec(rendered)!;
+ const isTaken =
+ block.type === 'paragraph' &&
+ openList !== '' &&
+ lead !== '' &&
+ (listAbove?.type === 'taskList' ||
+ new Lexer({ gfm: true }).blockTokens(`${openList}\n\n${lead}x`).at(-1)?.raw !== `${lead}x`);
+ const written =
+ block.type === 'table' && (openList || itemDepth > 0)
+ ? rendered
+ .split('\n')
+ .map((line) => line.trimStart())
+ .join('\n')
+ : isTaken
+ ? rendered.trimStart()
+ : rendered;
+ const previous = blocks[index - 1];
+ const gap: unknown = block.attrs?.gap;
+ // The underline of a heading is its last line; any other block reaches the text above only with its first.
+ const [first = ''] = written.split('\n', 1);
+ const underline = block.type === 'heading' ? written.slice(written.lastIndexOf('\n') + 1) : first;
+ const isClaimed =
+ OPENS_DEFINITION.test(above.slice(above.lastIndexOf('\n') + 1)) ||
+ (isTextOpen && previous?.type === 'horizontalRule' && UNDERLINE.test(underline));
+ const isTight =
+ above !== '' &&
+ written !== '' &&
+ !isClaimed &&
+ (typeof gap === 'number' ? gap === 0 : isTightByDefault) &&
+ (indented === null
+ ? canSitUnder(blocks[index - 1]!, block, written, isTightByDefault)
+ : CLOSED_BLOCKS.has(aboveType));
+ const { blank, indent } = block.attrs ?? {};
+ // A blank line is written with its spaces between two blocks that write something; with a tab in it
+ // only under a block a tab does not continue — it is a row to a table and a line to text.
+ const isBlankWritten =
+ above !== '' &&
+ written !== '' &&
+ typeof blank === 'string' &&
+ /^[ \t]+$/.test(blank) &&
+ (!blank.includes('\t') || ENDS_AT_A_TAB_LINE.has(previous?.type ?? ''));
+ // Every two blank lines past the first are read as an empty paragraph, which writes two: of an even
+ // number one is left over, and is written while the empty paragraphs above the block are the ones it
+ // was read with — half an odd number is no count of them.
+ // Not between a list of `-` bullets and the task lists under it, which are one list to marked: there
+ // the line is an empty paragraph of an item.
+ const isDashList = block.type === 'taskList' || (block.type === 'bulletList' && bulletOf(block) === '-');
+ const isOneList = isDashOpen && isDashList && openList !== '';
+ const isOneMore = typeof gap === 'number' && gap > 1 && empty === gap / 2 - 1 && !isOneList;
+ // Under a task list one of the blank lines is read as part of the list: an odd count above two comes
+ // back two short there, and any count one short where an empty paragraph stands between.
+ const isTwoMore =
+ typeof gap === 'number' && gap > 2 && gap % 2 === 1 && empty === (gap - 1) / 2 - 1 && !isOneList;
+ const more =
+ aboveType === 'taskList' && written !== ''
+ ? isTwoMore
+ ? 2
+ : isOneMore || empty > 0
+ ? 1
+ : 0
+ : Number(isOneMore);
+ const separator = index ? (isTight ? '\n' : isBlankWritten ? `\n${blank}\n` : `\n\n${'\n'.repeat(more)}`) : '';
+ // A list keeps the spaces its markers were written after, except where they reach the column an item
+ // of the list above — the nearest block above that writes anything — has its content at, which would
+ // nest it there, and with a line left of its items, which those spaces would move. A task item takes
+ // any line that does not start at the margin.
+ const contentColumn =
+ aboveType === 'orderedList' ? 3 : aboveType === 'taskList' ? 1 : LISTS.has(aboveType) ? 2 : 4;
+ const pad =
+ (block.type === 'bulletList' || block.type === 'orderedList') &&
+ (indent === 1 || indent === 2 || indent === 3) &&
+ indent < contentColumn &&
+ !/"lazyFrom":\d/.test(JSON.stringify(block))
+ ? indent
+ : 0;
+ // The spaces a list ended with are written after the text of its last item, and only above a block
+ // that is not a list: at the end of the document nothing reads them back, and above a list they are
+ // text of the item.
+ const { trail } = block.attrs ?? {};
+ const hasTrail =
+ typeof trail === 'string' && /^ +$/.test(trail) && LISTS.has(block.type ?? '') && endsWithText(block);
+ const under = hasTrail ? blocks.slice(index + 1).find((later) => !isWrittenEmpty(later)) : undefined;
+ const isTrailed = under !== undefined && !LISTS.has(under.type ?? '');
+ const placed = (pad ? indentList(written, pad) : written) + (isTrailed ? trail : '');
+
+ isTextOpen =
+ block.type === 'paragraph' || block.type === 'image'
+ ? written !== ''
+ : block.type === 'horizontalRule' && isTextOpen && isTight;
+ above = written;
+ aboveType = written === '' ? aboveType : (block.type ?? '');
+ empty = written === '' ? empty + 1 : 0;
+ isAboveIndented = written.trim() === '' ? isAboveIndented : indented !== null;
+
+ if (LISTS.has(block.type ?? '') && written !== '') {
+ isDashOpen =
+ block.type === 'taskList' ? isDashOpen : block.type === 'bulletList' && bulletOf(block) === '-';
+ listAbove = block;
+ openList = placed;
+ } else if (written.trim() !== '' && (block.type === 'paragraph' || /^\S/.test(written))) {
+ // Text that kept its spaces is what marked did not give to the list; white space alone is a blank
+ // line to it.
+ isDashOpen = false;
+ openList = '';
+ }
+
+ return output + separator + place(placed, index, block);
+ }, '');
+};
+
+// A line of an item's paragraph that is written left of the content column carries one mark for every
+// enclosing item that must leave it there; an item takes a mark off instead of indenting.
+const LAZY_LINE = '\uE001';
+const LAZY_MARKS = /^\uE001+/;
+
+const indentList = (list: string, indent: number): string =>
+ list
+ .split('\n')
+ .map((line) => (line ? ' '.repeat(indent) + line : line))
+ .join('\n');
+
+// A document that holds the mark itself, in its text or in an attribute: it is written with every line
+// indented, and nothing in it is taken for a mark.
+let hasOwnMarks = false;
+
+// marked keeps an under-indented line in an item only as the continuation of a line of text: not under a
+// blank line, not under one whose remainder past the content column opens a block or indented code, and not
+// if the line itself opens one — `<tag>` and `#word` included, which end an item though they are text
+// anywhere else.
+const BLOCK_START = new RegExp(
+ `^ {0,3}(?:#|>|${TAG_START}|\`{3}|~{3}|(?:[*+-]|\\d{1,9}[.)])(?:[ \\t]|$)|${RULE_LINE})`,
+ 'i',
+);
+const ENDS_LAZINESS = new RegExp(`^ {0,3}(?:#|\`{3}|~{3}|${RULE_LINE})`);
+
+const mayStayLazy = (line: string, above: string): boolean =>
+ /^\S/.test(line) &&
+ !BLOCK_START.test(line) &&
+ above.trim() !== '' &&
+ !above.startsWith(' ') &&
+ !ENDS_LAZINESS.test(above);
+
+// Indents the lines of one block of an item to the item's content column, from line `from` on. A line from
+// `lazyFrom` on, or one a nested item has marked, stays where it is while marked would still read it as a
+// continuation; the first that would not is indented, and so is every such line right under it. An empty
+// line is written as `blank`.
+export const indentItemBlock = (
+ block: string,
+ pad: string,
+ from = 0,
+ lazyFrom = Infinity,
+ lazyDepth = 1,
+ blank = '',
+): string => {
+ let above = '';
+ let isRefused = false;
+
+ return block
+ .split('\n')
+ .map((line, index) => {
+ const text = hasOwnMarks ? line : line.replace(LAZY_MARKS, '');
+ const isLazy = !hasOwnMarks && (text.length < line.length || index >= lazyFrom);
+
+ if (index < from || !line) {
+ above = text;
+ isRefused = false;
+
+ return line || blank;
+ }
+
+ if (isLazy && !isRefused && mayStayLazy(text, above)) {
+ above = text.replaceAll('\t', ' ').slice(pad.length);
+
+ return text.length < line.length ? line.slice(1) : LAZY_LINE.repeat(lazyDepth - 1) + text;
+ }
+
+ above = text;
+ isRefused = isLazy;
+
+ return pad + text;
+ })
+ .join('\n');
+};
+
+// The spaces the empty lines of a code block were written with in its item, while they still fit under the
+// item's marker: past its content column they would be read as the code's own.
+export const blankLineOf = (block: JSONContent, pad: string): string => {
+ const blank: unknown = block.attrs?.blankLine;
+
+ return typeof blank === 'string' && /^ +$/.test(blank) && blank.length <= pad.length ? blank : '';
+};
+
+export const isWrittenEmpty = (block: JSONContent | undefined): boolean =>
+ block?.type === 'paragraph' && !block.content?.length;
+
+// The blocks of the document, and around them what belongs to no block: the fence an unclosed code block
+// never had — an opening fence with nothing under it, not even a line break, is text of the block above —
+// and the line breaks the document ended with. Those stand for the empty paragraphs at its end —
+// the ones they were read as, and the one TrailingNode puts under a last block that is not a paragraph as
+// soon as anything is edited. A heading without text, or text that is only spaces outside a code span,
+// writes nothing either, and left at the end it would put two line breaks there that nobody wrote. Under an
+// unclosed block all but the last of them are lines of its code.
+export const renderDocument = (doc: JSONContent, render: Render): string => {
+ const all = doc.content ?? [];
+ // A heading is written trimmed; in a paragraph only spaces and tabs are nothing to marked.
+ const isNothing = (text: string, type?: string) => (type === 'heading' ? !text.trim() : /^[ \t]*$/.test(text));
+ const writesNothing = (block: JSONContent) =>
+ (block.type === 'heading' || block.type === 'paragraph') &&
+ (block.content ?? []).every(
+ (node) =>
+ node.type === 'hardBreak' ||
+ (node.type === 'text' &&
+ isNothing(node.text ?? '', block.type) &&
+ !node.marks?.some((mark) => mark.type === 'code')),
+ );
+ const blocks = all.slice(0, all.findLastIndex((block) => !writesNothing(block)) + 1);
+ const last = blocks.at(-1);
+ const eof: unknown = all.find((block) => typeof block.attrs?.eof === 'string')?.attrs?.eof;
+ const hasLineBreak = typeof eof === 'string' && eof !== '' && lineBreaksAtEnd(eof) === eof;
+ const code = last?.content?.[0]?.text ?? '';
+ const isUnclosed = last?.type === 'codeBlock' && last.attrs?.unclosed === true && (code !== '' || hasLineBreak);
+
+ hasOwnMarks = JSON.stringify(blocks).includes(LAZY_LINE);
+
+ let written = joinBlocks(blocks, render, false);
+
+ written = hasOwnMarks ? written : written.replaceAll(LAZY_LINE, '');
+
+ if (isUnclosed) {
+ written = written.slice(0, written.lastIndexOf('\n') - (code ? 0 : 1));
+ }
+
+ if (typeof eof === 'string' && hasLineBreak) {
+ return written + (isUnclosed ? '\n' : eof);
+ }
+
+ // The empty line an open block ends with is read as the document's line break, unless one more follows.
+ return isUnclosed && code.endsWith('\n') ? `${written}\n` : written;
+};
+
+const itemMarkers = new WeakMap<JSONContent, string>();
+
+export const itemMarker = (item: JSONContent): string => itemMarkers.get(item) ?? '- ';
+
+const bulletOf = (list: JSONContent): string => {
+ const marker: unknown = list.attrs?.marker;
+
+ return marker === '*' || marker === '+' ? marker : '-';
+};
+
+const isListNumber = (value: unknown): value is number =>
+ typeof value === 'number' && Number.isInteger(value) && value >= 0 && value <= 999_999_999;
+
+// A list writes its items' markers, because a number depends on the items above it.
+export const renderList = (list: JSONContent, render: (item: JSONContent, index: number) => string): string => {
+ const { numbering, start } = list.attrs ?? {};
+ const first = isListNumber(start) ? start : 1;
+ let number = first - 1;
+
+ const items = list.content ?? [];
+
+ return items.reduce((output, item, index) => {
+ const { gap, number: written, spaces } = item.attrs ?? {};
+
+ number = numbering === 'own' && isListNumber(written) ? written : numbering === 'same' ? first : number + 1;
+ itemMarkers.set(
+ item,
+ (list.type === 'orderedList' ? `${number}${markerKindOf(list)}` : bulletOf(list)) +
+ ' '.repeat(typeof spaces === 'number' && spaces > 1 && spaces < 5 ? spaces : 1),
+ );
+
+ // Two blank lines between items load as an empty paragraph closing the upper item, which writes one
+ // of them itself.
+ const isLoose = typeof gap === 'number' && gap > 0 && !isWrittenEmpty(items[index - 1]?.content?.at(-1));
+ // The spaces the one blank line above the item was written with.
+ const blank = gap === 1 && typeof item.attrs?.blank === 'string' && /^ +$/.test(item.attrs.blank);
+
+ return output + (index ? (isLoose ? `\n${blank ? item.attrs?.blank : ''}\n` : '\n') : '') + render(item, index);
+ }, '');
+};
+
+type Alignment = 'center' | 'left' | 'right' | null;
+
+const alignmentOf = (cell: string): Alignment | undefined => {
+ const [, left, right] = /^(:?)-+(:?)$/.exec(cell.trim()) ?? [];
+
+ if (left === undefined) {
+ return undefined;
+ }
+
+ return left && right ? 'center' : left ? 'left' : right ? 'right' : null;
+};
+
+const DELIMITER_CELL: Record<string, string> = { center: ':---:', left: ':---', right: '---:' };
+
+const alignmentsOf = (table: JSONContent): Alignment[] => {
+ const rows = (table.content ?? []).map((row) => row.content ?? []);
+ const width = rows.reduce((max, row) => Math.max(max, row.length), 0);
+
+ return Array.from(
+ { length: width },
+ (_, column): Alignment =>
+ (rows.map((row) => row[column]?.attrs?.align).find(Boolean) as Alignment | undefined) ?? null,
+ );
+};
+
+// Whether a delimiter row still describes the columns: their number and their alignment.
+const describes = (delimiterRow: string, alignments: Alignment[]): boolean => {
+ const written = delimiterRow
+ .trim()
+ .replace(/^\||\|$/g, '')
+ .split('|')
+ .map(alignmentOf);
+
+ return (
+ delimiterRow.includes('|') &&
+ written.length === alignments.length &&
+ written.every((alignment, column) => alignment === alignments[column])
+ );
+};
+
+// The table as rows of cell text, each cell one line, with one space around it and an empty cell as the one
+// or two spaces the table was written with. The delimiter row is the written one while it still describes the
+// columns, and a plain one once the table has been reshaped.
+export const renderCompactTable = (table: JSONContent, delimiterRow: string, emptyCell: unknown): string => {
+ const alignments = alignmentsOf(table);
+ const line = (cells: string[]) =>
+ `|${alignments.map((_, column) => (cells[column] ? ` ${cells[column]} ` : emptyCell === 2 ? ' ' : ' ')).join('|')}|`;
+ const texts = (table.content ?? []).map((row) =>
+ (row.content ?? []).map((cell) => (cell.content?.[0]?.text ?? '').trim()),
+ );
+
+ return [
+ line(texts[0] ?? []),
+ describes(delimiterRow, alignments)
+ ? delimiterRow
+ : line(alignments.map((alignment) => DELIMITER_CELL[alignment ?? ''] ?? '---')),
+ ...texts.slice(1).map(line),
+ ].join('\n');
+};
+
+// How many list items the writer is inside of.
+let itemDepth = 0;
+
+export const withinItem = <T>(write: () => T): T => {
+ itemDepth += 1;
+
+ try {
+ return write();
+ } finally {
+ itemDepth -= 1;
+ }
+};
+
+// A line that holds something: one of nothing but white space is a row only until a quote or a list item is
+// written around it.
+const isLine = (value: unknown): value is string =>
+ typeof value === 'string' && value.trim() !== '' && !value.includes('\n');
+
+// The lines of a table as it is written now, each replaced by the line it was written as while that line
+// still says the same: a row by the cells marked read in it — the writer's own line holds the same ones once
+// marked has trimmed them and taken the backslash off an escaped pipe — and the delimiter row by the columns
+// it describes. Rows are matched by what they hold, not by where they stand, so a row added, removed or moved
+// costs no other row its line; the header only by the header, which a line without a pipe cannot be. In a
+// list item a line that needs a pipe protected is not kept: the scan that protects it on the next load does
+// not find every table there.
+export const withWrittenRows = (lines: string[], table: JSONContent, written: unknown): string => {
+ const entries = Array.isArray(written) ? written.filter((entry): entry is unknown[] => Array.isArray(entry)) : [];
+ const [header, delimiter, ...body] = entries;
+ const keyOf = (cells: unknown[]): string => JSON.stringify(cells);
+ const rows = (table.content ?? []).map((row) =>
+ keyOf((row.content ?? []).map((cell) => (cell.content?.[0]?.text ?? '').trim().replace(/\\\|/g, '|'))),
+ );
+ const columns = alignmentsOf(table).length;
+ // A line that would end the table once another stands under it — a fence, a tag — is a row only as the
+ // last line of its container.
+ const isKept = (raw: unknown): raw is string =>
+ isLine(raw) && !isTableBodyEnd(raw) && !(itemDepth > 0 && holdsProtectedPipe(raw, columns));
+ // Rows that hold the same cells are handed out in the order they were written in.
+ const kept = new Map<string, { next: number; raws: string[] }>();
+
+ for (const [raw, ...cells] of body) {
+ if (isKept(raw)) {
+ const key = keyOf(cells);
+ const same = kept.get(key) ?? { next: 0, raws: [] };
+
+ same.raws.push(raw);
+ kept.set(key, same);
+ }
+ }
+
+ return lines
+ .map((line, at) => {
+ if (at === 1) {
+ return isLine(delimiter?.[0]) && describes(delimiter[0], alignmentsOf(table)) ? delimiter[0] : line;
+ }
+
+ if (!at) {
+ return header && isKept(header[0]) && keyOf(header.slice(1)) === rows[0] ? header[0] : line;
+ }
+
+ const same = kept.get(rows[at - 1] ?? '');
+
+ return same?.raws[same.next++] ?? line;
+ })
+ .join('\n');
+};
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-marked.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-marked.test.ts
new file mode 100644
index 00000000..aba4e496
--- /dev/null
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-marked.test.ts
@@ -0,0 +1,642 @@
+import type { JSONContent } from '@tiptap/core';
+
+import { Editor } from '@tiptap/core';
+import { beforeAll, describe, expect, it } from 'vitest';
+
+import { GROWTH_IF_QUADRATIC, slowdownWhenInputQuadruples } from '@/test-utils/cost-growth';
+
+import { createMarkdownExtensions } from './markdown-editor-extensions';
+import { markdownCodec, roundTrip, setupEditorJsdom } from './markdown-editor-test-setup';
+
+beforeAll(setupEditorJsdom);
+
+const load = (content: JSONContent | string) =>
+ new Editor({
+ content,
+ ...(typeof content === 'string' ? { contentType: 'markdown' as const } : {}),
+ extensions: createMarkdownExtensions(),
+ });
+
+const textsOf = (markdown: string): null | string[] => {
+ const editor = load(markdown);
+ const texts = JSON.stringify(editor.getJSON()).match(/"text":"[^"]*"/g);
+
+ editor.destroy();
+
+ return texts;
+};
+
+// The inline nodes of every paragraph, a line break as `/`.
+const linesOf = (markdown: string): string[] => {
+ const editor = load(markdown);
+ const paragraphs: string[] = [];
+
+ editor.state.doc.descendants((node) => {
+ if (node.type.name === 'paragraph') {
+ const inline: string[] = [];
+
+ node.forEach((child) => inline.push(child.isText ? (child.text ?? '') : '/'));
+ paragraphs.push(inline.join('|'));
+ }
+ });
+ editor.destroy();
+
+ return paragraphs;
+};
+
+// What the document is after `edit`, and what it is saved as.
+const afterEdit = (content: JSONContent | string, edit: (editor: Editor) => void = () => {}): string => {
+ const editor = load(content);
+
+ edit(editor);
+
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ return saved;
+};
+
+describe('inline text is saved the way it was written', () => {
+ it.each([
+ ['five asterisks', 'at §*****HTTP URL with Credentials*****§ now'],
+ ['uneven asterisks', '| a |\n|---|\n| §**ipv4***§ |'],
+ ['bold around it', '**§*CVE Number*§ Advisory**'],
+ ['emphasis around it', '*a §****ipv4*****§ b*'],
+ ['a backslash before it', '*a \\§****ipv4*****§ b*'],
+ ['a closer left over from a second pass', '**URL**: §*****HTTP URL*****§ Name Enhanced*§.net'],
+ ['an asterisk right after it', 'text §*a*§*b*'],
+ ['bold earlier on its line', '**Note:** see **records**. Spec: §***HTTP URL****§ Name Enhanced*§.com/en-us/x'],
+ ])('asterisks of an anonymizer placeholder, with %s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ ['padded with spaces', 'use `` alert(`XSS`) `` and ` a ` here'],
+ ['padded because it holds a backtick', 'a `` `x` `` b'],
+ ['unbalanced by a stray backtick', 'see `curl x and the `Set-Cookie` verbatim (keep `Bearer ` now'],
+ ['that runs over a line', 'A PHP `Warning: failed to\nopen stream` message'],
+ ['that runs over a line in an item', '- see `foo\n bar` baz\n- next'],
+ ['inside emphasis over a line', '**x `c\nd` y**'],
+ ['that opens and ends with a line break', '`\nfoo\n`'],
+ ])('a code span %s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ ['emphasis over a line', '*a\nb* and **c\nd**'],
+ ['emphasis on each of two lines', '*a*\n*b*'],
+ ['a link on each of two lines', '[x](/u "t")\n[x](/u "t")'],
+ ['a link text over a line', '[l\nm](/u)'],
+ ['asterisks that close nothing', '- **No /dev/sd*, /dev/vd*, /dev/fuse**'],
+ ['a line indented four past its item', '- item\n indented\n more\n- next'],
+ ['a numbered line that is the text of a loose item', '- 1. First thing\n\n- 2. Second thing'],
+ ['a backslash before a hash inside emphasis', '**\\#hashtag** and *\\>x*'],
+ ['a backslash before a hash in a link text', '[\\#1](http://a.b) text'],
+ ['a backslash before a hash in struck text', 'text ~~\\#no~~'],
+ ])('%s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+});
+
+describe('a Go template action is literal text', () => {
+ // Byte identity alone passes for a mark that writes its own delimiters back, hence the mark count.
+ it.each([
+ ['an entity in a string', '{{ printf "&amp; &lt;x&gt;" }} done', '"italic"', 0],
+ ['a quote entity in a string', '{{ printf "&quot;%s&quot;" .Name }}', '"italic"', 0],
+ ['an entity in a string after text', 'text {{ printf "&lt;" }} more', '"italic"', 0],
+ ['a raw string beside a code span', '{{ printf `%s` .A }} and `code` and {{ `x` }}', '"code"', 1],
+ ['a raw string after text, beside a code span', 'run {{ printf `a` }} and `code`', '"code"', 1],
+ ['a raw string with a pipe in a cell', '| a | b |\n|---|---|\n| {{ printf `a|b` }} | 2 |', '"code"', 0],
+ ['an asterisk in a comment', '*a {{/* c */}} b*', '"italic"', 1],
+ ['asterisks inside and outside', '*note {{ mul 2 3 }}* and {{/* c */}} *x*', '"italic"', 2],
+ ['a URL in a string', '{{ template "x" "https://a.b/c" }} and https://a.b/d', '"link"', 1],
+ ['a URL that runs into it', 'See https://example.com/{{ .Path }} now', '"link"', 0],
+ ['bold around it', '**{{ .Name }}** and *{{ .X | print }}*', '"bold"', 1],
+ ['a brace in a string', 'send {{ printf "{%s} &lt;" .X }} as JSON', '"link"', 0],
+ ['a quote in a comment', 'x {{/* 5" &lt; */}} y', '"link"', 0],
+ ['an apostrophe that opens no rune', "{{ .Team's &amp; }} done", '"link"', 0],
+ ['JSON around it', '{"name": "{{ printf "&lt;%s" .Name }}", "n": {{ .N }}}', '"link"', 0],
+ [
+ 'a brace in a string, in a code span in a cell',
+ '| a |\n|---|\n| `{{ printf "{%s}" .X | print }}` |',
+ '"code"',
+ 1,
+ ],
+ [
+ 'a brace in a string in a cell',
+ '| a | b |\n|---|---|\n| {{ printf "{%s}" .X | print }} | 2 |',
+ '"tableCell"',
+ 2,
+ ],
+ [
+ 'the end of an action in a string in a cell',
+ '| a | b |\n|---|---|\n| {{ print "}}" | print }} | 2 |',
+ '"tableCell"',
+ 2,
+ ],
+ [
+ 'a quote in a rune in a cell',
+ '| a | b |\n|---|---|\n| {{ printf "%c" \'"\' | print }} | 2 |',
+ '"tableCell"',
+ 2,
+ ],
+ [
+ 'a pipeline in a code span beside an escaped pipe',
+ '| a | b |\n|---|---|\n| `x \\| {{ .A | print }}` | 2 |',
+ '"tableCell"',
+ 2,
+ ],
+ [
+ 'a pipeline in a code span between bare pipes',
+ '| a | b |\n|---|---|\n| `x | {{ .A | print }} | y` | 2 |',
+ '"tableCell"',
+ 2,
+ ],
+ [
+ 'a raw string with a pipe beside an escaped pipe',
+ '| a | b |\n|---|---|\n| {{ printf `a|b` }} | `p \\| q` |',
+ '"tableCell"',
+ 2,
+ ],
+ ])('%s', (_name, md, kind, count) => {
+ const editor = load(md);
+ const found = JSON.stringify(editor.getJSON()).split(`"type":${kind}`).length - 1;
+
+ editor.destroy();
+
+ expect(found).toBe(count);
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it('still decodes an entity in the text beside one', () => {
+ expect(roundTrip('a &amp; b {{ .X }} c &lt; d')).toBe('a & b {{ .X }} c < d');
+ });
+
+ it('leaves emphasis between a code span that holds an opener and one that holds a closer to be read', () => {
+ const editor = load('Jinja uses `{{` *expression* `}}` syntax');
+ const italics = (editor.getJSON().content?.[0]?.content ?? [])
+ .filter((node: JSONContent) => node.marks?.some((mark) => mark.type === 'italic'))
+ .map((node: JSONContent) => node.text);
+
+ editor.destroy();
+
+ expect(italics).toEqual(['expression']);
+ });
+
+ it.each([
+ ['one over two lines', '{{ if\n.X }}a{{ end }}', ['{{ if|/|.X }}a{{ end }}']],
+ ['a pipeline over two lines', 'x {{ .Name\n| print }} y', ['x {{ .Name|/|| print }} y']],
+ ])('keeps the line break inside %s as a break node', (_name, md, lines) => {
+ expect(linesOf(md)).toEqual(lines);
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it('is still whole after the URL before it is edited', () => {
+ const saved = afterEdit('See https://example.com/{{ .Path }} now', (editor) =>
+ editor.chain().setTextSelection(25).insertContent('x').run(),
+ );
+
+ expect(saved).toBe('See https://example.com/x{{ .Path }} now');
+ });
+});
+
+describe('a pipe inside a code span of a table cell', () => {
+ // The loader reads a pipe inside a code span as content whether it is escaped or not, so a row is saved
+ // as the line it was; one that has been edited, or was made in the editor, is written the way GFM asks.
+ it.each([
+ ['written escaped', '| a | b |\n| --- | --- |\n| `x \\| y` | z |'],
+ ['written bare', '| a | b |\n| --- | --- |\n| `x | y` | z |'],
+ ['written both ways', '| a | b |\n| --- | --- |\n| `x | y` | `p \\| q` |'],
+ ['bare beside a pipe in a link', '| a | b |\n| --- | --- |\n| `x | y` | [l](http://h/?a=1|2) end |'],
+ ['escaped inside an action, in a code span', '| a | b |\n| --- | --- |\n| `x{{364\\|add:733}}y` | z |'],
+ ['escaped inside an action', '| a | b |\n| --- | --- |\n| {{ .A \\| upper }} | `p \\| q` |'],
+ [
+ 'escaped inside one action and bare inside another',
+ '| a | b |\n| --- | --- |\n| `{{ a \\| b }}` | {{ c | d }} |',
+ ],
+ ['bare under a header cell that holds a pipeline', '| {{ .A | print }} | b |\n| --- | --- |\n| `x | y` | z |'],
+ ])('saves a pipe inside a code span %s as it was written', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ [
+ 'a pipe in a code span',
+ '| a | b |\n| --- | --- |\n| `x | y` | z |\n| `p | q` | r |',
+ 'z',
+ '| a | b |\n| --- | --- |\n| `x \\| y` | z! |\n| `p | q` | r |',
+ ],
+ [
+ 'a pipe in a link',
+ '| a | b |\n| --- | --- |\n| [go](https://h/?x=1|2) | z |',
+ 'z',
+ '| a | b |\n| --- | --- |\n| [go](https://h/?x=1\\|2) | z! |',
+ ],
+ [
+ 'a pipe in an action, which is never escaped',
+ '| a | b |\n| --- | --- |\n| `{{ a \\| b }}` {{ c | d }} | z |',
+ 'z',
+ '| a | b |\n| --- | --- |\n| `{{ a | b }}` {{ c | d }} | z! |',
+ ],
+ ])('writes %s the way GFM asks once its row is edited', (_name, md, cell, saved) => {
+ const edited = afterEdit(md, (editor) => {
+ let at = 0;
+
+ editor.state.doc.descendants((node, position) => {
+ at = node.text === cell ? position + node.nodeSize : at;
+ });
+ editor.chain().setTextSelection(at).insertContent('!').run();
+ });
+
+ expect(edited).toBe(saved);
+ expect(roundTrip(edited)).toBe(edited);
+ });
+
+ // marked counts the cells of the header row before anything protects a pipe in it.
+ it('escapes a pipe typed into a code span of the header and leaves the rows under it as they were', () => {
+ const saved = afterEdit('| a | b |\n|---|---|\n| `x|y` | 2 |', (editor) =>
+ editor
+ .chain()
+ .setTextSelection(5)
+ .insertContent({ marks: [{ type: 'code' }], text: 'm|n', type: 'text' })
+ .run(),
+ );
+
+ expect(saved).toBe('| a`m\\|n` | b |\n|---|---|\n| `x|y` | 2 |');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('escapes a pipe typed into a code span of a table made in the editor', () => {
+ const cell = (type: string, content: object[]) => ({ content: [{ content, type: 'paragraph' }], type });
+ const editor = new Editor({
+ content: {
+ content: [
+ {
+ content: [
+ { content: [cell('tableHeader', [{ text: 'a', type: 'text' }])], type: 'tableRow' },
+ {
+ content: [
+ cell('tableCell', [{ marks: [{ type: 'code' }], text: 'x | y', type: 'text' }]),
+ ],
+ type: 'tableRow',
+ },
+ ],
+ type: 'table',
+ },
+ ],
+ type: 'doc',
+ },
+ extensions: createMarkdownExtensions(),
+ });
+ const saved = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(saved).toContain('`x \\| y`');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+});
+
+describe('a line under a table that is not a row of it', () => {
+ // marked takes any line under a table for a row, pads it into cells and drops what is past the header's
+ // count. The table ends above such a line and the line is the text it was written as.
+ it.each([
+ [
+ "a template's range around the rows",
+ '| a | b |\n|---|---|\n{{ range .Rows }}\n| {{ .Name }} | {{ .Val }} |\n{{ end }}',
+ 1,
+ ],
+ [
+ "a range that opens on the first row's line",
+ '| a | b |\n|---|---|\n{{ range .Rows }}| {{ .Name | print }} | {{ .Val }} |\n{{ end }}',
+ 1,
+ ],
+ [
+ 'a range around a table and its header',
+ '{{ range .Tables }}\n| a | b |\n|---|---|\n| {{ .A }} | {{ .B }} |\n{{- end }}',
+ 2,
+ ],
+ ['a control action written with a tab', '| a | b |\n|---|---|\n| 1 | 2 |\n{{\tend }}', 2],
+ ['an assignment on a line of its own', '| a | b |\n|---|---|\n{{- $n := len .Rows }}\n| 1 | 2 |', 1],
+ ['a comment on a line of its own', '| a | b |\n|---|---|\n| 1 | 2 |\n{{/* rows */}}', 2],
+ ['a pipeline on a line of its own', '| a |\n|---|\n{{ .X | print }}', 1],
+ [
+ 'an action after a row has closed',
+ '| a | b |\n|---|---|\n| static | row |{{ range .Items }}\n| {{ .Name }} | {{ .Value }} |{{ end }}',
+ 1,
+ ],
+ ['a row with a cell too many', '| a | b |\n|---|---|\n| 1 | 2 |\n| 3 | 4 | 5 |\n| 6 | 7 |', 2],
+ ['a row with a cell too many in a list item', '- t:\n | a | b |\n |---|---|\n | 1 | 2 | 3 |\n- next', 1],
+ [
+ 'a control action on the line of a row that has no cell too many',
+ '| a | b | c |\n|---|---|---|\n{{ if .A }}| x | y |\n{{ end }}',
+ 1,
+ ],
+ ['the same, with a tab after the braces', '| a | b | c |\n|---|---|---|\n{{\tif .A }}| x | y |\n{{ end }}', 1],
+ ])('%s', (_name, md, rows) => {
+ const editor = load(md);
+ const found = JSON.stringify(editor.getJSON()).split('"type":"tableRow"').length - 1;
+
+ editor.destroy();
+
+ expect(found).toBe(rows);
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it.each([
+ [
+ 'an action whose name begins with a control word',
+ '| a | b |\n|---|---|\n{{ endpoint }} | 2\n{{ iffy .X }} | 3',
+ 3,
+ ],
+ ['a row whose last cell is an action', '| a | b |\n|---|---|\n| 1 | {{ .B }}\n| 3 | 4 |', 3],
+ ['a row with a cell too few', '| a | b |\n|---|---|\n| 1 |\n| 3 | 4 |', 3],
+ ])('keeps %s a row', (_name, md, rows) => {
+ const editor = load(md);
+ const found = JSON.stringify(editor.getJSON()).split('"type":"tableRow"').length - 1;
+
+ editor.destroy();
+
+ expect(found).toBe(rows);
+ });
+
+ it('keeps the cells of a table in a task item of a mixed list, which is lexed a second time', () => {
+ const saved = roundTrip('- normal\n- [ ] task\n | a | b |\n |---|---|\n | `x | y` | {{ .A | print }} |');
+
+ expect(saved).toContain('| `x \\| y` | {{ .A | print }} |');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+});
+
+describe('the asterisks of an anonymizer placeholder are not emphasis', () => {
+ it.each([
+ ['emphasis around it', '*a §*x*§ b*', ['a §*x*§ b']],
+ ['a backslash before it', '*a \\§*x*§ b*', ['a \\§*x*§ b']],
+ ['more asterisks than the emphasis has', '*a §**ipv4***§ b*', ['a §**ipv4***§ b']],
+ ['emphasis right after it', 'text §*a*§*b*', ['b']],
+ ['a backslash before uneven asterisks', '*a \\§**ipv4***§ b*', ['a \\§**ipv4***§ b']],
+ ['a closer left over from a second pass', 'at §*HTTP URL*§ Name Enhanced*§.net', []],
+ ['none around it', 'see §*Name*§ now', []],
+ ])('with %s', (_name, md, italics) => {
+ const editor = load(md);
+ const nodes: JSONContent[] = editor.getJSON().content?.[0]?.content ?? [];
+ const marked = nodes
+ .filter((node) => node.marks?.some((mark) => mark.type === 'italic'))
+ .map((node) => node.text);
+
+ editor.destroy();
+
+ expect(marked).toEqual(italics);
+ });
+});
+
+describe('a code span whose later line could be read as a block is written on one line', () => {
+ it.each([
+ ['a fence of three backticks', 'x ```a`b``c\nd``` tail\n\nnext', 'x ```a`b``c d``` tail\n\nnext'],
+ ['a bullet in an item', '- a `foo\n - bar` b', '- a `foo - bar` b'],
+ ['a numbered line', 'a `foo\n2. bar` b', 'a `foo 2. bar` b'],
+ ['a line indented like code', 'a `foo\n bar` b', 'a `foo bar` b'],
+ ])('%s', (_name, md, saved) => {
+ expect(roundTrip(md)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+});
+
+describe('a line break is not a character of the text', () => {
+ it('is a break node, in emphasis too, so reading the paragraph back from the DOM keeps it', () => {
+ const editor = load('one\n*two\nthree* `four\nfive`');
+ const html = editor.getHTML();
+ const texts = JSON.stringify(editor.getJSON()).match(/"text":"[^"]*"/g);
+
+ editor.destroy();
+
+ expect(texts).toEqual([
+ '"text":"one"',
+ '"text":"two"',
+ '"text":"three"',
+ '"text":" "',
+ '"text":"four"',
+ '"text":"five"',
+ ]);
+ expect(html).toBe(
+ '<p>one<br data-break=""><em>two<br data-break="">three</em> <code>four<br data-break="">five</code></p>',
+ );
+ });
+});
+
+describe('a line break is written only between two lines of text', () => {
+ const paragraph = (...content: JSONContent[]): JSONContent => ({
+ content: [{ content, type: 'paragraph' }],
+ type: 'doc',
+ });
+ const text = (value: string): JSONContent => ({ text: value, type: 'text' });
+ const soft: JSONContent = { attrs: { marker: '' }, type: 'hardBreak' };
+ const hard: JSONContent = { type: 'hardBreak' };
+
+ it.each([
+ [
+ 'Enter at the end of a line',
+ 'line one\nline two\nline three',
+ (editor: Editor) => editor.chain().setTextSelection(9).splitBlock().run(),
+ 'line one\n\nline two\nline three',
+ ],
+ [
+ 'Enter at the start of a line',
+ 'line one\nline two\nline three',
+ (editor: Editor) => editor.chain().setTextSelection(10).splitBlock().run(),
+ 'line one\n\nline two\nline three',
+ ],
+ [
+ 'Enter at the end of a line of an item',
+ '- item one\n item two\n- next',
+ (editor: Editor) => editor.chain().setTextSelection(11).splitListItem('listItem').run(),
+ '- item one\n- item two\n- next',
+ ],
+ ])('%s splits the text there and leaves no empty line', (_name, md, edit, saved) => {
+ expect(afterEdit(md, edit)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it.each([
+ ['two breaks in a row', paragraph(text('a'), soft, soft, text('b')), 'a\nb'],
+ ['a break under a hard break', paragraph(text('a'), hard, soft, text('b')), 'a \nb'],
+ ['a break above a hard break', paragraph(text('a'), soft, hard, text('b')), 'a \nb'],
+ ['a break at the start', paragraph(soft, text('a')), 'a'],
+ ['a break at the end', paragraph(text('a'), soft), 'a'],
+ ['nothing but a break', paragraph(soft), ''],
+ ])('%s is one line break at most', (_name, content, saved) => {
+ expect(afterEdit(content)).toBe(saved);
+ });
+
+ it('is one break where the text of a middle line is deleted', () => {
+ expect(
+ afterEdit('line one\nline two\nline three', (editor) => editor.commands.deleteRange({ from: 10, to: 18 })),
+ ).toBe('line one\nline three');
+ });
+
+ // marked merges a definition or an indented line into the text above it and leaves a line break behind.
+ it.each([
+ [
+ 'a definition above a row of dashes',
+ 'para\n[r]: u\n|---|---|',
+ 'para\n[r]: u\n\n|---|---|',
+ ['para', '[r]: u', '|---|---|'],
+ ],
+ ['a definition between quoted lines', '> q\n[r]: u\n> q', '> q\n> [r]: u\n> q', ['q', '[r]: u', 'q']],
+ [
+ 'an indented line above a rule',
+ 'Some text\n indented\n---',
+ 'Some text\nindented\n\n---',
+ ['Some text', 'indented'],
+ ],
+ [
+ 'a tab-indented line in an indented item',
+ ' - item\n\tmore text',
+ '- item\n more text',
+ ['item', 'more text'],
+ ],
+ [
+ 'a definition whose target is the row under it',
+ 'Columns\n[x]:\n|:--|--:|',
+ 'Columns\n[x]:\n|:--|--:|',
+ ['Columns', '[x]:', '|:--|--:|'],
+ ],
+ ])('%s is read without an empty line', (_name, md, saved, texts) => {
+ expect(linesOf(md).join('|/|')).toBe(texts.join('|/|'));
+ expect(roundTrip(md)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+});
+
+describe('a reference definition stays the text it was written as', () => {
+ it.each([
+ ['a references section', '## References\n\n[1]: https://example.com/advisory\n[2]: https://example.com/poc'],
+ ['a cited reference', 'See [the advisory][1] for details.\n\n[1]: https://example.com/advisory'],
+ ['a footnote-style definition', 'Text with a note[^1].\n\n[^1]: https://example.com/src'],
+ ['a definition with a title', '[spec]: https://example.com/spec "The spec"\n\ntext'],
+ ['a key and its value', '[target]: 10.0.0.5'],
+ ['a definition as an item', '- [docs]: https://example.com/docs\n- next'],
+ [
+ 'a second run of definitions',
+ '[1]: https://a.b/one\n\ntext between\n\n[2]: https://a.b/two\n[3]: https://a.b/c',
+ ],
+ ['definitions right under a heading', '## References\n[1]: https://a.b/c\n[2]: https://a.b/d'],
+ ['definitions a blank line apart', '[1]: https://a.b/one\n\n[2]: https://a.b/two'],
+ ])('%s', (_name, md) => {
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ // marked merges the lines under a paragraph into it, and a run of definitions is not one it queued.
+ it.each([
+ ['a quoted definition above quoted text', '> [1]: http://x.test\n> text', ['[1]: http://x.test', 'text']],
+ ['a definition above an indented line', '[1]: http://x.test\n indented', ['[1]: http://x.test', 'indented']],
+ [
+ 'a definition between two paragraphs',
+ 'Intro paragraph.\n\n[1]: http://x.test\n\n indented',
+ ['Intro paragraph.', '[1]: http://x.test', 'indented'],
+ ],
+ ])('%s loads with every text in place', (_name, md, texts) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const loaded = JSON.stringify(editor.getJSON()).match(/"text":"[^"]*"/g);
+
+ editor.destroy();
+
+ expect(loaded).toEqual(texts.map((text) => `"text":"${text}"`));
+ });
+
+ it.each([
+ ['a row of dashes', '[Summary]:\n---\n\ntext', '[Summary]:\n\\---\n\ntext', '---'],
+ ['a redirect', '[x]:\n>out.txt', '[x]:\n\\>out.txt', '>out.txt'],
+ ])(
+ 'reads a target on its own line that is %s without the backslash it is written with',
+ (_name, md, saved, target) => {
+ expect(roundTrip(md)).toBe(saved);
+ expect(roundTrip(saved)).toBe(saved);
+ expect(textsOf(saved)).toContain(`"text":"${target}"`);
+ },
+ );
+
+ it('keeps the lines of a run apart as break nodes, like the lines of any paragraph', () => {
+ const editor = load('[1]: https://a.b/one\n[2]: https://a.b/two\n[3]: https://a.b/three');
+ const [run] = editor.getJSON().content ?? [];
+
+ editor.destroy();
+
+ expect(run?.content?.map((node: JSONContent) => node.text ?? node.attrs?.marker)).toEqual([
+ '[1]: https://a.b/one',
+ '',
+ '[2]: https://a.b/two',
+ '',
+ '[3]: https://a.b/three',
+ ]);
+ });
+});
+
+describe('the cost of a paragraph grows with its text, not with the square of it', () => {
+ const lines = (count: number, line: (at: number) => string): string =>
+ Array.from({ length: count }, (_, at) => line(at)).join('\n');
+
+ it.each<[string, 'read' | 'saved', (size: number) => string, number]>([
+ ['lines that end in a hard break', 'saved', (size) => lines(size, (at) => `x${at} `), 8000],
+ ['emphasis after emphasis', 'saved', (size) => lines(size, () => '*em* **strong**'), 4000],
+ ['lines of text', 'read', (size) => lines(size, (at) => `line ${at} of output with a *word*`), 8000],
+ [
+ 'lines without a placeholder',
+ 'read',
+ (size) => lines(size, (at) => `line ${at} open port on host `),
+ 32_000,
+ ],
+ ['an action on every line', 'read', (size) => lines(size, (at) => `Host {{ .Host${at} }} is up `), 32_000],
+ [
+ 'a placeholder and emphasis on every line',
+ 'read',
+ (size) => lines(size, (at) => `Host §*Host${at}*§ is *up*`),
+ 8000,
+ ],
+ [
+ 'one placeholder and an action on every word',
+ 'read',
+ (size) => `§*Secret*§ ${lines(size, (at) => `w {{ .F${at} }}`)}`,
+ 64_000,
+ ],
+ [
+ 'a heading over text, block after block',
+ 'read',
+ (size) => lines(size, (at) => `## Step ${at}\nRun the scan against host ${at}.`),
+ 4000,
+ ],
+ ['setext headings one after another', 'read', (size) => lines(size, (at) => `Title ${at}\n---`), 4000],
+ [
+ 'headings underlined with equals signs, one after another',
+ 'read',
+ (size) => lines(size, (at) => `Title ${at}\n===`),
+ 4000,
+ ],
+ [
+ 'tables with a blank line between them',
+ 'read',
+ (size) => lines(size, () => '| a | b |\n|---|---|\n| 1 | 2 |\n'),
+ 4000,
+ ],
+ ])('%s, %s', (_name, side, build, size) => {
+ const { destroy, parse, serialize } = markdownCodec();
+ const growth =
+ side === 'read'
+ ? slowdownWhenInputQuadruples(build, parse, size)
+ : slowdownWhenInputQuadruples((length) => parse(build(length)), serialize, size);
+
+ destroy();
+
+ expect(growth).toBeLessThan(GROWTH_IF_QUADRATIC / 2);
+ });
+
+ it('reads a paragraph of sixty thousand lines', () => {
+ const { destroy, parse } = markdownCodec();
+ const [paragraph] = parse('a\n'.repeat(60_000)).content ?? [];
+
+ destroy();
+
+ expect(paragraph?.content).toHaveLength(119_999);
+ });
+});
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-marked.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-marked.ts
index f8f6a92b..4b7c55a0 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-marked.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-marked.ts
@@ -1,11 +1,27 @@
-import type { JSONContent, MarkdownRendererHelpers } from '@tiptap/core';
+import type { AnyExtension, JSONContent, MarkdownParseHelpers, MarkdownRendererHelpers } from '@tiptap/core';
import { Extension } from '@tiptap/core';
import { renderTableToMarkdown, Table } from '@tiptap/extension-table';
import { Markdown } from '@tiptap/markdown';
-import { Marked } from 'marked';
+import { Marked, type Token } from 'marked';
-import { escapeTablePipes, TEMPLATE_ACTION } from './markdown-editor-table-pipes';
+import {
+ annotateLayout,
+ lineBreaksAtEnd,
+ renderCompactTable,
+ withLayoutFacts,
+ withWrittenRows,
+} from './markdown-editor-layout';
+import {
+ countRowCells,
+ escapeTablePipes,
+ findCodeSpanCloser,
+ isNotARow,
+ isTableBodyEnd,
+ TABLE_DELIMITER_LINE,
+ TABLE_PIPE,
+ TEMPLATE_ACTION,
+} from './markdown-editor-table-pipes';
// @tiptap/markdown parses with `marked`, which — unlike markdown-it's `html: false` — always tries to
// interpret `<...>` as HTML. Our content uses literal XML-ish tags (`<container_environment>`, `<input>`)
@@ -14,6 +30,32 @@ import { escapeTablePipes, TEMPLATE_ACTION } from './markdown-editor-table-pipes
// every `<...>` fall through to plain text, recreating markdown-it's `html: false`.
const createTunedMarked = () => {
const instance = new Marked();
+ const BaseTokenizer = instance.Tokenizer;
+ const cellTokens = new WeakSet<object>();
+ let isInsideTable = false;
+ let definitionRuns = 0;
+ // marked asks where the next literal span starts before every piece of text, with all that is left of the
+ // paragraph. Each answer is kept as a distance from the end until the text has passed it — and for good
+ // once there is none — or a paragraph with one placeholder and a thousand actions is searched to its end
+ // a thousand times.
+ let spansAhead: Record<string, null | number> = {};
+
+ const nextSpan = (src: string, opener: string): number => {
+ const known = spansAhead[opener];
+
+ if (typeof known === 'number' && (known < 0 || src.length >= known)) {
+ return known < 0 ? -1 : src.length - known;
+ }
+
+ const at = src.indexOf(opener);
+
+ spansAhead[opener] = at < 0 ? -1 : src.length - at;
+
+ return at;
+ };
+
+ let inlineDepth = 0;
+ let inlineSource: null | string = null;
instance.use({
tokenizer: {
@@ -21,13 +63,26 @@ const createTunedMarked = () => {
// prose on round-trip. Returning `undefined` forces the char to stay literal text; `false` defers
// to marked's default. Neutralise the lossy cases, keep what the toolbar emits:
// • del — keep GFM `~~strike~~`, drop a lone `~…~` (else `~5~` / ranges become <del>)
- // • emStrong — keep `*`/`**`, drop `_`-delimited emphasis (else `__init__`/`_word_` become em/strong)
+ // • emStrong — keep `*`/`**`, drop `_`-delimited emphasis (else `__init__`/`_word_` become em/strong),
+ // and emphasis nested in its own kind: a text node holds a mark once, so
+ // `**No /dev/sd*, /dev/vd*, /dev/fuse**` — italic inside italic to CommonMark — is
+ // saved one pair of asterisks short. Left literal, its opening asterisk is text
+ // and what follows pairs up into marks the document can hold.
// • escape — keep `\`+punct literal (`\.` `\*` `\|` `\\`); marked's default DROPS the backslash
// (CommonMark unescape), silently corrupting regex/paths on the first load. The
// `\#`/`\>` counterpart to the paragraph serializer is NOT here: an inline tokenizer
// fires at any position, so it also ate the backslash mid-line (`grep '\<root\>'`).
// It lives in the Lexer's inlineTokens override below, which sees line starts.
// • html/tag — keep `<xml-like>` tags literal (marked swallows real-HTML-element names)
+ // • def — keep `[label]: target` lines as the text they are. marked lifts a definition out
+ // of the document into a link table and nothing writes it back, so a References
+ // section loses its lines and `[text][1]` is rewritten as an inline link. The
+ // run of definitions is one paragraph of literal text (LinkDefinitions below):
+ // tokenized inline, its target would be a link of its own.
+ // • autolink — `<scheme:…>` is a link for any scheme, so a Flask route `<int:order_id>` is one,
+ // shown and saved without its brackets. Only a web address or an e-mail is a
+ // link; anything else is the text it was written as, brackets and all — taken
+ // whole, or the bare-URL rule would find a link inside `<git+https://…>`.
// NB: autolink/url are intentionally NOT neutralised — a bare `https://…`, `<url>` or email is
// meant to become a link (see markdown-editor-extensions.ts link config, kept symmetric with typing).
// NB: named HTML entities (`&lt; &gt; &amp; &quot;`) are decoded downstream — @tiptap/markdown's
@@ -35,11 +90,121 @@ const createTunedMarked = () => {
// HTML-encoding artifacts from ingestion). Numeric refs (`&#123;`) and anything inside code are
// untouched; a bare `&` survives as `&`. Don't re-add a literalAmpersand token to "preserve"
// `&lt;` — that re-freezes the artifacts.
+ autolink(src: string) {
+ const link = BaseTokenizer.prototype.autolink.call(this, src);
+ const isAddress = link && /^(?:https?:|ftp:|mailto:)/i.test(link.href);
+
+ return link && !isAddress ? ({ raw: link.raw, text: link.raw, type: 'escape' } as never) : link;
+ },
+ codespan(src: string) {
+ const span = BaseTokenizer.prototype.codespan.call(this, src);
+
+ return span && { ...span, text: readCodeSpan(span.raw) };
+ },
+ def(src: string) {
+ const definition = this.rules.block.def;
+ let raw = '';
+
+ // The rule takes the blank lines under a definition with it; the run ends at the first.
+ for (let line = definition.exec(src); line; line = definition.exec(src.slice(raw.length))) {
+ raw += line[0];
+
+ if (line[0].endsWith('\n\n')) {
+ break;
+ }
+ }
+
+ // Not a `paragraph`: marked appends the lines under one to it and edits the inline source it
+ // queued for it, and this run queues none. And a tag of its own: marked keeps a definition
+ // only the first time it sees its tag.
+ return raw
+ ? ({ raw, tag: `\0${definitionRuns++}`, text: raw.trimEnd(), type: 'linkDefinitions' } as never)
+ : undefined;
+ },
del: (src: string) => (/^~~(?!~)/.test(src) ? false : undefined),
- emStrong: (src: string) => (/^_/.test(src) ? undefined : false),
+ emStrong(src: string, maskedSrc: string, prevChar?: string) {
+ if (src.startsWith('_')) {
+ return undefined;
+ }
+
+ const emphasis = BaseTokenizer.prototype.emStrong.call(this, src, maskedSrc, prevChar);
+
+ return emphasis && nestsItsOwnKind(emphasis) ? undefined : emphasis;
+ },
escape: () => undefined,
html: () => undefined,
+ // marked hands each cell's text to `lexer.inline` from inside this tokenizer — the one place a
+ // cell can be told from a paragraph line (see the Lexer's `inline` below).
+ table(src: string) {
+ isInsideTable = true;
+
+ try {
+ return BaseTokenizer.prototype.table.call(this, withoutTemplateRows(src));
+ } finally {
+ isInsideTable = false;
+ }
+ },
tag: () => undefined,
+ // A URL that runs into a Go action — `https://host/{{ .Path }}` — would be a link that ends inside
+ // the action: edited, it is written as `[…{{](…{{) .Path }}`, which no longer parses.
+ url(src: string) {
+ const link = BaseTokenizer.prototype.url.call(this, src);
+
+ return link?.raw.includes('{{') ? undefined : link;
+ },
+ },
+ });
+
+ // Two things are literal text whatever markdown would read in them. The anonymizer replaces a secret with
+ // `§*Name*§`, and a second pass over its own output yields `§***Name****§`: its asterisks are not
+ // emphasis, and read as emphasis they pair with asterisks outside the placeholder and come back fewer. A
+ // Go template action is the template's: a raw string between backticks is not a code span, a URL in a
+ // string is not a link, and `&lt;` in one is not `<` — decoded, it is another string, and `&quot;` a
+ // parse error. Each is taken whole here — as an `escape` token, the one kind @tiptap/markdown copies
+ // without decoding entities — and masked from the scan that looks for the other end of an emphasis run.
+ // That scan measures positions by length, so the mask is as long as what it hides; and marked has already
+ // blotted out `\§` in the string it hands the hook, so the spans are found in the source the Lexer saw.
+ instance.use({
+ extensions: [
+ {
+ level: 'inline',
+ name: 'literalSpan',
+ start(src: string) {
+ const placeholder = nextSpan(src, '§*');
+ const action = nextSpan(src, '{{');
+
+ return placeholder < 0 || action < 0
+ ? Math.max(placeholder, action)
+ : Math.min(placeholder, action);
+ },
+ tokenizer(src: string) {
+ const [raw] = LITERAL_SPAN_AT_START.exec(src) ?? [];
+
+ return raw ? { raw, text: raw, type: 'escape' } : undefined;
+ },
+ },
+ ],
+ hooks: {
+ emStrongMask(masked: string) {
+ const source = inlineSource?.length === masked.length ? inlineSource : masked;
+ const hidden: string[] = [];
+ let at = 0;
+
+ inlineSource = null;
+
+ for (const { 0: span, index } of source.matchAll(LITERAL_SPAN)) {
+ // What marked has blotted out already — `{{` inside a code span — opens no span. A `+` is
+ // its mark for an escaped character, which the span still begins with.
+ if (masked[index] !== span[0] && masked[index] !== '+') {
+ continue;
+ }
+
+ hidden.push(masked.slice(at, index), span[0]!, 'a'.repeat(span.length - 2), span.at(-1)!);
+ at = index + span.length;
+ }
+
+ return at ? hidden.join('') + masked.slice(at) : masked;
+ },
},
});
@@ -52,52 +217,346 @@ const createTunedMarked = () => {
const BaseLexer = instance.Lexer;
instance.Lexer = class extends BaseLexer {
+ // Set for the content of a task item, which @tiptap/markdown lexes a second time and renders itself:
+ // it gets the pipe protection and none of the layout its renderer would not honour.
+ isNested = false;
+
+ override inline(src: string, tokens: Token[] = []) {
+ if (isInsideTable) {
+ cellTokens.add(tokens);
+ }
+
+ return super.inline(src, tokens);
+ }
+
// Undo the paragraph serializer's line-leading `\#`/`\>` here rather than in an inline tokenizer or in
// `lex`. An inline tokenizer has no notion of position and ate the backslash mid-line; `lex` runs
// BEFORE block tokenization, so unescaping there hands marked a live `#`/`>` and the paragraph the
// escape exists to protect becomes a heading or a quote again.
- override inlineTokens(src: string, tokens = []) {
- const unescaped = src
- .replace(/(^|\n)\\([#>])/g, '$1$2')
- // Only a backslash before a run that FILLS the line, matching what the serializer escapes —
- // an author's `\-` inside `[a-z\-_]` keeps its backslash.
- .replace(/(\n {0,3})\\((?:=+|-+) *)(?=\n|$)/g, '$1$2');
+ // A table cell is left alone: it cannot open a heading or a quote, cellText never escapes one, and a
+ // backslash found there is the author's — `\>` in a column of regex tokens. So is the content of
+ // emphasis, a link or a strike-through, which marked lexes with this method as well: its first
+ // character is not the start of a line, and the paragraph's own call has already seen every line of
+ // it.
+ override inlineTokens(src: string, tokens: Token[] = []) {
+ const isParagraph = !inlineDepth && !cellTokens.has(tokens);
+ const source = isParagraph ? withoutLineEscapes(src) : src;
+
+ const outer = spansAhead;
+
+ spansAhead = {};
+ inlineSource = source;
+ inlineDepth += 1;
+
+ try {
+ super.inlineTokens(source, tokens);
+ } finally {
+ inlineDepth -= 1;
+ spansAhead = outer;
+ }
+
+ // marked fills the array it was handed, so the line breaks are split out in place — one by one: a
+ // paragraph of fifty thousand lines is more arguments than a call takes. A literal span and the
+ // plain text beside it go back as one token: @tiptap/markdown merges the text nodes it makes of
+ // neighbours by splicing one out of the array at a time, the square of a paragraph of actions.
+ const split = withLineBreaks(tokens);
+
+ tokens.length = 0;
+
+ for (const token of split) {
+ const last = tokens.at(-1);
+
+ if (
+ last &&
+ isLiteral(last) &&
+ isLiteral(token) &&
+ (last.type === 'escape' || token.type === 'escape')
+ ) {
+ const raw = last.raw + token.raw;
+
+ tokens[tokens.length - 1] = { raw, text: raw, type: 'escape' } as Token;
+ } else {
+ tokens.push(token);
+ }
+ }
- return super.inlineTokens(unescaped, tokens);
+ return tokens;
}
override lex(src: string) {
- return super.lex(escapeTablePipes(src));
+ // A document that holds the stand-in itself gets the backslash, which marked honours as well.
+ const standIn = src.includes(TABLE_PIPE) ? undefined : TABLE_PIPE;
+ const escaped = escapeTablePipes(src, standIn);
+ const shielded = shieldFenceTabs(escaped);
+ const tokens = super.lex(shielded);
+
+ if (shielded !== escaped) {
+ restoreShielded(tokens, FENCE_TAB, '\t');
+ }
+
+ if (!this.isNested) {
+ this.restoreItemLines(tokens);
+ annotateLayout(tokens, src);
+ }
+
+ // After the layout is read: the style of a table is picked from rows in which a pipe of a code span
+ // or of an action is still a stand-in, and no separator.
+ if (standIn && escaped !== src) {
+ restoreShielded(tokens, standIn, '|');
+ }
+
+ return tokens;
+ }
+
+ // Inside a list item marked reads a line indented four past the content column as indented code and
+ // then merges it into the text above: without its indentation, and with the code's own line break,
+ // so the paragraph gains an empty line and is saved as two. The lines as they were written are still
+ // in `raw` — except a line that opens with a tab, which marked reads as four spaces the next time
+ // and so would not come back the same.
+ restoreItemLines(tokens: Token[], isInItem = false) {
+ for (const token of tokens as (Token & { items?: Token[]; text?: string; tokens?: Token[] })[]) {
+ const isText = isInItem && /^(?:text|paragraph)$/.test(token.type);
+ const written = isText ? token.raw.slice(0, token.raw.length - lineBreaksAtEnd(token.raw).length) : '';
+
+ if (isText && token.text !== written && !written.includes('\n\t')) {
+ token.text = written;
+ token.tokens = this.inlineTokens(written);
+ } else if (token.type === 'list' || token.type === 'blockquote') {
+ this.restoreItemLines(token.items ?? token.tokens ?? []);
+ } else if (token.type === 'list_item') {
+ this.restoreItemLines(token.tokens ?? [], true);
+ }
+ }
}
} as typeof BaseLexer;
+ // @tiptap/markdown lexes the content of a task item again through `instance.lexer`, which is the base
+ // class's: without the pre-pass a pipe in a code span of a table nested there splits its cell.
+ instance.lexer = ((src: string, options?: typeof instance.defaults) => {
+ const lexer = new instance.Lexer(options ?? instance.defaults) as InstanceType<typeof instance.Lexer> & {
+ isNested: boolean;
+ };
+
+ lexer.isNested = true;
+
+ return lexer.lex(src);
+ }) as typeof instance.lexer;
+
// A PRIVATE instance — mutating the shared global `marked` would also affect report-pdf.
return instance;
};
+// What the paragraph serializer puts a backslash before, without it: `#` and `>` at the start of a line, and a
+// run of `=` or `-` that FILLS a line under another — an author's `\-` inside `[a-z\-_]` keeps its backslash.
+const withoutLineEscapes = (text: string): string =>
+ text.replace(/(^|\n)( {0,3})\\([#>])/g, '$1$2$3').replace(/(\n {0,3})\\((?:=+|-+) *)(?=\n|$)/g, '$1$2');
+
+// An anonymizer placeholder, or a Go template action.
+const LITERAL_SPAN = new RegExp(`§\\*[^§\\n]*\\*§|${TEMPLATE_ACTION.source}`, 'g');
+const LITERAL_SPAN_AT_START = new RegExp(`^(?:${LITERAL_SPAN.source})`);
+
+// What @tiptap/markdown copies into a text node as it is: an `escape` token, and text that holds no entity.
+const isLiteral = (token: Token): boolean =>
+ token.type === 'escape' ||
+ (token.type === 'text' &&
+ !('tokens' in token && token.tokens) &&
+ token.text === token.raw &&
+ !token.raw.includes('&'));
+
+// The rows of a table, up to the first line that is not one (isNotARow). What is tested first is the delimiter
+// row: without one marked reads no table, and the lines below are left unread — this runs at the start of
+// every block, and reading on to the next blank line each time is quadratic.
+const withoutTemplateRows = (src: string): string => {
+ const header = src.indexOf('\n');
+ const delimiter = src.indexOf('\n', header + 1);
+
+ const row = src.slice(header + 1, delimiter);
+
+ if (header < 0 || delimiter < 0 || !TABLE_DELIMITER_LINE.test(row)) {
+ return src;
+ }
+
+ const columns = countRowCells(row);
+
+ for (let at = delimiter + 1; at < src.length; ) {
+ const end = src.indexOf('\n', at);
+ const line = src.slice(at, end < 0 ? src.length : end);
+
+ if (isTableBodyEnd(line)) {
+ return src;
+ }
+
+ if (isNotARow(line, columns)) {
+ return src.slice(0, at);
+ }
+
+ at = end < 0 ? src.length : end + 1;
+ }
+
+ return src;
+};
+
+// A line break inside a paragraph is a character of its text to marked. Left in a text node it does not
+// survive an edit: ProseMirror reads the paragraph back from the DOM on every keystroke and turns each line
+// break it finds in text into a hard break, so typing one letter gives every line of the paragraph two
+// trailing spaces. As a token of its own it becomes a break node that remembers it was written as nothing.
+// A paragraph has no empty line and does not end in a break; marked's merge of a definition or an indented
+// line into the text above leaves both behind, and they are dropped.
+const isLineBreak = (token?: Token): boolean => token?.type === 'br' && token.raw === '\n';
+
+const withLineBreaks = (tokens: Token[]): Token[] => {
+ const split: Token[] = [];
+
+ for (const token of tokens) {
+ const isText = token.type === 'text' || (token.type === 'escape' && token.raw.startsWith('{{'));
+
+ if (!isText || ('tokens' in token && token.tokens) || !token.raw.includes('\n')) {
+ split.push(token);
+ continue;
+ }
+
+ token.raw.split('\n').forEach((line, at) => {
+ if (at && !isLineBreak(split.at(-1))) {
+ split.push({ raw: '\n', type: 'br' } as Token);
+ }
+
+ if (line) {
+ split.push({ ...token, raw: line, text: line } as Token);
+ }
+ });
+ }
+
+ while (isLineBreak(split.at(-1))) {
+ split.pop();
+ }
+
+ return split;
+};
+
+const nestsItsOwnKind = (emphasis: Token): boolean => {
+ const holds = (tokens: Token[] = []): boolean =>
+ tokens.some((token) => token.type === emphasis.type || holds((token as { tokens?: Token[] }).tokens));
+
+ return holds((emphasis as { tokens?: Token[] }).tokens);
+};
+
+// marked's list rule replaces every tab on an item's continuation lines with four spaces before it dedents
+// them, fenced code included, so a Makefile recipe or gofmt-indented Go under a list item loses its tabs. A
+// tab past the fence's own indentation carries no structure, so it crosses the lexer as a private-use
+// character and comes back in the tokens. A fence that never closes is left alone: marked ends it with the
+// item, and tabs shielded past that point would stop counting as indentation.
+const FENCE_TAB = '\uE000';
+const FENCE_RUN = /^([ \t>]*)(`{3,}(?=[^`\n]*$)|~{3,})/;
+
+const shieldFenceTabs = (markdown: string): string => {
+ if (!markdown.includes('\t') || markdown.includes(FENCE_TAB)) {
+ return markdown;
+ }
+
+ const lines = markdown.split('\n');
+ let open: null | { at: number; indent: number; run: string } = null;
+
+ lines.forEach((line, index) => {
+ const fence = FENCE_RUN.exec(line);
+
+ if (!open) {
+ open = fence ? { at: index, indent: fence[1]!.length, run: fence[2]! } : null;
+
+ return;
+ }
+
+ const closes =
+ fence &&
+ fence[2]![0] === open.run[0] &&
+ fence[2]!.length >= open.run.length &&
+ !line.slice(fence[0].length).trim();
+
+ if (!closes) {
+ return;
+ }
+
+ for (let body = open.at + 1; body < index; body++) {
+ const text = lines[body]!;
+ const structural = Math.min(open.indent, /^[ \t>]*/.exec(text)![0].length);
+
+ lines[body] = text.slice(0, structural) + text.slice(structural).replaceAll('\t', FENCE_TAB);
+ }
+
+ open = null;
+ });
+
+ return lines.join('\n');
+};
+
+const restoreShielded = (value: unknown, shield: string, written: string): void => {
+ if (!value || typeof value !== 'object') {
+ return;
+ }
+
+ const fields = value as Record<string, unknown>;
+
+ for (const key of Object.keys(fields)) {
+ const field = fields[key];
+
+ if (typeof field === 'string') {
+ fields[key] = field.replaceAll(shield, written);
+ } else {
+ restoreShielded(field, shield, written);
+ }
+ }
+};
+
type ManagerWithEncode = { encodeTextForMarkdown: (text: string, node?: SerializedNode, parent?: unknown) => string };
-type SerializedNode = { marks?: (string | { type?: string })[]; text?: string };
-
-const hasCodeMark = (node?: SerializedNode): boolean =>
- (node?.marks ?? []).some((mark) => (typeof mark === 'string' ? mark : mark.type) === 'code');
-
-// Serialize a literal string as a CommonMark code span (spec 0.31.2 §6.1). @tiptap/markdown's mark path
-// can't do this: it derives the code fence by rendering the mark against a placeholder, so the fence is a
-// fixed single backtick blind to the content — a span whose text holds a backtick (`` `x` ``, common in
-// pentest write-ups) serialises to invalid `` ``x`` `` and degrades further on every save. Choose a fence
-// one longer than the longest backtick run inside, and pad with a space when the content starts or ends
-// with a backtick (or is all-but-not-only spaces) so the reader strips exactly that pad back off. Mirrors
-// prosemirror-markdown's backticksFor; the code-mark renderMarkdown override in markdown-editor-extensions.ts
-// zeroes the placeholder fence so this is the ONLY fence emitted.
-export const serializeCodeSpan = (content: string): string => {
- const longestRun = (content.match(/`+/g) ?? []).reduce((max, run) => Math.max(max, run.length), 0);
- const fence = '`'.repeat(longestRun + 1);
- const needsPad =
- content.startsWith('`') ||
- content.endsWith('`') ||
- (content.startsWith(' ') && content.endsWith(' ') && /\S/.test(content));
-
- return `${fence}${needsPad ? ` ${content} ` : content}${fence}`;
+type SerializedNode = { marks?: (string | { attrs?: Record<string, unknown>; type?: string })[]; text?: string };
+
+const codeMarkOf = (node?: SerializedNode) =>
+ (node?.marks ?? []).find((mark) => (typeof mark === 'string' ? mark : mark.type) === 'code');
+
+// A code span is written between fences one backtick longer than the longest run inside it, or between the
+// ones it was written with (`written`) while no run inside is exactly that long — one backtick around
+// `` a``b `` closes only at one backtick. @tiptap/markdown's mark path cannot do either: it derives the fence
+// by rendering the mark against a placeholder, so a span that holds a backtick (`` `x` ``, common in pentest
+// write-ups) serialises to invalid `` ``x`` `` and degrades on every save. The code-mark renderMarkdown
+// override in markdown-editor-extensions.ts zeroes the placeholder fence so this is the ONLY fence emitted.
+// A span whose content begins or ends with a backtick needs a space between it and the fence. That space is
+// the only padding the load side takes back off (readCodeSpan), so the spaces of `` ` a ` `` are content and
+// are written as they stand.
+const touchesBacktick = (content: string): boolean => /^`|`$/.test(content.trim());
+
+// A span may run over a line, and is written that way while its later lines cannot be read as anything else:
+// a fence of three backticks at a line's start opens a code block, and a line that begins like a list item, a
+// heading, a quote, a table row or a rule — or is indented like code — starts that block inside a list
+// item. Otherwise its line breaks are written as the spaces they read as.
+const CONTINUES_SPAN = /^ {0,3}(?![-+*>#=|~`[]|\d{1,9}[.)]|[_*-](?: *[_*-]){2,} *$)\S/;
+
+const keepsItsLines = (content: string, fence: string): boolean =>
+ fence.length < 3 &&
+ content
+ .split('\n')
+ .slice(1)
+ .every((line, at, lines) => CONTINUES_SPAN.test(line) || (!line && at === lines.length - 1));
+
+export const serializeCodeSpan = (text: string, written?: unknown): string => {
+ const runs = (text.match(/`+/g) ?? []).map((run) => run.length);
+ const own = '`'.repeat(Math.max(0, ...runs) + 1);
+ const length = typeof written === 'number' && Number.isInteger(written) ? written : 0;
+ const kept = length > 0 && length <= 64 && !runs.includes(length) ? '`'.repeat(length) : own;
+ // A fence that cannot hold the span's lines gives way to one that can.
+ const fence = !text.includes('\n') || keepsItsLines(text, kept) ? kept : own;
+ const content = keepsItsLines(text, fence) ? text : text.replaceAll('\n', ' ');
+
+ return `${fence}${touchesBacktick(content) ? ` ${content} ` : content}${fence}`;
+};
+
+// marked strips one space from each end of every padded span, so `` `` alert(`XSS`) `` `` is saved without
+// its spaces, and an agent's unbalanced `` ` capture the ` `` loses both and glues the words around it
+// together; and it turns the span's line breaks into spaces.
+const readCodeSpan = (raw: string): string => {
+ const fence = /^`+/.exec(raw)![0].length;
+ const content = raw.slice(fence, -fence);
+ const isPadded = content.length > 2 && content.startsWith(' ') && content.endsWith(' ');
+
+ return isPadded && touchesBacktick(content) ? content.slice(1, -1) : content;
};
const TunedMarkdownText = Extension.create({
@@ -115,7 +574,11 @@ const TunedMarkdownText = Extension.create({
// its content (see serializeCodeSpan). The code MARK is the discriminator, NOT the manager's broader
// isInsideCode — a code BLOCK's body arrives here with empty marks (parent is codeBlock) and must stay
// verbatim, its fence coming from TunedCodeBlock.
- manager.encodeTextForMarkdown = (text, node) => (hasCodeMark(node) ? serializeCodeSpan(text) : text);
+ manager.encodeTextForMarkdown = (text, node) => {
+ const code = codeMarkOf(node);
+
+ return code ? serializeCodeSpan(text, typeof code === 'string' ? undefined : code.attrs?.fence) : text;
+ };
},
// Lower priority than the Markdown extension so this runs AFTER its onBeforeCreate has created the
// manager and assigned editor.markdown. onBeforeCreate (not onCreate) because it is synchronous —
@@ -123,6 +586,234 @@ const TunedMarkdownText = Extension.create({
priority: 50,
});
+type InlineToken = Token & { href?: string; text?: string; tokens?: InlineToken[] };
+
+let reader: ReturnType<typeof createTunedMarked> | undefined;
+
+const textOf = (tokens: InlineToken[]): string =>
+ tokens
+ .map((token) => (token.tokens ? textOf(token.tokens) : token.type === 'br' ? '\n' : (token.text ?? '')))
+ .join('');
+
+// A setext heading is what its lines happen to read as: one that opens a block, or is an underline itself,
+// ends the heading above it.
+export const readsAsHeading = (markdown: string, level: number): boolean => {
+ const { defaults, Lexer } = (reader ??= createTunedMarked());
+ const [heading, ...rest] = new Lexer(defaults).lex(markdown) as Array<Token & { depth?: number }>;
+
+ return heading?.type === 'heading' && heading.depth === level && !rest.length;
+};
+
+// Whether a line of inline markdown reads as one link to `href`, under `title`, around nothing but one image.
+export const readsAsLinkedImage = (markdown: string, href: string, title: null | string): boolean => {
+ const { defaults, Lexer } = (reader ??= createTunedMarked());
+ const [link] = new Lexer(defaults).inlineTokens(markdown) as (InlineToken & { title?: null | string })[];
+
+ return (
+ link?.type === 'link' &&
+ link.href === href &&
+ (link.title || null) === title &&
+ link.tokens?.length === 1 &&
+ link.tokens[0]!.type === 'image'
+ );
+};
+
+// Every link a line of inline markdown reads as: its target and its text.
+const readLinks = (markdown: string): string => {
+ const { defaults, Lexer } = (reader ??= createTunedMarked());
+ // A link around an image is the image's own (its `link` attribute), not a link of the text.
+ const collect = (tokens: InlineToken[]): string[] =>
+ tokens.flatMap((token) => {
+ if (token.type !== 'link') {
+ return collect(token.tokens ?? []);
+ }
+
+ const text = textOf((token.tokens ?? []).filter((child) => child.type !== 'image'));
+
+ return text ? [`${token.href}\0${text}`] : [];
+ });
+
+ return collect(new Lexer(defaults).inlineTokens(markdown)).join('\0\0');
+};
+
+const linkOf = (node?: JSONContent) =>
+ node?.type === 'text' ? node.marks?.find((mark) => mark.type === 'link') : undefined;
+
+const markTypesOf = (node: JSONContent): string =>
+ (node.marks ?? [])
+ .map((mark) => mark.type)
+ .sort()
+ .join();
+
+const stylesOf = (node?: JSONContent): string =>
+ (node?.marks ?? [])
+ .map((mark) => mark.type)
+ .filter((type) => type !== 'link')
+ .sort()
+ .join();
+
+const isSoftBreak = (node?: JSONContent): boolean => node?.type === 'hardBreak' && node.attrs?.marker === '';
+const marksKey = (node?: JSONContent): string => JSON.stringify(node?.marks ?? []);
+
+const codeOf = (node: JSONContent) => node.marks?.find((mark) => mark.type === 'code');
+
+// Code that an edit has put beside a span written with a fence of its own is one span with it and is fenced
+// once: two fences back to back are read as one longer fence. The fence a span was written with is kept only
+// where every piece of the span carries it.
+const withOneFence = (content: JSONContent[]): JSONContent[] => {
+ const result = [...content];
+
+ for (let from = 0, to = 1; from < result.length; from = to++) {
+ if (!codeOf(result[from]!)) {
+ continue;
+ }
+
+ while (to < result.length && codeOf(result[to]!)) {
+ to++;
+ }
+
+ const fences = new Set(result.slice(from, to).map((node) => codeOf(node)?.attrs?.fence ?? null));
+
+ for (let at = from; fences.size > 1 && at < to; at++) {
+ result[at] = {
+ ...result[at],
+ marks: result[at]!.marks?.map((mark) => (mark.type === 'code' ? { type: 'code' } : mark)),
+ };
+ }
+ }
+
+ return result;
+};
+
+// The line breaks of a paragraph as characters of its text again, which is how they are written. A break
+// keeps the marks it was read under (withMarkedBreaks), and text under the same marks is one node: emphasis
+// that runs over a line is not closed at its end and opened on the next, and a code span is fenced once.
+// A break is written only between two lines of text. Enter at the end of a wrapped line leaves one at the
+// edge of a block, and deleting a line leaves two in a row or one beside a hard break: written, each would
+// be an empty line, which ends the paragraph. Inside a code span a break is content, wherever it stands.
+const withLineBreakText = (nodes: JSONContent[]): JSONContent[] => {
+ const content = withOneFence(nodes);
+ const merged: JSONContent[] = [];
+ let line: JSONContent | undefined;
+ let above: JSONContent | undefined;
+
+ for (const node of content) {
+ if (isSoftBreak(node) && !markTypesOf(node).includes('code')) {
+ line ??= node;
+ continue;
+ }
+
+ const written = isSoftBreak(node) ? { marks: node.marks, text: '\n', type: 'text' } : node;
+ const nodes =
+ line && above && above.type !== 'hardBreak' && node.type !== 'hardBreak'
+ ? [{ text: '\n', type: 'text', ...(line.marks ? { marks: line.marks } : {}) }, written]
+ : [written];
+
+ for (const text of nodes) {
+ const last = merged.at(-1);
+
+ if (text.type === 'text' && last?.type === 'text' && marksKey(last) === marksKey(text)) {
+ merged[merged.length - 1] = { ...last, text: `${last.text}${text.text}` };
+ } else {
+ merged.push(text);
+ }
+ }
+
+ line = undefined;
+ above = node;
+ }
+
+ return merged;
+};
+
+type MarkResult = { attrs?: Record<string, unknown>; content: JSONContent[]; mark: string };
+type ParseMark = (this: unknown, token: unknown, helpers: unknown) => MarkResult | undefined;
+
+// @tiptap/markdown puts a mark on the text nodes it covers and on nothing else, so a break inside `*a\nb*`
+// would be the same node as the one between `*a*` and `*b*`. Each mark is added to the breaks it covers
+// here, which is also where ProseMirror puts it when it reads the paragraph back from the DOM.
+export const withMarkedBreaks = <T extends AnyExtension>(extension: T): T => {
+ const parse = (extension.config as { parseMarkdown?: ParseMark }).parseMarkdown;
+
+ if (extension.type !== 'mark' || !parse) {
+ return extension;
+ }
+
+ return extension.extend({
+ parseMarkdown(token: unknown, helpers: unknown) {
+ const parsed = parse.call(this, token, helpers);
+
+ if (!parsed?.mark) {
+ return parsed;
+ }
+
+ const { attrs, mark: type } = parsed;
+ const mark = { type, ...(attrs && Object.keys(attrs).length ? { attrs } : {}) };
+
+ return {
+ ...parsed,
+ content: parsed.content.map((node) =>
+ isSoftBreak(node) ? { ...node, marks: [...(node.marks ?? []), mark] } : node,
+ ),
+ };
+ },
+ }) as T;
+};
+
+// A URL or an address written bare, or between angle brackets, is a link by itself. Saved as `[url](url)` it
+// is not what was written, and for some URLs not the same link either. But where a bare URL ends is decided
+// by the text around it, and an edit can move that: so the line is written with those links in their own
+// form, and that is kept only if it reads as the very links the document holds. A link whose text is not
+// its target — an entity decoded in it, a word typed into it — is not one of them, and does not cost the
+// other links of the line their form.
+export const renderInline = (nodes: JSONContent[], helpers: MarkdownRendererHelpers): string => {
+ const content = withLineBreakText(nodes);
+ const links: string[] = [];
+ const written: JSONContent[] = [];
+ let hasOwnForm = false;
+
+ for (let at = 0, end = 1; at < content.length; at = end++) {
+ const link = linkOf(content[at]);
+
+ while (link && linkOf(content[end])?.attrs?.href === link.attrs?.href) {
+ end++;
+ }
+
+ const run = content.slice(at, end);
+ const text = run.map((node) => node.text ?? '').join('');
+ const { form, href, title } = link?.attrs ?? {};
+ const isOwnTarget = href === text || href === `mailto:${text}` || href === `http://${text}`;
+ const isPlain = run.every(
+ (node) => markTypesOf(node) === markTypesOf(run[0]!) && !markTypesOf(node).includes('code'),
+ );
+ // Bold or emphasis that begins or ends at the link has its delimiter between the link and the text
+ // beside it, where only a space lets marked read it as one.
+ const [before, after] = [content[at - 1], content[end]];
+ const isAgainstMark =
+ (before !== undefined && stylesOf(before) !== stylesOf(run[0]) && /\S/.test(before.text?.at(-1) ?? ' ')) ||
+ (after !== undefined && stylesOf(after) !== stylesOf(run[0]) && /\S/.test(after.text?.[0] ?? ' '));
+
+ if (link) {
+ links.push(`${href}\0${text}`);
+ }
+
+ if ((form === 'angle' || form === 'bare') && !title && isOwnTarget && isPlain && !isAgainstMark) {
+ hasOwnForm = true;
+ written.push({
+ ...run[0],
+ marks: run[0]!.marks?.filter((mark) => mark.type !== 'link'),
+ text: form === 'angle' ? `<${text}>` : text,
+ });
+ } else {
+ written.push(...run);
+ }
+ }
+
+ const candidate = hasOwnForm ? helpers.renderChildren(written) : '';
+
+ return hasOwnForm && readLinks(candidate) === links.join('\0\0') ? candidate : helpers.renderChildren(content);
+};
+
// marked's cell splitter honors `\|` only after an ODD run of backslashes (it counts them), never collapses
// `\\`, and truncates a row that splits into extra columns — so cell text holding an odd run + pipe (`a\|b`)
// has NO exact GFM encoding: escaping the pipe alone yields `\\|`, a live delimiter that drops the trailing
@@ -132,25 +823,73 @@ const TunedMarkdownText = Extension.create({
// whole prompt (`unexpected "\" in operand`), so the file cannot be saved at all — and every save re-added the
// backslash, leaving no way out from rich mode. The loader counts cells with action pipes masked, so leaving
// them raw no longer skews the header/delimiter comparison.
+// A pipe inside a code span is escaped as well, as GFM asks. Whichever opens first is what marked reads
+// there: an action that holds a raw string is an action, a code span that holds an action is code, and the
+// action inside it still keeps its pipes. This is how a row is written once it has been edited; one that
+// has not is saved as the line it was (withWrittenRows), with its pipes the way its author put them.
export const escapeCellPipes = (text: string): string => {
- const escapeOutsideActions = (segment: string) =>
+ const escapePipes = (segment: string) =>
segment.replace(/(\\*)\|/g, (_, run: string) => `${run}${run.length % 2 ? '\\\\|' : '\\|'}`);
+
+ const escapeOutsideActions = (segment: string): string => {
+ let result = '';
+ let cursor = 0;
+
+ for (const action of segment.matchAll(TEMPLATE_ACTION)) {
+ result += escapePipes(segment.slice(cursor, action.index)) + action[0];
+ cursor = action.index + action[0].length;
+ }
+
+ return result + escapePipes(segment.slice(cursor));
+ };
+
+ const actions = new RegExp(TEMPLATE_ACTION.source, 'g');
+ let action = actions.exec(text);
let result = '';
let cursor = 0;
- for (const action of text.matchAll(TEMPLATE_ACTION)) {
- result += escapeOutsideActions(text.slice(cursor, action.index)) + action[0];
- cursor = action.index + action[0].length;
+ for (let at = 0; at < text.length; ) {
+ if (action && action.index < at) {
+ actions.lastIndex = at;
+ action = actions.exec(text);
+ }
+
+ const tick = text.indexOf('`', at);
+
+ if (!action && tick < 0) {
+ break;
+ }
+
+ if (action && (tick < 0 || action.index < tick)) {
+ result += escapePipes(text.slice(cursor, action.index)) + action[0];
+ cursor = at = action.index + action[0].length;
+ continue;
+ }
+
+ let runEnd = tick;
+
+ while (text[runEnd] === '`') {
+ runEnd++;
+ }
+
+ const closer = findCodeSpanCloser(text, runEnd, runEnd - tick);
+
+ // An unclosed backtick run is literal content, not a code span.
+ if (closer < 0) {
+ at = runEnd;
+ continue;
+ }
+
+ const code = text.slice(runEnd, closer);
+
+ result += escapePipes(text.slice(cursor, runEnd)) + escapeOutsideActions(code);
+ cursor = closer;
+ at = closer + runEnd - tick;
}
- return result + escapeOutsideActions(text.slice(cursor));
+ return result + escapePipes(text.slice(cursor));
};
-// @tiptap/extension-table's renderTableToMarkdown is alignment-aware but never escapes pipes, so a literal
-// `|` a cell emits (even from inside inline code) would re-parse as a column delimiter on the next SAVE and
-// drop cells (tiptap PR #7884). renderTableToMarkdown emits cell content only via h.renderChildren, so wrapping
-// that one call to escape pipes fixes the save side, on the official alignment-aware renderer.
-// The LOAD side of the same class is covered by the tuned Lexer subclass in createTunedMarked above.
// GFM has no headerless table, and renderTableToMarkdown answers that by emitting an EMPTY header row above
// the demoted rows — so every header-off + save + reload cycle grew the table by one blank row and the header
// switch silently flipped back on. Promote the first row instead: the row count and every cell survive, and a
@@ -172,28 +911,82 @@ const withPromotedHeaderRow = (node: JSONContent): JSONContent => {
};
};
-export const TunedTable = Table.extend({
+// A GFM cell is one line. renderTableToMarkdown writes a break inside one as `<br>`, which this editor — where
+// a tag is literal text — reloads as four characters. So each cell is rendered here, with a break collapsed to
+// a space, and handed over as plain text. A paragraph's inline content is rendered directly, past the paragraph
+// serializer: a cell cannot open a heading or a quote, and its line-leading escape would save `| # | Name |`
+// as `| \# | Name |`. escapeCellPipes is the save side of the pipe protection the tuned Lexer gives the load.
+const cellText = (cell: JSONContent, helpers: MarkdownRendererHelpers): string =>
+ escapeCellPipes(
+ (cell.content ?? [])
+ .map((block) =>
+ block.type === 'paragraph'
+ ? renderInline(block.content ?? [], helpers)
+ : helpers.renderChildren([block]),
+ )
+ .join(' ')
+ .replace(/[ \t]*\r?\n[ \t]*/g, ' '),
+ );
+
+const withSingleLineCells = (node: JSONContent, helpers: MarkdownRendererHelpers): JSONContent => ({
+ ...node,
+ content: (node.content ?? []).map((row) => ({
+ ...row,
+ content: (row.content ?? []).map((cell) => ({
+ ...cell,
+ content: [{ text: cellText(cell, helpers), type: 'text' }],
+ })),
+ })),
+});
+
+// `extend({ markdownTokenizer: undefined })` reads as "not overridden" and falls through to the parent's
+// tokenizer, so the absence has to be a value.
+export const withoutMarkdownTokenizer = <T extends AnyExtension>(extension: T): T =>
+ extension.extend({ markdownTokenizer: null as unknown as undefined }) as T;
+
+// @tiptap/extension-table tokenizes tables itself to protect a pipe inside a code span. The tuned Lexer above
+// already does that, for Go actions too, and the two together cost content: a row-like block whose delimiter
+// row has the wrong cell count is shredded into single characters, and a list item right after a table loses
+// its text when it holds a pipe. marked's own table rule, fed by our pre-pass, has neither problem.
+export const TunedTable = withoutMarkdownTokenizer(Table).extend({
renderMarkdown(node: JSONContent, helpers: MarkdownRendererHelpers) {
- const pipeEscaping: MarkdownRendererHelpers = {
+ const verbatim: MarkdownRendererHelpers = {
...helpers,
- renderChildren: (nodes, separator) => escapeCellPipes(helpers.renderChildren(nodes, separator)),
+ renderChildren: (nodes) => [nodes].flat().reduce((text, child) => text + (child.text ?? ''), ''),
};
- // renderTableToMarkdown joins a multi-block cell's children (e.g. two paragraphs from Enter-in-cell) with
- // a U+001F separator, outside renderChildren. A GFM cell is single-line, so collapse it to a space rather
- // than persist a raw control byte that survives into the saved .tmpl/knowledge and round-trips unchanged.
+ const table = withSingleLineCells(withPromotedHeaderRow(node), helpers);
// Trim the renderer's own surrounding blank lines. It emits a leading and trailing newline on top of the
// block separator the manager already adds, and between two ADJACENT tables that extra line reloads as an
- // empty paragraph — which serialises to another blank line on the next save. Measured before the trim:
- // two tables separated by one blank line gained one empty paragraph per cycle and never converged.
- return renderTableToMarkdown(withPromotedHeaderRow(node), pipeEscaping)
- .replaceAll(String.fromCharCode(0x1f), ' ')
- .replace(/^\n+/, '')
- .replace(/\n+$/, '');
+ // empty paragraph — which serialises to another blank line on the next save, so two tables separated by
+ // one blank line gain an empty paragraph per cycle and never converge.
+ const rendered =
+ typeof node.attrs?.delimiterRow === 'string'
+ ? renderCompactTable(table, node.attrs.delimiterRow, node.attrs.emptyCell)
+ : renderTableToMarkdown(table, verbatim).replace(/^\n+/, '').replace(/\n+$/, '');
+
+ return withWrittenRows(rendered.split('\n'), table, node.attrs?.written);
},
});
+// The token the tuned `def` tokenizer makes of a run of `[label]: target` lines: a paragraph of literal text,
+// its lines apart the way a paragraph's are (withLineBreaks), and written with a paragraph's escapes — a
+// target on a line of its own may be `---`.
+const LinkDefinitions = Extension.create({
+ name: 'linkDefinitions',
+ parseMarkdown: (token: { text?: string }, helpers: MarkdownParseHelpers) => ({
+ content: withoutLineEscapes(token.text ?? '')
+ .split('\n')
+ .flatMap((line, at) => [
+ ...(at ? [{ attrs: { marker: '' }, type: 'hardBreak' }] : []),
+ ...(line ? [helpers.createTextNode(line)] : []),
+ ]),
+ type: 'paragraph',
+ }),
+});
+
export const createMarkdownLayer = () => [
Markdown.configure({ marked: createTunedMarked() as unknown as typeof import('marked').marked }),
TunedMarkdownText,
+ withLayoutFacts(LinkDefinitions),
];
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-paste.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-paste.test.ts
index 56991380..b519ed1a 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-paste.test.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-paste.test.ts
@@ -1,6 +1,6 @@
import { Editor } from '@tiptap/core';
import { Slice } from '@tiptap/pm/model';
-import { beforeAll, describe, expect, it } from 'vitest';
+import { beforeAll, describe, expect, it, vi } from 'vitest';
import { GROWTH_IF_QUADRATIC, slowdownWhenInputQuadruples } from '@/test-utils/cost-growth';
@@ -87,6 +87,71 @@ describe('MarkdownPaste — the parsed payload matches load (same tuned markdown
return out;
};
+ it('leaves numbered lines pasted into a code block to the code block', () => {
+ const editor = new Editor({
+ content: '```\nnotes\n```',
+ contentType: 'markdown',
+ extensions: createMarkdownExtensions(),
+ });
+
+ editor.commands.setTextSelection(2);
+
+ const isHandled = editor.view.someProp('handlePaste', (handler) =>
+ handler(editor.view, pasteEvent('1. nmap -sV 10.0.0.5\n2. curl http://example.com/'), Slice.empty),
+ );
+ const html = editor.getHTML();
+
+ editor.destroy();
+
+ expect(isHandled).toBeFalsy();
+ expect(html).not.toContain('<ol');
+ expect(html).toContain('notes');
+ });
+
+ it.each([
+ ['a document that ends without one', 'first\n\nlast', '- x\n- y\n', 'first\n\n- x\n- y\n\nlast'],
+ ['a document that ends with one', 'first\n\nlast\n', '# T\n\ntext\n\n\n', 'first\n\n# T\n\ntext\n\nlast\n'],
+ ])('leaves the line breaks the pasted text ends with out of %s', (_name, md, pasted, saved) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+
+ editor.chain().setTextSelection(6).splitBlock().run();
+ editor.view.someProp('handlePaste', (handler) => handler(editor.view, pasteEvent(pasted), Slice.empty));
+
+ const out = editor.getMarkdown();
+
+ editor.destroy();
+
+ expect(out).toBe(saved);
+ });
+
+ // A rich clipboard is left to ProseMirror, whose link rule ends a URL inside the action it runs into.
+ // jsdom has neither ClipboardEvent nor DataTransfer, which the paste rules build for their handlers.
+ it.each([
+ ['a URL that runs into an action', 'https://e.x/{{.P}}'],
+ ['an address that begins inside one', '{{.User}}@example.com'],
+ ])('pastes %s from a rich clipboard as the text it is', (_name, text) => {
+ vi.stubGlobal('ClipboardEvent', class extends Event {});
+ vi.stubGlobal(
+ 'DataTransfer',
+ class {
+ getData = () => '';
+ setData = () => {};
+ },
+ );
+
+ const editor = new Editor({ content: 'x', contentType: 'markdown', extensions: createMarkdownExtensions() });
+
+ editor.chain().focus('end').insertContent(` ${text}`, { applyPasteRules: true }).run();
+
+ const out = editor.getMarkdown();
+
+ editor.destroy();
+
+ vi.unstubAllGlobals();
+
+ expect(out).toBe(`x ${text}`);
+ });
+
it('parses pasted block markdown into rich blocks (heading + list + table)', () => {
const html = pasteHtml('# Heading\n\n- one\n- two\n\n| A | B |\n| --- | --- |\n| 1 | 2 |');
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-paste.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-paste.ts
index 88a5d3d3..619424d4 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-paste.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-paste.ts
@@ -1,6 +1,8 @@
import { Extension } from '@tiptap/core';
import { Plugin, PluginKey } from '@tiptap/pm/state';
+import { lineBreaksAtEnd } from './markdown-editor-layout';
+
// @tiptap/markdown parses markdown for load/insertContent but never for the clipboard, so a paste of block
// markdown (# ## - 1. | >) would land as literal text (only StarterKit's inline mark paste-rules fire).
// Route markdown-looking plain-text pastes through the same markdown layer as load; defer to ProseMirror's
@@ -64,7 +66,12 @@ export const MarkdownPaste = Extension.create({
return false;
}
- return editor.commands.insertContent(text, { contentType: 'markdown' });
+ // Without the line breaks it ends with: read as a document's, they would be written
+ // at the end of this one.
+ return editor.commands.insertContent(
+ text.slice(0, text.length - lineBreaksAtEnd(text).length),
+ { contentType: 'markdown' },
+ );
},
},
}),
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-table-pipes.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-table-pipes.test.ts
index 76526c8a..684d5522 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-table-pipes.test.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-table-pipes.test.ts
@@ -5,7 +5,7 @@ import { GROWTH_IF_QUADRATIC, slowdownWhenInputQuadruples } from '@/test-utils/c
import { createMarkdownExtensions } from './markdown-editor-extensions';
import { escapeTablePipes } from './markdown-editor-table-pipes';
-import { roundTrip, setupEditorJsdom } from './markdown-editor-test-setup';
+import { roundTrip, setupEditorJsdom, structuralCounts } from './markdown-editor-test-setup';
setupEditorJsdom();
@@ -71,11 +71,9 @@ describe('escapeTablePipes — pure pre-lex pipe protection', () => {
describe('table cell with a piped code span — content survives load and converges (H1)', () => {
it('keeps the trailing cell and preserves the code content', () => {
- const out = roundTrip('| Op | Meaning |\n| --- | --- |\n| `x | y` | z |');
+ const src = '| Op | Meaning |\n| --- | --- |\n| `x | y` | z |';
- expect(out).toContain('z');
- expect(out).toContain('`x \\| y`');
- expect(roundTrip(out)).toBe(out);
+ expect(roundTrip(src)).toBe(src);
});
// This used to pin `{{.X \| upper}}` — the escaped form, which Go text/template rejects outright
@@ -94,7 +92,7 @@ describe('table cell with a piped code span — content survives load and conver
const out = roundTrip('| Op | Meaning |\n| --- | --- |\n| `` `x` | y `` | kept |');
expect(out).toContain('kept');
- expect(out).toContain('`` `x` \\| y ``');
+ expect(out).toContain('`` `x` | y ``');
expect(roundTrip(out)).toBe(out);
});
});
@@ -110,7 +108,7 @@ describe('pipe-less GFM tables (no outer pipe) — cells survive too', () => {
const out = roundTrip('A | B | C\n--- | --- | ---\n`git log | head` | notes | done');
expect(out).toContain('done');
- expect(out).toContain('`git log \\| head`');
+ expect(out).toContain('`git log | head`');
expect(roundTrip(out)).toBe(out);
});
@@ -176,12 +174,11 @@ describe('table cell with a pipe inside a URL — content survives load and conv
);
});
- it('keeps the URL and the trailing cell on round-trip, and converges', () => {
- const out = roundTrip('| A | B |\n| --- | --- |\n| [go](https://h/?x=1|2) | TRAILING |');
+ it('keeps the URL and the trailing cell on round-trip', () => {
+ const md = '| A | B |\n| --- | --- |\n| [go](https://h/?x=1|2) | TRAILING |';
- expect(out).toContain('TRAILING');
- expect(out).toContain('x=1\\|2');
- expect(roundTrip(out)).toBe(out);
+ expect(structuralCounts(md)).toMatchObject({ tableCell: 2 });
+ expect(roundTrip(md)).toBe(md);
});
});
@@ -199,10 +196,9 @@ describe('tables inside a blockquote — prefix-stripped and protected', () => {
});
it('keeps the trailing cell of a blockquoted table on round-trip', () => {
- const out = roundTrip('> | a | b |\n> | --- | --- |\n> | `x | y` | z |');
+ const src = '> | a | b |\n> | --- | --- |\n> | `x | y` | z |';
- expect(out).toContain('z');
- expect(out).toContain('`x \\| y`');
+ expect(roundTrip(src)).toBe(src);
});
it('leaves blockquote prose (no table) untouched', () => {
@@ -226,10 +222,9 @@ describe('fence length tracking — a longer fence is not closed by a shorter in
});
it('round-trips a table after a fence-demonstrating code block without losing the cell', () => {
- const out = roundTrip('````\n```\ninner\n````\n\n| a | b |\n| --- | --- |\n| `x | y` | z |');
+ const src = '````\n```\ninner\n````\n\n| a | b |\n| --- | --- |\n| `x | y` | z |';
- expect(out).toContain('z');
- expect(out).toContain('`x \\| y`');
+ expect(roundTrip(src)).toBe(src);
});
});
@@ -243,8 +238,7 @@ describe('CRLF line endings — tables still protected', () => {
it('keeps the trailing cell of a CRLF table on round-trip', () => {
const out = roundTrip('| a | b |\r\n| --- | --- |\r\n| `x | y` | z |\r\n');
- expect(out).toContain('z');
- expect(out).toContain('`x \\| y`');
+ expect(out).toBe('| a | b |\n| --- | --- |\n| `x | y` | z |\n');
});
it('leaves CRLF bytes untouched when there is no table to escape', () => {
@@ -450,10 +444,100 @@ describe('a Go template pipeline in a table cell keeps its own pipe', () => {
expect(roundTrip(once)).toBe(once);
});
+});
+describe('the scan takes for a row only what marked takes for one', () => {
+ it.each([
+ ['a standard HTML block tag under the table', '| a |\n|---|\n<p>`x | y`</p>'],
+ ['a closing block tag under the table', '| a |\n|---|\n| 1 |\n</details>\n`x | y`'],
+ [
+ 'a template control line under the table',
+ '| a | b |\n|---|---|\n{{ range .Rows }}| `x | y` | z |\n{{ end }}',
+ ],
+ ])('leaves the pipes of %s alone', (_name, md) => {
+ expect(escapeTablePipes(md)).toBe(md);
+ });
+
+ it.each([
+ ['a tag that is no HTML block', '| a |\n|---|\n<tool>`x | y`</tool>', '| a |\n|---|\n<tool>`x \\| y`</tool>'],
+ ['an action that is no control line', '| a |\n|---|\n{{ .X }} `x | y`', '| a |\n|---|\n{{ .X }} `x \\| y`'],
+ [
+ 'an action with a brace in a string',
+ '| a |\n|---|\n| {{ printf "{%s}" .X | print }} |',
+ '| a |\n|---|\n| {{ printf "{%s}" .X \\| print }} |',
+ ],
+ ])('still protects the row of %s', (_name, md, escaped) => {
+ expect(escapeTablePipes(md)).toBe(escaped);
+ });
+
+ // The scan may take a line for a row that marked reads as something else. The stand-in it leaves there is
+ // a pipe again in whatever the line turned out to be.
+ it.each([
+ [
+ 'a heading over a row of dashes',
+ '## T | N\n|---|---|\n| `a | b` | {{ .A | print }} |',
+ '| `a | b` | {{ .A | print }} |',
+ ],
+ ['a line indented like code over a row of dashes', ' a | b\n|---|---|\n| `x | y` | c |', '| `x | y` | c |'],
+ ])('leaves no trace in %s, which is no table', (_name, md, row) => {
+ const saved = roundTrip(md);
+
+ expect(structuralCounts(md).table).toBeUndefined();
+ expect(saved.split('\n').at(-1)).toBe(row);
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it.each([
+ ['a row with a cell too many', '| a | b |\n|---|---|\n| 1 | 2 | `x | y` |'],
+ ['a line of nothing but actions', '| a | b |\n|---|---|\n{{ $n := len .Rows }} {{/* `x | y` */}}'],
+ ['the rows under the line that ended the table', '| a | b |\n|---|---|\n{{ range .R }}\n| `x | y` | 2 |'],
+ ])('leaves the pipes of %s alone', (_name, md) => {
+ expect(escapeTablePipes(md)).toBe(md);
+ });
+
+ it('finds the table under an item that opens its fence on the marker line', () => {
+ const list = '1. Run:\n ```sh\n nmap\n ```\n2. ```sh\n curl\n ```\n\n';
+
+ expect(escapeTablePipes(`${list}| a | b |\n|---|---|\n| \`x | y\` | 2 |`)).toBe(
+ `${list}| a | b |\n|---|---|\n| \`x \\| y\` | 2 |`,
+ );
+ });
+
+ it('takes a string that holds the end of an action for part of the action', () => {
+ expect(escapeTablePipes('| a | b |\n|---|---|\n| {{ print "}}" | print }} | 2 |')).toBe(
+ '| a | b |\n|---|---|\n| {{ print "}}" \\| print }} | 2 |',
+ );
+ });
+
+ it('finds actions that hold strings in time that grows with the row, not with the square of it', () => {
+ const row = (size: number) => `| a |\n|---|\n| ${'{{ "x '.repeat(size / 7)} |`;
+
+ expect(slowdownWhenInputQuadruples(row, escapeTablePipes, 140_000)).toBeLessThan(GROWTH_IF_QUADRATIC / 2);
+ });
+
+ it('keeps a row that opens with an action whose name only begins with a control word', () => {
+ const md = '| a | b |\n|---|---|\n{{ endpoint }} | 2\n{{ iffy .X }} | 3';
+
+ expect(structuralCounts(md).tableRow).toBe(3);
+ expect(escapeTablePipes(`${md} \`x | y\``)).toBe(`${md} \`x \\| y\``);
+ });
+
+ it('protects with a backslash in a document that holds the stand-in itself', () => {
+ const saved = roundTrip('x \uE002 y\n\n| a | b |\n|---|---|\n| `p | q` | 2 |');
+
+ expect(saved).toBe('x \uE002 y\n\n| a | b |\n|---|---|\n| `p \\| q` | 2 |');
+ expect(roundTrip(saved)).toBe(saved);
+ });
+
+ it('writes the pipes it protects as the stand-in it is asked for', () => {
+ expect(escapeTablePipes('| a | b |\n|---|---|\n| `x | y` | {{ .A | print }} |', '\uE002')).toBe(
+ '| a | b |\n|---|---|\n| `x \uE002 y` | {{ .A \uE002 print }} |',
+ );
+ });
+
+ it('finds actions in time that grows with the row, not with the square of it', () => {
+ const row = (size: number) => `| a |\n|---|\n| ${'{{ x { '.repeat(size / 7)} |`;
- // A structural pipe still has to be escaped when it is real content, not a template operator.
- it('still escapes a pipe inside a code span', () => {
- expect(roundTrip('| a | b |\n| --- | --- |\n| `x | y` | z |')).toContain('\\|');
+ expect(slowdownWhenInputQuadruples(row, escapeTablePipes, 140_000)).toBeLessThan(GROWTH_IF_QUADRATIC / 2);
});
});
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-table-pipes.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-table-pipes.ts
index cbecc8b6..09b5c316 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-table-pipes.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-table-pipes.ts
@@ -1,8 +1,13 @@
+import { Lexer } from 'marked';
+
// marked's GFM table tokenizer splits every row on raw `|` BEFORE inline tokenization, so a pipe inside a
// code span (`` `x | y` ``), a Go-template action (`{{.X | upper}}`), or a URL (`[x](http://a|b)`, a bare
// `http://a|b`, an image src) in a cell creates a phantom column and the trailing cells are silently DROPPED
// on load. The splitter does honor `\|` — and unescapes it in the cell text — so pre-escaping those pipes
-// before marked lexes protects the load side the same way TunedTable's renderChildren escape protects save.
+// before marked lexes protects the load side the same way escapeCellPipes in cellText protects save. The
+// Lexer asks for a stand-in character instead of `\|` (see TABLE_PIPE): it is not a pipe to the splitter
+// either, it is put back as a pipe wherever it lands, and it tells a pipe that was written bare from one the
+// author escaped.
//
// Scope must match EXACTLY the rows marked itself treats as the table — no more, no less:
// • the header/delimiter pair must already parse as a table (matching cell counts, delimiter has |/:) —
@@ -11,11 +16,8 @@
// • the leading/trailing pipe is OPTIONAL in GFM, so the header row AND every body row are transformed
// (marked's own gfmTable body capture — `(?!blank|hr|heading|blockquote|code|fences|list).*` — has no
// leading-pipe requirement; gating on a leading `|` dropped cells for the common no-outer-pipe style);
-// • a table's rows run until a blank line or the start of another block (mirrored by ENDS_TABLE_BODY);
+// • a table's rows run until a blank line or the start of another block (isTableBodyEnd);
// • fenced code blocks are never touched.
-// Not covered: an HTML-block line (a standard block tag) immediately following a table with no blank line —
-// marked would end the table there; we do not replicate its HTML-block interrupt (vanishingly rare in this
-// content, and our html tokenizers render such lines as literal text anyway).
// Capture the full fence run (not a fixed 3) plus the trailing text: renderTunedCodeBlock widens a fence to
// 4+ backticks when its content holds a ``` line, and CommonMark closes a fence only with a run of the same
@@ -27,35 +29,38 @@
const FENCE_LINE = /^ {0,3}(`{3,}(?=[^`\n]*$)|~{3,})(.*)$/;
// Written to be linear: the trailing `(?: *\|)? *$` (not `\|? *$`) plus per-cell spacing keep any two space
// runs from competing for the same characters, so a crafted delimiter-looking line can't force O(n²) backtracking.
-const TABLE_DELIMITER_LINE = /^ {0,3}\|? *:?-+:?(?: *\| *:?-+:?)*(?: *\|)? *$/;
-export const TEMPLATE_ACTION = /\{\{[^{}]*\}\}/g;
+export const TABLE_DELIMITER_LINE = /^ {0,3}\|? *:?-+:?(?: *\| *:?-+:?)*(?: *\|)? *$/;
+// A Go template action. A lone brace inside it — `{{ printf "{%s}" .X }}` — is part of it, and so is `}}`
+// inside a string, a rune or a comment. Every character has one reading and none of them runs over `{{`, so a
+// scan that fails stops at the next one, which keeps it linear.
+export const TEMPLATE_ACTION =
+ /\{\{(?:\/\*(?:[^*{]|\*(?!\/)|\{(?!\{))*\*\/|"(?:\\.|[^"\\\n{]|\{(?!\{))*"|`(?:[^`{]|\{(?!\{))*`|'(?:\\.|[^'\\\n{}])'|'(?!(?:\\.|[^'\\\n{}])')|\/(?!\*)|[^{}"`'/]|\{(?!\{)|\}(?!\}))*\}\}/g;
+// A line that opens with a control action is the template's own line: `{{ range .Rows }}` and `{{ end }}`
+// around the rows of a table are not rows of it, and read as rows they are padded into cells.
+const TEMPLATE_CONTROL_LINE = /^ {0,3}\{\{-?[ \t]*(?:range|if|else|end|with|define|template|block|break|continue)\b/;
// A pipe inside a Go action is the template's pipeline operator, never a column separator — marked's splitter
// does not know that, so the scanner masks those pipes before counting. Without this the serializer cannot
// stop escaping them: an unescaped pipeline in the HEADER row would count one cell too many, detection would
// bail, and marked would degrade the whole table to a paragraph.
const maskActionPipes = (row: string): string => row.replace(TEMPLATE_ACTION, (action) => action.replace(/\|/g, ''));
-// One blockquote marker (`>` + an optional space). The line scanner only recognizes top-level tables, but marked
+// One blockquote marker (`>` + an optional space or tab, as marked takes it). The line scanner only recognizes top-level tables, but marked
// strips this prefix and re-lexes the inner content, so a table inside a blockquote needs the prefix removed
// before detection — see the recursive handling in escapeTablePipes.
-const BLOCKQUOTE_PREFIX = /^ {0,3}> ?/;
+const BLOCKQUOTE_PREFIX = /^ {0,3}>[ \t]?/;
// A list item whose content actually starts on the same line. CommonMark puts that content at the column after
// the marker plus its following spaces, except that 5+ spaces mean the item opens with indented code and the
// content column is one past the marker.
const LIST_ITEM_START = /^( {0,3})([*+-]|\d{1,9}[.)])( +)(?=\S)/;
-// A table's body ends at a blank line or the first line that starts a different block — the same interrupts
-// marked's gfmTable body-row negative lookahead lists (heading, blockquote, fences, list, hr, indented code).
-const ENDS_TABLE_BODY = [
- /^[ \t]*$/,
- FENCE_LINE,
- /^ {0,3}#{1,6}(?: |\t|$)/,
- /^ {0,3}>/,
- /^ {0,3}(?:[*+-]|1[.)])[ \t]/,
- /^ {0,3}(?:(?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})$/,
- /^(?: {4}| {0,3}\t)/,
-];
+// A table's body ends at a blank line or the first line that starts a different block. Which lines those are
+// is read off marked's own table rule — its body rows are `(?:(?!…).*(?:\n|$))*` — so the two cannot drift
+// apart: a standard HTML block tag ends a table too. The writer asks the same question before it puts text
+// on the line right under a table. A marked whose rule has another shape leaves only the blank line.
+const BODY_ROWS = /\(\?:\\n\(\(\?:\(\?!(.*)\)\.\*\(\?:\\n\|\$\)\)\*\)\\n\*\|\$\)$/;
+const ENDS_TABLE_BODY = new RegExp(`^(?:${BODY_ROWS.exec(Lexer.rules.block.gfm.table.source)?.[1] ?? ' *\\n'})`);
-const isTableBodyEnd = (line: string): boolean => ENDS_TABLE_BODY.some((rule) => rule.test(line));
+// The rule reads a line with the line break that ends it.
+export const isTableBodyEnd = (line: string): boolean => ENDS_TABLE_BODY.test(`${line}\n`);
const isEscapedAt = (text: string, offset: number): boolean => {
let isEscaped = false;
@@ -68,8 +73,13 @@ const isEscapedAt = (text: string, offset: number): boolean => {
return isEscaped;
};
-const escapeUnescapedPipes = (text: string): string =>
- text.replace(/\|/g, (pipe, offset: number, source: string) => (isEscapedAt(source, offset) ? pipe : '\\|'));
+// What a protected pipe is written as while marked lexes the document: a private-use character that the Lexer
+// turns back into a pipe in every token. A scan that takes a line for a row which marked does not then leaves
+// nothing behind, where `\|` would stay in the text as a backslash.
+export const TABLE_PIPE = '\uE002';
+
+const escapeUnescapedPipes = (text: string, escaped: string): string =>
+ text.replace(/\|/g, (pipe, offset: number, source: string) => (isEscapedAt(source, offset) ? pipe : escaped));
// Mirrors marked's splitCells: split on unescaped pipes, drop a blank leading/trailing cell.
const countCells = (row: string): number => {
@@ -88,7 +98,7 @@ const countCells = (row: string): number => {
return cells.length;
};
-const findCodeSpanCloser = (row: string, from: number, runLength: number): number => {
+export const findCodeSpanCloser = (row: string, from: number, runLength: number): number => {
for (let index = from; index < row.length; index++) {
if (row[index] !== '`') {
continue;
@@ -110,7 +120,7 @@ const findCodeSpanCloser = (row: string, from: number, runLength: number): numbe
return -1;
};
-const escapeRowPipes = (row: string, expectedCells: number): string => {
+const escapeRowPipes = (row: string, expectedCells: number, escaped: string): string => {
let result = '';
let index = 0;
@@ -139,11 +149,11 @@ const escapeRowPipes = (row: string, expectedCells: number): string => {
continue;
}
- result += run + escapeUnescapedPipes(row.slice(runEnd, closerStart)) + run;
+ result += run + escapeUnescapedPipes(row.slice(runEnd, closerStart), escaped) + run;
index = closerStart + run.length;
}
- result = result.replace(TEMPLATE_ACTION, (action) => escapeUnescapedPipes(action));
+ result = result.replace(TEMPLATE_ACTION, (action) => escapeUnescapedPipes(action, escaped));
// A pipe inside a URL (bare or linked) is content, but a spaceless `|` between two cells is a separator —
// the two are indistinguishable in isolation. Only escape scheme-run pipes when the row otherwise splits
@@ -153,10 +163,29 @@ const escapeRowPipes = (row: string, expectedCells: number): string => {
return result;
}
- return result.replace(/\S+/g, (token) => (token.includes('://') ? escapeUnescapedPipes(token) : token));
+ return result.replace(/\S+/g, (token) => (token.includes('://') ? escapeUnescapedPipes(token, escaped) : token));
+};
+
+// A line marked would take for a row and that is not one: the template's own line — it opens with a control
+// action, or holds nothing but actions — and a line with more cells than the header, whose extra cells marked
+// drops without a trace. The table ends above it and the line is the text it was written as.
+export const isNotARow = (line: string, columns: number): boolean => {
+ if (TEMPLATE_CONTROL_LINE.test(line)) {
+ return true;
+ }
+
+ const rest = line.replace(TEMPLATE_ACTION, '');
+
+ return (rest !== line && !rest.trim()) || countCells(escapeRowPipes(line, columns, TABLE_PIPE)) > columns;
};
-export const escapeTablePipes = (markdown: string): string => {
+export const countRowCells = (row: string): number => countCells(row);
+
+// Whether a row reads as its cells only because this scan protects a pipe in it before marked splits it.
+export const holdsProtectedPipe = (row: string, columns: number): boolean =>
+ escapeRowPipes(row, columns, TABLE_PIPE) !== row;
+
+export const escapeTablePipes = (markdown: string, escaped = '\\|'): string => {
if (!markdown.includes('|')) {
return markdown;
}
@@ -170,10 +199,10 @@ export const escapeTablePipes = (markdown: string): string => {
let isChanged = false;
const escapeRow = (row: number, expectedCells: number): void => {
- const escaped = escapeRowPipes(lines[row]!, expectedCells);
+ const protectedRow = escapeRowPipes(lines[row]!, expectedCells, escaped);
- if (escaped !== lines[row]) {
- lines[row] = escaped;
+ if (protectedRow !== lines[row]) {
+ lines[row] = protectedRow;
isChanged = true;
}
};
@@ -210,10 +239,10 @@ export const escapeTablePipes = (markdown: string): string => {
const prefixes = lines.slice(index, end).map((row) => BLOCKQUOTE_PREFIX.exec(row)![0]);
const inner = lines.slice(index, end).map((row, offset) => row.slice(prefixes[offset]!.length));
- const escaped = escapeTablePipes(inner.join('\n')).split('\n');
+ const inside = escapeTablePipes(inner.join('\n'), escaped).split('\n');
for (let row = index; row < end; row++) {
- const rebuilt = prefixes[row - index]! + escaped[row - index]!;
+ const rebuilt = prefixes[row - index]! + inside[row - index]!;
if (rebuilt !== lines[row]) {
lines[row] = rebuilt;
@@ -248,15 +277,23 @@ export const escapeTablePipes = (markdown: string): string => {
const run = lines.slice(index, end);
- if (end > index + 1 && run.some((row) => row.includes('|'))) {
- const escaped = escapeTablePipes(run.map((row) => row.slice(contentColumn)).join('\n')).split('\n');
-
- for (let row = index; row < end; row++) {
- const rebuilt = lines[row]!.slice(0, contentColumn) + escaped[row - index]!;
-
- if (rebuilt !== lines[row]) {
- lines[row] = rebuilt;
- isChanged = true;
+ // The run is the item's, with or without a table in it: walked line by line here, a fence the
+ // item opens on its marker line would be closed by this scan where the item closes it, and
+ // every table below would go unprotected.
+ if (end > index + 1) {
+ if (run.some((row) => row.includes('|'))) {
+ const inside = escapeTablePipes(
+ run.map((row) => row.slice(contentColumn)).join('\n'),
+ escaped,
+ ).split('\n');
+
+ for (let row = index; row < end; row++) {
+ const rebuilt = lines[row]!.slice(0, contentColumn) + inside[row - index]!;
+
+ if (rebuilt !== lines[row]) {
+ lines[row] = rebuilt;
+ isChanged = true;
+ }
}
}
@@ -266,10 +303,7 @@ export const escapeTablePipes = (markdown: string): string => {
}
}
- if (!line.includes('|')) {
- continue;
- }
-
+ // A header may be written without a pipe; the delimiter row under it is what makes the table.
const delimiter = lines[index + 1];
if (delimiter === undefined || !TABLE_DELIMITER_LINE.test(delimiter) || !/[|:]/.test(delimiter)) {
@@ -286,7 +320,11 @@ export const escapeTablePipes = (markdown: string): string => {
// content. escapeRowPipes leaves structural pipes alone, so escaping the header can't skew its cell count.
escapeRow(index, expected);
- for (let row = index + 2; row < lines.length && !isTableBodyEnd(lines[row]!); row++) {
+ for (let row = index + 2; row < lines.length; row++) {
+ if (isTableBodyEnd(lines[row]!) || isNotARow(lines[row]!, expected)) {
+ break;
+ }
+
escapeRow(row, expected);
}
}
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-test-setup.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-test-setup.ts
index 9deb2ed0..fd8704f2 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-test-setup.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-test-setup.ts
@@ -1,3 +1,5 @@
+import type { JSONContent } from '@tiptap/core';
+
import { Editor } from '@tiptap/core';
import { createMarkdownExtensions } from './markdown-editor-extensions';
@@ -54,3 +56,18 @@ export const structuralCounts = (markdown: string): Record<string, number> => {
return counts;
};
+
+// The two halves of a round trip, apart, for a test that measures how the cost of one grows.
+export const markdownCodec = (): {
+ destroy: () => void;
+ parse: (markdown: string) => JSONContent;
+ serialize: (doc: JSONContent) => string;
+} => {
+ const editor = new Editor({ extensions: createMarkdownExtensions() });
+
+ return {
+ destroy: () => editor.destroy(),
+ parse: (markdown) => editor.markdown!.parse(markdown),
+ serialize: (doc) => editor.markdown!.serialize(doc),
+ };
+};
diff --git a/frontend/src/features/knowledges/knowledge-form-blocker.test.tsx b/frontend/src/features/knowledges/knowledge-form-blocker.test.tsx
index 6549b760..ca4471c2 100644
--- a/frontend/src/features/knowledges/knowledge-form-blocker.test.tsx
+++ b/frontend/src/features/knowledges/knowledge-form-blocker.test.tsx
@@ -95,7 +95,7 @@ describe('KnowledgeForm — Save and leave (data router)', () => {
render(<RouterProvider router={router} />);
- await user.type(screen.getByLabelText('content'), 'hello world');
+ await user.type(screen.getByLabelText('content'), 'hello world{Enter}');
await user.type(screen.getByLabelText('question'), 'why');
await user.click(screen.getByRole('link', { name: 'go elsewhere' }));
@@ -106,5 +106,6 @@ describe('KnowledgeForm — Save and leave (data router)', () => {
await waitFor(() => expect(screen.getByText('elsewhere page')).toBeInTheDocument());
expect(screen.queryByText('new doc page')).not.toBeInTheDocument();
expect(onSubmit).toHaveBeenCalledOnce();
+ expect(onSubmit.mock.lastCall![0].content).toBe('hello world\n');
});
});
diff --git a/frontend/src/features/knowledges/knowledge-form-helpers.test.tsx b/frontend/src/features/knowledges/knowledge-form-helpers.test.tsx
index 9964ece9..6becc79d 100644
--- a/frontend/src/features/knowledges/knowledge-form-helpers.test.tsx
+++ b/frontend/src/features/knowledges/knowledge-form-helpers.test.tsx
@@ -64,6 +64,15 @@ describe('formValuesToCreateInput', () => {
});
});
+describe('the content a form value carries', () => {
+ it('reaches both inputs as it was written', () => {
+ const values = { ...baseValues, content: ' # Title\n\ntext\n' };
+
+ expect(formValuesToCreateInput(values).content).toBe(' # Title\n\ntext\n');
+ expect(formValuesToUpdateInput(values, {}).content).toBe(' # Title\n\ntext\n');
+ });
+});
+
describe('formValuesToUpdateInput', () => {
it('always sends content and omits untouched fields', () => {
const input = formValuesToUpdateInput(baseValues, {});
@@ -148,6 +157,36 @@ describe('formSchema', () => {
expect(result.error?.issues.find((i) => i.path[0] === 'content')?.message).toBe('Content is required');
});
+ it.each([
+ ['spaces', ' '],
+ ['line breaks and tabs', '\n\t \n'],
+ ])('refuses a content of nothing but %s', (_name, content) => {
+ const result = formSchema.safeParse({ ...valid, content });
+
+ expect(result.error?.issues.find((i) => i.path[0] === 'content')?.message).toBe('Content is required');
+ });
+
+ it('hands the content on as it was written and trims the one-line fields', () => {
+ const result = formSchema.safeParse({
+ ...valid,
+ codeLang: ' go ',
+ content: ' # Title\n\ntext\n',
+ description: ' about ',
+ question: ' which port? ',
+ });
+
+ expect(result.data).toMatchObject({
+ codeLang: 'go',
+ content: ' # Title\n\ntext\n',
+ description: 'about',
+ question: 'which port?',
+ });
+ });
+
+ it('counts the line break a content ends with toward its limit', () => {
+ expect(formSchema.safeParse({ ...valid, content: `${'a'.repeat(65536)}\n` }).success).toBe(false);
+ });
+
it('requires question', () => {
const result = formSchema.safeParse({ ...valid, question: '' });
diff --git a/frontend/src/features/knowledges/knowledge-form.test.tsx b/frontend/src/features/knowledges/knowledge-form.test.tsx
index e97ac10b..1db72c84 100644
--- a/frontend/src/features/knowledges/knowledge-form.test.tsx
+++ b/frontend/src/features/knowledges/knowledge-form.test.tsx
@@ -99,7 +99,7 @@ const newValues: FormValues = {
const editValues: FormValues = {
answerType: KnowledgeAnswerType.Other,
codeLang: '',
- content: 'existing content',
+ content: 'existing content\n',
description: '',
docType: KnowledgeDocType.Answer,
guideType: undefined,
@@ -162,6 +162,7 @@ describe('KnowledgeForm — update', () => {
await waitFor(() => expect(onSubmit).toHaveBeenCalledOnce());
const [values, dirty] = onSubmit.mock.lastCall!;
expect(values.question).toBe('updated question');
+ expect(values.content).toBe('existing content\n');
expect(dirty).toMatchObject({ question: true });
// Save re-disables once the post-save reset clears isDirty — a deterministic
@@ -172,7 +173,7 @@ describe('KnowledgeForm — update', () => {
it('resets untouched fields to the server document returned by onSubmit', async () => {
const user = userEvent.setup();
- // Server normalizes `content` (a field the user never edits here). The form's
+ // The server's document carries another `content` (a field the user never edits here). The form's
// resetOptions keep DIRTY fields at their typed value, so asserting the reset
// applied the server document means asserting on an untouched field.
const serverDocument = {
@@ -212,7 +213,7 @@ describe('KnowledgeForm — update', () => {
await waitFor(() => expect(onSubmit).toHaveBeenCalledOnce());
- // editValues.content is 'existing content'; reaching the server value proves the
+ // editValues.content is 'existing content\n'; reaching the server value proves the
// form took `documentToFormValues(result.document)`, not the local `values` fallback.
await waitFor(() => expect(screen.getByLabelText('content')).toHaveValue('server normalized content'));
expect(screen.getByRole('button', { name: 'Save' })).toBeDisabled();
diff --git a/frontend/src/features/knowledges/knowledge-form.tsx b/frontend/src/features/knowledges/knowledge-form.tsx
index 3603a34d..cc6d4d20 100644
--- a/frontend/src/features/knowledges/knowledge-form.tsx
+++ b/frontend/src/features/knowledges/knowledge-form.tsx
@@ -51,10 +51,11 @@ export const formSchema = z
.object({
answerType: z.nativeEnum(KnowledgeAnswerType).optional(),
codeLang: optionalTrimmed(KNOWLEDGE_LIMITS.codeLang, 'Code language'),
+ // The body is a document: it is sent as it was written, the line break it ends with included, and the
+ // limit counts all of it, as the endpoint does.
content: z
.string()
- .trim()
- .min(1, { message: 'Content is required' })
+ .refine((value) => value.trim() !== '', { message: 'Content is required' })
.refine(withinLimit(KNOWLEDGE_LIMITS.content), {
message: `Content must be ${KNOWLEDGE_LIMITS.content} characters or fewer`,
}),
@@ -229,7 +230,7 @@ export function KnowledgeForm({ initialValues, isNew, knowledge, onSubmit }: Kno
// `isDirty`/`canSubmit` at submit time.
const result = await onSubmit(values, form.formState.dirtyFields as DirtyFlags);
- // The backend may trim/normalize fields, so reset to its returned document when present.
+ // The returned document is the one that is stored, so the form is reset to it when present.
const resetValues = result.document ? documentToFormValues(result.document) : values;
// Reset BEFORE the caller navigates so `isDirty` is false by the
@@ -259,7 +260,7 @@ export function KnowledgeForm({ initialValues, isNew, knowledge, onSubmit }: Kno
// `form.getValues()` returns raw field state (no zod transforms applied),
// so we run it through the schema explicitly. This way the dialog path
- // produces the same trimmed/normalized values as the form-button path
+ // produces the same parsed values as the form-button path
// (which gets parsed values directly from `handleSubmit`'s callback).
const parsed = formSchema.safeParse(form.getValues());
diff --git a/frontend/src/pages/knowledges/knowledge.tsx b/frontend/src/pages/knowledges/knowledge.tsx
index 01a2d7a2..96dd18a9 100644
--- a/frontend/src/pages/knowledges/knowledge.tsx
+++ b/frontend/src/pages/knowledges/knowledge.tsx
@@ -63,7 +63,7 @@ function Knowledge() {
);
const handleSubmit = useCallback(
- // `values` are the zod-parsed form output (trimmed, length-validated).
+ // `values` are the zod-parsed form output (single-line fields trimmed, lengths validated).
// CREATE sends a full payload; UPDATE sends only fields the user
// actually changed (`dirtyFields`) so untouched optional fields stay
// untouched on the backend and explicit clears (e.g. wiping an existing
diff --git a/frontend/src/pages/templates/template.test.tsx b/frontend/src/pages/templates/template.test.tsx
index 49ee30af..f56a433b 100644
--- a/frontend/src/pages/templates/template.test.tsx
+++ b/frontend/src/pages/templates/template.test.tsx
@@ -30,6 +30,26 @@ describe('the flow template form schema', () => {
expect(parse({ text: 'a'.repeat(TEXT_LIMIT + 1) }).success).toBe(false);
});
+ it.each([
+ ['spaces', ' '],
+ ['line breaks and tabs', '\n\t \n'],
+ ])('refuses a text of nothing but %s', (_name, text) => {
+ expect(parse({ text }).error?.issues.find((issue) => issue.path[0] === 'text')?.message).toBe(
+ 'Text is required',
+ );
+ });
+
+ it('hands the text on as it was written and trims the title', () => {
+ expect(parse({ text: ' # Plan\n\n- scan\n', title: ' Recon ' }).data).toEqual({
+ text: ' # Plan\n\n- scan\n',
+ title: 'Recon',
+ });
+ });
+
+ it('counts the line break a text ends with toward its limit', () => {
+ expect(parse({ text: `${'a'.repeat(TEXT_LIMIT)}\n` }).success).toBe(false);
+ });
+
it('counts an astral text character once, the way the mutation does', () => {
expect(parse({ text: '\u{1f512}'.repeat(TEXT_LIMIT) }).success).toBe(true);
expect(parse({ text: '\u{1f512}'.repeat(TEXT_LIMIT + 1) }).success).toBe(false);
diff --git a/frontend/src/pages/templates/template.tsx b/frontend/src/pages/templates/template.tsx
index 48fae81f..a2f5c9e6 100644
--- a/frontend/src/pages/templates/template.tsx
+++ b/frontend/src/pages/templates/template.tsx
@@ -50,10 +50,11 @@ const MAX_TEMPLATE_TITLE_LENGTH = 255;
const MAX_TEMPLATE_TEXT_LENGTH = 65536;
export const formSchema = z.object({
+ // The text is a document: it is sent as it was written, the line break it ends with included, and the
+ // limit counts all of it, as the mutation does.
text: z
.string()
- .trim()
- .min(1, { message: 'Text is required' })
+ .refine((value) => value.trim() !== '', { message: 'Text is required' })
.refine((value) => [...value].length <= MAX_TEMPLATE_TEXT_LENGTH, {
message: `Text must not exceed ${MAX_TEMPLATE_TEXT_LENGTH} characters`,
}),