1 | # CommonMark Transformer
|
2 |
|
3 | Converts markdown text to/from a DOM.
|
4 |
|
5 | ## Usage
|
6 |
|
7 | To transform markdown text, first install the `markdown-common` package:
|
8 |
|
9 | ```
|
10 | npm install @accordproject/markdown-common --save
|
11 | ```
|
12 |
|
13 | Then in your JavaScript code:
|
14 |
|
15 | ``` javascript
|
16 | const CommonMarkTransformer = require('@accordproject/markdown-common').CommonMarkTransformer;
|
17 | const transformer = new CommonMarkTransformer({ tagInfo : true });
|
18 | const json = transformer.fromMarkdown('# Heading\n\nThis is some `code`.\n\nFin.', 'json');
|
19 | console.log(JSON.stringify(json, null, 4));
|
20 | ```
|
21 |
|
22 | The output should be:
|
23 |
|
24 | ``` json
|
25 | {
|
26 | "$class": "org.accordproject.commonmark.Document",
|
27 | "xmlns": "http://commonmark.org/xml/1.0",
|
28 | "nodes": [
|
29 | {
|
30 | "$class": "org.accordproject.commonmark.Heading",
|
31 | "level": "1",
|
32 | "nodes": [
|
33 | {
|
34 | "$class": "org.accordproject.commonmark.Text",
|
35 | "text": "Heading"
|
36 | }
|
37 | ]
|
38 | },
|
39 | {
|
40 | "$class": "org.accordproject.commonmark.Paragraph",
|
41 | "nodes": [
|
42 | {
|
43 | "$class": "org.accordproject.commonmark.Text",
|
44 | "text": "This is some "
|
45 | },
|
46 | {
|
47 | "$class": "org.accordproject.commonmark.Code",
|
48 | "text": "code"
|
49 | },
|
50 | {
|
51 | "$class": "org.accordproject.commonmark.Text",
|
52 | "text": "."
|
53 | }
|
54 | ]
|
55 | },
|
56 | {
|
57 | "$class": "org.accordproject.commonmark.Paragraph",
|
58 | "nodes": [
|
59 | {
|
60 | "$class": "org.accordproject.commonmark.Text",
|
61 | "text": "Fin."
|
62 | }
|
63 | ]
|
64 | }
|
65 | ]
|
66 | }
|
67 | ```
|
68 |
|
69 | Please refer to the [schema](https://models.accordproject.org/commonmark/markdown.html) for the details of all the the nodes that you should expect in the DOM.
|
70 |
|
71 | You can then manipulate the DOM object, making any required changes:
|
72 |
|
73 | ``` javascript
|
74 | json.nodes[0].nodes[0].text = 'My New Heading';
|
75 | ```
|
76 |
|
77 | Finally converting the DOM back into a markdown string:
|
78 |
|
79 | ``` javascript
|
80 | const newMarkdown = commonMark.toMarkdown(json);
|
81 | console.log(newMarkdown);
|
82 | ```
|
83 |
|
84 | The new markdown string will be:
|
85 |
|
86 | ``` markdown
|
87 | My New Heading
|
88 | ====
|
89 |
|
90 | This is some `code`.
|
91 |
|
92 | Fin.
|
93 | ```
|
94 |
|
95 | > Note how the original H1 heading has been normalized during conversion from `#` syntax to `====` syntax. In commonmark these are equivalent.
|
96 |
|
97 | ## License <a name="license"></a>
|
98 | Accord Project source code files are made available under the Apache License, Version 2.0 (Apache-2.0), located in the LICENSE file. Accord Project documentation files are made available under the Creative Commons Attribution 4.0 International License (CC-BY-4.0), available at http://creativecommons.org/licenses/by/4.0/.
|
99 |
|
100 | © 2017-2019 Clause, Inc.
|