-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
googledocsnormalizer.ts
54 lines (44 loc) · 1.48 KB
/
googledocsnormalizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module paste-from-office/normalizers/googledocsnormalizer
*/
import { UpcastWriter, type ViewDocument } from 'ckeditor5/src/engine.js';
import removeBoldWrapper from '../filters/removeboldwrapper.js';
import transformBlockBrsToParagraphs from '../filters/br.js';
import { unwrapParagraphInListItem } from '../filters/list.js';
import type { Normalizer, NormalizerData } from '../normalizer.js';
const googleDocsMatch = /id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;
/**
* Normalizer for the content pasted from Google Docs.
*/
export default class GoogleDocsNormalizer implements Normalizer {
public readonly document: ViewDocument;
/**
* Creates a new `GoogleDocsNormalizer` instance.
*
* @param document View document.
*/
constructor( document: ViewDocument ) {
this.document = document;
}
/**
* @inheritDoc
*/
public isActive( htmlString: string ): boolean {
return googleDocsMatch.test( htmlString );
}
/**
* @inheritDoc
*/
public execute( data: NormalizerData ): void {
const writer = new UpcastWriter( this.document );
const { body: documentFragment } = data._parsedData;
removeBoldWrapper( documentFragment, writer );
unwrapParagraphInListItem( documentFragment, writer );
transformBlockBrsToParagraphs( documentFragment, writer );
data.content = documentFragment;
}
}