]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/filters.js
dee5df4c28b68e3193ddf23691dd50581dc2a623
[bookstack] / resources / js / wysiwyg / filters.js
1 /**
2  * Setup a serializer filter for <br> tags to ensure they're not rendered
3  * within code blocks and that we use newlines there instead.
4  * @param {Editor} editor
5  */
6 function setupBrFilter(editor) {
7     editor.serializer.addNodeFilter('br', nodes => {
8         for (const node of nodes) {
9             if (node.parent && node.parent.name === 'code') {
10                 const newline = window.tinymce.html.Node.create('#text');
11                 newline.value = '\n';
12                 node.replace(newline);
13             }
14         }
15     });
16 }
17
18 /**
19  * Remove accidentally added pointer elements that are within the content.
20  * These could have accidentally been added via getting caught in range
21  * selection within page content.
22  * @param {Editor} editor
23  */
24 function setupPointerFilter(editor) {
25     editor.parser.addNodeFilter('div', nodes => {
26         for (const node of nodes) {
27             const id = node.attr('id') || '';
28             const nodeClass = node.attr('class') || '';
29             if (id === 'pointer' || nodeClass.includes('pointer')) {
30                 node.remove();
31             }
32         }
33     });
34 }
35
36 /**
37  * Setup global default filters for the given editor instance.
38  * @param {Editor} editor
39  */
40 export function setupFilters(editor) {
41     setupBrFilter(editor);
42     setupPointerFilter(editor);
43 }