Skip to content

Build your personal knowledge base with Trilium Notes

fix(mermaid): keep the highlighted text of a pasted diagram

Elian Doran157cf09d4728
diff --git a/packages/ckeditor5/src/plugins/content_embed/inline_source.spec.ts b/packages/ckeditor5/src/plugins/content_embed/inline_source.spec.ts
index 03070ca05d..b5d45d8f53 100644
--- a/packages/ckeditor5/src/plugins/content_embed/inline_source.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/inline_source.spec.ts
@@ -155,7 +155,9 @@ describe("ContentEmbedInlineSource", () => {
editor.setData(`<pre><code class="language-mermaid">a</code></pre>`);
const source = getLoadedSource();
const listener = vi.fn();
+ const other = vi.fn();
const unsubscribe = source.subscribe(listener);
+ source.subscribe(other);
source.setSource("a");
expect(listener).not.toHaveBeenCalled();
@@ -163,6 +165,47 @@ describe("ContentEmbedInlineSource", () => {
unsubscribe();
source.setSource("b");
expect(listener).not.toHaveBeenCalled();
+ expect(other).toHaveBeenCalledOnce();
+ });
+
+ it("reads the text of a highlighted code block, its line breaks included", () => {
+ editor.setData(
+ `<pre><code class="language-mermaid">graph TD;<br>`
+ + `<span class="hljs-keyword">A</span>--&gt;B</code></pre>`
+ );
+
+ expect(getEmbed().getAttribute("source")).toBe("graph TD;\nA-->B");
+ });
+
+ it("saves an embed that has lost its source or its Editable flag", () => {
+ editor.setData(`<pre data-box-size="medium" data-editable="true">`
+ + `<code class="language-mermaid">a</code></pre>`);
+ editor.model.change((writer) => {
+ writer.removeAttribute("source", getEmbed());
+ writer.removeAttribute("editable", getEmbed());
+ });
+
+ expect(getLoadedSource().getSource()).toBe("");
+ expect(editor.getData()).toBe(`<pre data-box-size="medium" data-hide-title="true">`
+ + `<code class="language-mermaid"></code></pre>`);
+ });
+
+ it("makes no embed where the schema allows none", () => {
+ editor.model.schema.addChildCheck((_context, definition) =>
+ definition.name === "contentEmbed" ? false : undefined);
+ editor.setData(`<pre><code class="language-mermaid">graph TD;</code></pre>`);
+
+ expect(getModelData(editor.model, { withoutSelection: true }))
+ .not.toContain("<contentEmbed");
+ expect(loadInlineEmbed).not.toHaveBeenCalled();
+ });
+
+ it("makes no embed of any code block when the host lists no inline language", async () => {
+ const bare = await createTestEditor([ Essentials, Paragraph, Widget, ContentEmbed ]);
+ bare.setData(`<pre><code class="language-mermaid">graph TD;</code></pre>`);
+
+ expect(getModelData(bare.model, { withoutSelection: true }))
+ .not.toContain("<contentEmbed");
});
it("inserts a new embed editable and untitled, and has its content take the focus", () => {
diff --git a/packages/ckeditor5/src/plugins/content_embed/inline_source.ts b/packages/ckeditor5/src/plugins/content_embed/inline_source.ts
index 96b18ad6f4..2002533818 100644
--- a/packages/ckeditor5/src/plugins/content_embed/inline_source.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/inline_source.ts
@@ -147,9 +147,7 @@ export default class ContentEmbedInlineSource extends Plugin {
return;
}
- const source = Array.from( code.getChildren() )
- .map( child => child.is( '$text' ) ? child.data : '' )
- .join( '' );
+ const source = getViewText( code );
const embed = api.writer.createElement( 'contentEmbed', {
language,
source,
@@ -231,6 +229,21 @@ function downcastData( model: Model, embed: ModelElement, api: DowncastConversio
mapper.bindElements( embed, pre );
}
+/**
+ * The text of `element`, with a `<br>` as a line break. A code block pasted from a page can hold
+ * its highlighting as `<span>` elements.
+ */
+function getViewText( element: ViewElement ): string {
+ return Array.from( element.getChildren() )
+ .map( child => {
+ if ( child.is( '$text' ) ) {
+ return child.data;
+ }
+ return child.is( 'element', 'br' ) ? '\n' : getViewText( child as ViewElement );
+ } )
+ .join( '' );
+}
+
/** The inline language of `code`, when it is one in `contentEmbed.inlineLanguages`. */
function getInlineLanguage( editor: Editor, code: ViewElement ) {
const languages = editor.config.get( 'contentEmbed.inlineLanguages' ) ?? [];