Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for plain syntax highlight #4114

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/quill/src/modules/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
}

highlight(
highlight: (text: string, language: string) => Delta,
highlight: (text: string, language: string, oldDelta: Delta | null) => Delta,

Check failure on line 118 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (chromium)

Replace `text:·string,·language:·string,·oldDelta:·Delta·|·null` with `⏎······text:·string,⏎······language:·string,⏎······oldDelta:·Delta·|·null,⏎····`

Check failure on line 118 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (webkit)

Replace `text:·string,·language:·string,·oldDelta:·Delta·|·null` with `⏎······text:·string,⏎······language:·string,⏎······oldDelta:·Delta·|·null,⏎····`

Check failure on line 118 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (firefox)

Replace `text:·string,·language:·string,·oldDelta:·Delta·|·null` with `⏎······text:·string,⏎······language:·string,⏎······oldDelta:·Delta·|·null,⏎····`
forced = false,
) {
if (this.children.head == null) return;
Expand All @@ -130,7 +130,7 @@
// @ts-expect-error
return delta.concat(blockDelta(child, false));
}, new Delta());
const delta = highlight(text, language);
const delta = highlight(text, language, oldDelta);
oldDelta.diff(delta).reduce((index, { retain, attributes }) => {
// Should be all retains
if (!retain) return index;
Expand Down Expand Up @@ -288,9 +288,29 @@
}
}

highlightBlot(text: string, language = 'plain') {
highlightBlot(text: string, language = 'plain', oldDelta: Delta | null = null) {

Check failure on line 291 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (chromium)

Replace `text:·string,·language·=·'plain',·oldDelta:·Delta·|·null·=·null` with `⏎····text:·string,⏎····language·=·'plain',⏎····oldDelta:·Delta·|·null·=·null,⏎··`

Check failure on line 291 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (webkit)

Replace `text:·string,·language·=·'plain',·oldDelta:·Delta·|·null·=·null` with `⏎····text:·string,⏎····language·=·'plain',⏎····oldDelta:·Delta·|·null·=·null,⏎··`

Check failure on line 291 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (firefox)

Replace `text:·string,·language·=·'plain',·oldDelta:·Delta·|·null·=·null` with `⏎····text:·string,⏎····language·=·'plain',⏎····oldDelta:·Delta·|·null·=·null,⏎··`
language = this.languages[language] ? language : 'plain';
if (language === 'plain') {
if (oldDelta) {
// If old delta includes highlights from a different language, go through each op and change

Check failure on line 295 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (chromium)

Delete `·`

Check failure on line 295 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (webkit)

Delete `·`

Check failure on line 295 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (firefox)

Delete `·`
// that highlight to plain instead of creating new ops. Otherwise, diff may include inserts
// and some syntax highlights will not be removed
return oldDelta.ops.reduce((delta, op) => {
// Deep cloning all attributes not important here because the only thing that matters is
// the diff between old and new delta
const newOp = { ...op }

Check failure on line 301 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (chromium)

Insert `;`

Check failure on line 301 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (webkit)

Insert `;`

Check failure on line 301 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (firefox)

Insert `;`
if (op.attributes) {
newOp.attributes = { ...op.attributes }

Check failure on line 303 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (chromium)

Insert `;`

Check failure on line 303 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (webkit)

Insert `;`

Check failure on line 303 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (firefox)

Insert `;`
if (newOp.attributes[SyntaxCodeBlock.blotName]) {
newOp.attributes[SyntaxCodeBlock.blotName] = 'plain'

Check failure on line 305 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (chromium)

Insert `;`

Check failure on line 305 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (webkit)

Insert `;`

Check failure on line 305 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (firefox)

Insert `;`
}
if (newOp.attributes[CodeToken.blotName]) {
newOp.attributes[CodeToken.blotName] = null

Check failure on line 308 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (chromium)

Insert `;`

Check failure on line 308 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (webkit)

Insert `;`

Check failure on line 308 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (firefox)

Insert `;`
}
}
return delta.push(newOp)

Check failure on line 311 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (chromium)

Insert `;`

Check failure on line 311 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (webkit)

Insert `;`

Check failure on line 311 in packages/quill/src/modules/syntax.ts

View workflow job for this annotation

GitHub Actions / test / Unit Tests (firefox)

Insert `;`
}, new Delta());
}
return escapeText(text)
.split('\n')
.reduce((delta, line, i) => {
Expand Down