Bläddra i källkod

Documentation

main
Rocketsoup 1 år sedan
förälder
incheckning
c89c42f466
3 ändrade filer med 333 tillägg och 0 borttagningar
  1. 4
    0
      README.md
  2. 25
    0
      custom.md
  3. 304
    0
      markdown.md

+ 4
- 0
README.md Visa fil

@@ -0,0 +1,4 @@
1
+# Markdown Library
2
+
3
+* [Markdown syntax reference](markdown.md)
4
+* [Creating custom syntax](custom.md)

+ 25
- 0
custom.md Visa fil

@@ -0,0 +1,25 @@
1
+# Custom Syntax Modules
2
+
3
+Syntax falls into two categories: block syntax and inline syntax. Block syntax operates on one or more whole lines of content to form structures like paragraphs, lists, tables, etc. Inline syntax operates on stretches of text within a block, such as marking a word as bold or inserting a link.
4
+
5
+## Block Reader
6
+
7
+Block readers extend from `MDBlockReader`.
8
+
9
+They have a priority value that determines when the reader is tried relative to other readers. Built-in readers range from 0 to 100 but any priority value is valid. Lower values are evaluated before larger values. Generally readers for more complex syntaxes should precede simpler ones. E.g. Headers have a lower priority value because they are easy to match by their leading `#` characters. Paragraphs have a priority of 100 because they are the fallback when no other format matches.
10
+
11
+Block readers have a `readBlock` method that must be overriden. The method is passed an `MDState` object repeatedly, allowing the reader to check if a matching block is found at the current line pointer. An array of lines of markdown text are in `MDState.lines`, and the current line pointer is in `MDState.p`. If the reader does not detect a matching block starting at that line, the method should return `null` (preferably as soon as possible for good performance). If the line pointer does point to the beginning of a matching block, the `p` pointer should be positioned to the next line following the end of that block and an `MDBlock` subclass instance should be returned.
12
+
13
+A reader can also perform post-processing after the document has been parsed. This can be done by overriding the `postProcess` method. It will be passed an array of top-level document blocks. This array can be modified with `.splice` operations to add, remove, or replace top-level blocks. Blocks can also be recursed into by calling `MDBlock.visitChildren` with a function. The function will be called with every `MDBlock` or `MDSpan` by recursing into all child nodes. This gives the ability to manipulate specific nodes easily without walking the node tree manually.
14
+
15
+## Inline Reader
16
+
17
+Inline readers work differently. They extend from `MDInlineReader`. Inline parsing is done non-linearly by progressively swapping out elements of a `MDToken` array with `MDSpan` elements as patterns of tokens are found that match inline syntax structures.
18
+
19
+Inline parsing is done in two phases: tokenization and substitution. These two phases have separate priorities specified by each reader. For the substitution phase, readers can even specify more than one priority with an array so that it can be called multiple times during that phase. As with block readers, lower priority values will be evaluated before higher values. This should be used to match more complex tokens and more complex token patterns before simpler patterns that might lead to ambiguities with syntaxes that use the same characters.
20
+
21
+In the tokenize phase, each inline reader is given a chance to look for a relevant token at the beginning of a line. This is done by the `readFirstToken` method. It is passed a substring of a line of markdown, and the method should see if the string begins with a recognized token. Only tokens at the beginning of the string should be looked for. If no token is found the method should return `null`. If a token is found an `MDToken` should be returned.
22
+
23
+After a block of markdown content has been tokenized, the substitution phase begins. This phase repeatedly processes an array of `MDToken`s, allowing readers to swap out one or more tokens with `MDSpan` subclasses in place. E.g. an emphasis reader will look for an asterisk token, some number of intermediary tokens, and another closing asterisk token. It will swap all of those out with an `MDEmphasisSpan`. Once the substitution phase is complete, any remaining `MDToken`s in the array get converted to `MDTextSpan`.
24
+
25
+For inline readers with inner content (such as the previous emphasis example), the inner tokens can be converted to `MDSpan`s by calling `state.tokensToSpans()`. Alternately, if an inner markdown string needs to be converted to `MDSpan`s, call `state.inlineMarkdownToSpan` or `.inlineMarkdownToSpans` (plural).

+ 304
- 0
markdown.md Visa fil

@@ -0,0 +1,304 @@
1
+# Markdown Syntax
2
+
3
+(todo: include class names for enabling these features)
4
+
5
+## Block Formats
6
+
7
+Formats that apply to one or more lines.
8
+
9
+### Paragraph
10
+
11
+Any contiguous set of lines not formatted like one of the other formats below is treated as a paragraph. Paragraphs are separated by double blank lines.
12
+
13
+```markdown
14
+Lorem ipsum dolor
15
+sit amet.
16
+
17
+New paragraph begins.
18
+```
19
+
20
+### Headers
21
+
22
+Headers are marked with one or more leading hashes.
23
+
24
+```markdown
25
+# Header 1 has one hash
26
+
27
+## Header 2 has two hashes
28
+
29
+###### Up to header 6
30
+
31
+## Optional Matched Hashes At The End ##
32
+```
33
+
34
+Alternately, H1 and H2 can be marked with underlines.
35
+
36
+```markdown
37
+Header 1 underlined with equals
38
+===============================
39
+
40
+Header 2 with hyphens. Number of underline chars doesn't matter
41
+---
42
+```
43
+
44
+### Lists
45
+
46
+Unordered lists are bulleted with an asterisk, plus, or hyphen and space.
47
+
48
+```markdown
49
+* Item
50
++ Any of these
51
+- Work as bullets
52
+```
53
+
54
+Lists can be nested with one or more spaces below an item.
55
+
56
+```markdown
57
+* Item
58
+    * Sublist
59
+	* Sublist
60
+* Item
61
+```
62
+
63
+Ordered lists start with a number, period, and space.
64
+
65
+```markdown
66
+1. Item
67
+2. Item
68
+3. Item
69
+```
70
+
71
+Again, they can be nested.
72
+
73
+```markdown
74
+1. Item
75
+    1. Subitem
76
+	2. Subitem
77
+2. Item
78
+```
79
+
80
+The first number dictates where the numbering begins. The numbers on the other items are ignored and incremented by one.
81
+
82
+```markdown
83
+5. Rendered as a 5
84
+1. Rendered as a 6
85
+1. Rendered as a 7
86
+```
87
+
88
+### Blockquote
89
+
90
+Blockquotes enclose lengthy quoted content in an indented block.
91
+
92
+```markdown
93
+Lorem Ipsum said:
94
+
95
+> Dolor sit amet
96
+> adipiscing.
97
+```
98
+
99
+### Code Blocks
100
+
101
+Blocks of preformatted code can be enclosed in triple backticks.
102
+
103
+```markdown
104
+`​``
105
+function foo() {
106
+	return 'bar';
107
+}
108
+`​``
109
+```
110
+
111
+Alternately, lines indented by at least four spaces or a tab character will be interpreted as code blocks.
112
+
113
+```markdown
114
+    function foo() {
115
+		return 'bar';
116
+	}
117
+```
118
+
119
+### Horizontal Rule
120
+
121
+Horizontal divider lines are written as 3 or more hyphens or asterisks on a line by themselves, with or without interstitial whitespace.
122
+
123
+```markdown
124
+---
125
+
126
+***
127
+
128
+- - - -
129
+
130
+* * * * * * *
131
+```
132
+
133
+### Tables
134
+
135
+Tables consist of one header row and any number of body rows
136
+
137
+```markdown
138
+Header   | Row | Here
139
+---------|-----|-----
140
+Cell     | 123 |  xyz
141
+Next row | 543 |  abc
142
+```
143
+
144
+The pipe characters do not have to line up.
145
+
146
+Rows can optionally have leading and/or trailing pipes. This is required for a single column table.
147
+
148
+```markdown
149
+| One column |
150
+| --- |
151
+| 123 |
152
+| 234 |
153
+```
154
+
155
+Columns can be right, center, or left aligned with colons in the divider line.
156
+
157
+```markdown
158
+| Left | Center | Right |
159
+| :--- | :----: | ----: |
160
+| 1 | 2 | 3 |
161
+```
162
+
163
+### Definition List
164
+
165
+Definition lists show terms with definitions below them. Useful for glossaries.
166
+
167
+```markdown
168
+term
169
+: definition of term
170
+another
171
+: definition of another
172
+: alternate definition of another
173
+```
174
+
175
+### Abbreviation Definition
176
+
177
+Abbreviations can be defined anywhere in the document. Anywhere that abbreviation appears in the document will have a mouseover title showing the expanded definition. The definition is plaintext only (no markdown).
178
+
179
+```markdown
180
+*[ABC]: Always Be Closing
181
+```
182
+
183
+### CSS Modifiers
184
+
185
+Many block structures can be modified with CSS classes, IDs, or other simple HTML attributes inside curly braces.
186
+
187
+```markdown
188
+# Header {.mycssclass}
189
+
190
+--- {#mydividerid}
191
+
192
+| Table | {style=color:green;}
193
+|---|
194
+|123|
195
+
196
+# Multiple Modifiers {.linkclass #linkid lang=en}
197
+```
198
+
199
+## Inline Formats
200
+
201
+Formats that apply within a block to style runs of words or inject other content within text.
202
+
203
+### Strong
204
+
205
+Text can be marked as bold by enclosing it in double asterisks.
206
+
207
+```markdown
208
+Some **bold** text. Can also use __underscores__.
209
+```
210
+
211
+### Emphasis
212
+
213
+Text can be marked as italic by enclosing it in single underscores or single asterisks.
214
+
215
+```markdown
216
+Some _italic_ text. Can also use *asterisks*.
217
+```
218
+
219
+### Strikethrough
220
+
221
+Text can be stricken out using tildes, singly or in pairs.
222
+
223
+```markdown
224
+Not ~~this text~~. Not ~this text~ either.
225
+```
226
+
227
+### Code
228
+
229
+Words can be rendered in monospace font using single backticks.
230
+
231
+```markdown
232
+The name of the function is `foo()`.
233
+```
234
+
235
+### Link
236
+
237
+Linking to a document is done with marked text and a URL.
238
+
239
+```markdown
240
+Click [here](page.html).
241
+```
242
+
243
+An optional mouseover title can be added to the link after the URL.
244
+
245
+```markdown
246
+Click [here](page.html "Goes to a neat page").
247
+```
248
+
249
+Repetitive or lengthy URLs can be referenced out and defined elsewhere in the document.
250
+
251
+```markdown
252
+Click [here][foo] or [here][foo] or [even here][foo] to go to the same place.
253
+
254
+[foo]: page.html "Optional title"
255
+```
256
+
257
+Lastly, to make a link with the URL as the link text, you can use angle brackets.
258
+
259
+```markdown
260
+Go visit <page.html>.
261
+```
262
+
263
+### Image
264
+
265
+An image is similar to link syntax with an exclamation in front. Instead of clickable link text, the text is alt text for the image.
266
+
267
+```markdown
268
+![a test image](https://picsum.photos/100/75)
269
+```
270
+
271
+Images also support title attributes.
272
+
273
+```markdown
274
+![a test image](https://picsum.photos/100/75 "title text")
275
+```
276
+
277
+Images also support referenced URLs.
278
+
279
+```markdown
280
+![alt text][foo]
281
+
282
+[foo]: https://picsum.photos/100/75
283
+```
284
+
285
+Images can be made clickable by enclosing them in a link where the link text goes.
286
+
287
+```markdown
288
+[![alt text](image.jpg)](page.html)
289
+```
290
+
291
+### Footnote
292
+
293
+Footnotes can be inserted into text. The text of the footnotes is added to the bottom of the document, regardless of where they're defined.
294
+
295
+```markdown
296
+Lorem ipsum[^1] dolor sit[^2] amet.
297
+
298
+[^1]: Further information here
299
+[^2]: See _Interesting Book_, pg 213
300
+```
301
+
302
+### HTML Tags
303
+
304
+Currently all HTML tags and attributes are permitted, when markdown syntax doesn't do what you want.

Laddar…
Avbryt
Spara