1/*
2 * fragment.ts
3 *
4 * Copyright (C) 2021 by RStudio, PBC
5 *
6 * Unless you have received this program directly from RStudio pursuant
7 * to the terms of a commercial license agreement with RStudio, then
8 * this program is licensed to you under the terms of version 3 of the
9 * GNU Affero General Public License. This program is distributed WITHOUT
10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13 *
14 */
15
16import { Fragment } from 'prosemirror-model';
17
18export function fragmentText(fragment: Fragment, unemoji = false) {
19  let text = '';
20  fragment.forEach(node => {
21    const emjojiMark = node.marks.find(mark => mark.type === node.type.schema.marks.emoji);
22    if (unemoji && emjojiMark) {
23      return (text = text + (emjojiMark.attrs.emojihint || node.textContent));
24    } else {
25      return (text = text + node.textContent);
26    }
27  });
28  return text;
29}
30