/
converters.ts
340 lines (293 loc) · 9.33 KB
/
converters.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* @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 code-block/converters
*/
import type { GetCallback } from 'ckeditor5/src/utils.js';
import type { DowncastInsertEvent, Element, Model, UpcastElementEvent, UpcastTextEvent, EditingView } from 'ckeditor5/src/engine.js';
import type { CodeBlockLanguageDefinition } from './codeblockconfig.js';
import { getPropertyAssociation } from './utils.js';
/**
* A model-to-view (both editing and data) converter for the `codeBlock` element.
*
* Sample input:
*
* ```html
* <codeBlock language="javascript">foo();<softBreak></softBreak>bar();</codeBlock>
* ```
*
* Sample output (editing):
*
* ```html
* <pre data-language="JavaScript"><code class="language-javascript">foo();<br />bar();</code></pre>
* ```
*
* Sample output (data, see {@link module:code-block/converters~modelToDataViewSoftBreakInsertion}):
*
* ```html
* <pre><code class="language-javascript">foo();\nbar();</code></pre>
* ```
*
* @param languageDefs The normalized language configuration passed to the feature.
* @param useLabels When `true`, the `<pre>` element will get a `data-language` attribute with a
* human–readable label of the language. Used only in the editing.
* @returns Returns a conversion callback.
*/
export function modelToViewCodeBlockInsertion(
model: Model,
languageDefs: Array<CodeBlockLanguageDefinition>,
useLabels: boolean = false
): GetCallback<DowncastInsertEvent> {
// Language CSS classes:
//
// {
// php: 'language-php',
// python: 'language-python',
// javascript: 'js',
// ...
// }
const languagesToClasses = getPropertyAssociation( languageDefs, 'language', 'class' );
// Language labels:
//
// {
// php: 'PHP',
// python: 'Python',
// javascript: 'JavaScript',
// ...
// }
const languagesToLabels = getPropertyAssociation( languageDefs, 'language', 'label' );
return ( evt, data, conversionApi ): void => {
const { writer, mapper, consumable } = conversionApi;
if ( !consumable.consume( data.item, 'insert' ) ) {
return;
}
const codeBlockLanguage = data.item.getAttribute( 'language' ) as string;
const targetViewPosition = mapper.toViewPosition( model.createPositionBefore( data.item ) );
const preAttributes: Record<string, string> = {};
// Attributes added only in the editing view.
if ( useLabels ) {
preAttributes[ 'data-language' ] = languagesToLabels[ codeBlockLanguage ];
preAttributes.spellcheck = 'false';
}
const codeAttributes = languagesToClasses[ codeBlockLanguage ] ? {
class: languagesToClasses[ codeBlockLanguage ]
} : undefined;
const code = writer.createContainerElement( 'code', codeAttributes );
const pre = writer.createContainerElement( 'pre', preAttributes, code );
writer.insert( targetViewPosition, pre );
mapper.bindElements( data.item as Element, code );
};
}
/**
* A model-to-data view converter for the new line (`softBreak`) separator.
*
* Sample input:
*
* ```html
* <codeBlock ...>foo();<softBreak></softBreak>bar();</codeBlock>
* ```
*
* Sample output:
*
* ```html
* <pre><code ...>foo();\nbar();</code></pre>
* ```
*
* @returns Returns a conversion callback.
*/
export function modelToDataViewSoftBreakInsertion( model: Model ): GetCallback<DowncastInsertEvent> {
return ( evt, data, conversionApi ) => {
if ( data.item.parent!.name !== 'codeBlock' ) {
return;
}
const { writer, mapper, consumable } = conversionApi;
if ( !consumable.consume( data.item, 'insert' ) ) {
return;
}
const position = mapper.toViewPosition( model.createPositionBefore( data.item ) );
writer.insert( position, writer.createText( '\n' ) );
};
}
/**
* A view-to-model converter for `<pre>` with the `<code>` HTML.
*
* Sample input:
*
* ```html
* <pre><code class="language-javascript">foo();bar();</code></pre>
* ```
*
* Sample output:
*
* ```html
* <codeBlock language="javascript">foo();bar();</codeBlock>
* ```
*
* @param languageDefs The normalized language configuration passed to the feature.
* @returns Returns a conversion callback.
*/
export function dataViewToModelCodeBlockInsertion(
editingView: EditingView,
languageDefs: Array<CodeBlockLanguageDefinition>
): GetCallback<UpcastElementEvent> {
// Language names associated with CSS classes:
//
// {
// 'language-php': 'php',
// 'language-python': 'python',
// js: 'javascript',
// ...
// }
const classesToLanguages = getPropertyAssociation( languageDefs, 'class', 'language' );
const defaultLanguageName = languageDefs[ 0 ].language;
return ( evt, data, conversionApi ) => {
const viewCodeElement = data.viewItem;
const viewPreElement = viewCodeElement.parent;
if ( !viewPreElement || !viewPreElement.is( 'element', 'pre' ) ) {
return;
}
// In case of nested code blocks we don't want to convert to another code block.
if ( data.modelCursor.findAncestor( 'codeBlock' ) ) {
return;
}
const { consumable, writer } = conversionApi;
if ( !consumable.test( viewCodeElement, { name: true } ) ) {
return;
}
const codeBlock = writer.createElement( 'codeBlock' );
const viewChildClasses = [ ...viewCodeElement.getClassNames() ];
// As we're to associate each class with a model language, a lack of class (empty class) can be
// also associated with a language if the language definition was configured so. Pushing an empty
// string to make sure the association will work.
if ( !viewChildClasses.length ) {
viewChildClasses.push( '' );
}
// Figure out if any of the <code> element's class names is a valid programming
// language class. If so, use it on the model element (becomes the language of the entire block).
for ( const className of viewChildClasses ) {
const language = classesToLanguages[ className ];
if ( language ) {
writer.setAttribute( 'language', language, codeBlock );
break;
}
}
// If no language value was set, use the default language from the config.
if ( !codeBlock.hasAttribute( 'language' ) ) {
writer.setAttribute( 'language', defaultLanguageName, codeBlock );
}
conversionApi.convertChildren( viewCodeElement, codeBlock );
// Let's try to insert code block.
if ( !conversionApi.safeInsert( codeBlock, data.modelCursor ) ) {
return;
}
consumable.consume( viewCodeElement, { name: true } );
conversionApi.updateConversionResult( codeBlock, data );
};
}
/**
* A view-to-model converter for new line characters in `<pre>`.
*
* Sample input:
*
* ```html
* <pre><code class="language-javascript">foo();\nbar();</code></pre>
* ```
*
* Sample output:
*
* ```html
* <codeBlock language="javascript">foo();<softBreak></softBreak>bar();</codeBlock>
* ```
*
* @returns {Function} Returns a conversion callback.
*/
export function dataViewToModelTextNewlinesInsertion(): GetCallback<UpcastTextEvent> {
return ( evt, data, { consumable, writer } ) => {
let position = data.modelCursor;
// When node is already converted then do nothing.
if ( !consumable.test( data.viewItem ) ) {
return;
}
// When not inside `codeBlock` then do nothing.
if ( !position.findAncestor( 'codeBlock' ) ) {
return;
}
consumable.consume( data.viewItem );
const text = data.viewItem.data;
const textLines = text.split( '\n' ).map( data => writer.createText( data ) );
const lastLine = textLines[ textLines.length - 1 ];
for ( const node of textLines ) {
writer.insert( node, position );
position = position.getShiftedBy( node.offsetSize );
if ( node !== lastLine ) {
const softBreak = writer.createElement( 'softBreak' );
writer.insert( softBreak, position );
position = writer.createPositionAfter( softBreak );
}
}
data.modelRange = writer.createRange(
data.modelCursor,
position
);
data.modelCursor = position;
};
}
/**
* A view-to-model converter that handles orphan text nodes (white spaces, new lines, etc.)
* that surround `<code>` inside `<pre>`.
*
* Sample input:
*
* ```html
* // White spaces
* <pre> <code>foo()</code> </pre>
*
* // White spaces
* <pre> <code>foo()</code> </pre>
*
* // White spaces
* <pre> <code>foo()</code> </pre>
*
* // New lines
* <pre>
* <code>foo()</code>
* </pre>
*
* // Redundant text
* <pre>ABC<code>foo()</code>DEF</pre>
* ```
*
* Unified output for each case:
*
* ```html
* <codeBlock language="plaintext">foo()</codeBlock>
* ```
*
* @returns Returns a conversion callback.
*/
export function dataViewToModelOrphanNodeConsumer(): GetCallback<UpcastElementEvent> {
return ( evt, data, { consumable } ) => {
const preElement = data.viewItem;
// Don't clean up nested pre elements. Their content should stay as it is, they are not upcasted
// to code blocks.
if ( preElement.findAncestor( 'pre' ) ) {
return;
}
const preChildren = Array.from( preElement.getChildren() );
const childCodeElement = preChildren.find( node => node.is( 'element', 'code' ) );
// <code>-less <pre>. It will not upcast to code block in the model, skipping.
if ( !childCodeElement ) {
return;
}
for ( const child of preChildren ) {
if ( child === childCodeElement || !child.is( '$text' ) ) {
continue;
}
// Consuming the orphan to remove it from the input data.
// Second argument in `consumable.consume` is discarded for text nodes.
consumable.consume( child, { name: true } );
}
};
}