Welcome to mirror list, hosted at ThFree Co, Russian Federation.

remark_markdown_deserializer.js « services « content_editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 770de1df0d0caeb9072ba16dd453c4f9f158af34 (plain)
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
import { isString } from 'lodash';
import { render } from '~/lib/gfm';
import { createProseMirrorDocFromMdastTree } from './hast_to_prosemirror_converter';

const factorySpecs = {
  blockquote: { block: 'blockquote' },
  p: { block: 'paragraph' },
  li: { block: 'listItem', wrapTextInParagraph: true },
  ul: { block: 'bulletList' },
  ol: { block: 'orderedList' },
  h1: {
    block: 'heading',
    getAttrs: () => ({ level: 1 }),
  },
  h2: {
    block: 'heading',
    getAttrs: () => ({ level: 2 }),
  },
  h3: {
    block: 'heading',
    getAttrs: () => ({ level: 3 }),
  },
  h4: {
    block: 'heading',
    getAttrs: () => ({ level: 4 }),
  },
  h5: {
    block: 'heading',
    getAttrs: () => ({ level: 5 }),
  },
  h6: {
    block: 'heading',
    getAttrs: () => ({ level: 6 }),
  },
  pre: {
    block: 'codeBlock',
    skipChildren: true,
    getContent: ({ hastNodeText }) => hastNodeText.replace(/\n$/, ''),
    getAttrs: (hastNode) => {
      const languageClass = hastNode.children[0]?.properties.className?.[0];
      const language = isString(languageClass) ? languageClass.replace('language-', '') : null;

      return { language };
    },
  },
  hr: { inline: 'horizontalRule' },
  img: {
    inline: 'image',
    getAttrs: (hastNode) => ({
      src: hastNode.properties.src,
      title: hastNode.properties.title,
      alt: hastNode.properties.alt,
    }),
  },
  br: { inline: 'hardBreak' },
  code: { mark: 'code' },
  em: { mark: 'italic' },
  i: { mark: 'italic' },
  strong: { mark: 'bold' },
  b: { mark: 'bold' },
  a: {
    mark: 'link',
    getAttrs: (hastNode) => ({
      href: hastNode.properties.href,
      title: hastNode.properties.title,
    }),
  },
};

export default () => {
  return {
    deserialize: async ({ schema, content: markdown }) => {
      const document = await render({
        markdown,
        renderer: (tree) =>
          createProseMirrorDocFromMdastTree({
            schema,
            factorySpecs,
            tree,
            source: markdown,
          }),
      });

      return { document };
    },
  };
};