md-processor 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Markdown.js CHANGED
@@ -26,6 +26,33 @@ class Markdown {
26
26
  return this.blocks.find(block => block.header === header)
27
27
  }
28
28
 
29
+ getAll (header) {
30
+ const root = this.get(header)
31
+
32
+ if (!root) {
33
+ return []
34
+ }
35
+
36
+ const matches = []
37
+ let index = this.blocks.indexOf(root)
38
+
39
+ for (;;) {
40
+ matches.push(this.blocks[index])
41
+
42
+ const next = this.blocks[++index]
43
+
44
+ if (!next) {
45
+ break
46
+ }
47
+
48
+ if (next.level <= root.level) {
49
+ break
50
+ }
51
+ }
52
+
53
+ return matches
54
+ }
55
+
29
56
  parse (content) {
30
57
  const lines = content.split('\n')
31
58
 
@@ -35,7 +62,7 @@ class Markdown {
35
62
  if (parsed) {
36
63
  this.append({
37
64
  header: parsed[2],
38
- level: parsed[1]
65
+ level: parsed[1].length
39
66
  })
40
67
  }
41
68
 
package/lib/Processor.js CHANGED
@@ -22,15 +22,26 @@ class Processor {
22
22
  async processImports ({ content, path }) {
23
23
  const imports = new Map()
24
24
 
25
- for (const [text, header, importPath] of [...content.matchAll(parseImportRegex)]) {
26
- imports.set(text, { header, importPath })
25
+ for (const [text, args, importPath] of [...content.matchAll(parseImportRegex)]) {
26
+ const [header, ...others] = args.split(',')
27
+ const deep = others.includes('deep')
28
+
29
+ imports.set(text, { deep, header, importPath })
27
30
  }
28
31
 
29
- for (const [text, { header, importPath }] of imports) {
32
+ for (const [text, { deep, header, importPath }] of imports) {
30
33
  const resolved = resolve(dirname(path), importPath)
31
34
  const markdown = await Markdown.load(resolved)
32
35
 
33
- content = content.replaceAll(text, markdown.get(header))
36
+ let imported
37
+
38
+ if (deep) {
39
+ imported = markdown.getAll(header).join('\n')
40
+ } else {
41
+ imported = markdown.get(header)
42
+ }
43
+
44
+ content = content.replaceAll(text, imported)
34
45
  }
35
46
 
36
47
  return content
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "md-processor",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A preprocessor for markdown files",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -91,6 +91,42 @@ describe('Markdown', () => {
91
91
  })
92
92
  })
93
93
 
94
+ describe('.getAll', () => {
95
+ it('should be a method', () => {
96
+ const markdown = new Markdown()
97
+
98
+ strictEqual(typeof markdown.getAll, 'function')
99
+ })
100
+
101
+ it('should return all blocks linked to the matching header', () => {
102
+ const markdown = new Markdown()
103
+ .append({ header: 'abc' })
104
+ .append({ header: 'def', level: 1 })
105
+ .append({ header: 'def 1', level: 2 })
106
+ .append({ header: 'def 1.1', level: 3 })
107
+ .append({ header: 'def 2', level: 2 })
108
+ .append({ header: 'ghi' })
109
+
110
+ const result = markdown.getAll('def', { deep: true })
111
+
112
+ strictEqual(Array.isArray(result), true)
113
+ strictEqual(result.length, 4)
114
+ strictEqual(result[0].header, 'def')
115
+ strictEqual(result[1].header, 'def 1')
116
+ strictEqual(result[2].header, 'def 1.1')
117
+ strictEqual(result[3].header, 'def 2')
118
+ })
119
+
120
+ it('should return an empty Array no matching block was found', () => {
121
+ const markdown = new Markdown()
122
+
123
+ const result = markdown.getAll('def')
124
+
125
+ strictEqual(Array.isArray(result), true)
126
+ strictEqual(result.length, 0)
127
+ })
128
+ })
129
+
94
130
  describe('.parse', () => {
95
131
  it('should be a method', () => {
96
132
  const markdown = new Markdown()
@@ -35,5 +35,13 @@ describe('Processor', () => {
35
35
 
36
36
  strictEqual(result, examples.multiImports.content)
37
37
  })
38
+
39
+ it('should process deep imports', async () => {
40
+ const processor = new Processor()
41
+
42
+ const result = await processor.process('./test/support/import-deep.md')
43
+
44
+ strictEqual(result, examples.importDeep.content)
45
+ })
38
46
  })
39
47
  })
@@ -1,7 +1,7 @@
1
1
  const multiBlock = (() => {
2
2
  const blocks = [{
3
3
  header: 'Header 1',
4
- level: '#',
4
+ level: 1,
5
5
  lines: [
6
6
  '# Header 1',
7
7
  'First line of the body of header 1',
@@ -9,7 +9,7 @@ const multiBlock = (() => {
9
9
  ]
10
10
  }, {
11
11
  header: 'Header 1.1',
12
- level: '##',
12
+ level: 2,
13
13
  lines: [
14
14
  '## Header 1.1',
15
15
  'First line of the body of header 1.1',
@@ -31,7 +31,7 @@ const multiImports = (() => {
31
31
  ...multiBlock.blocks,
32
32
  {
33
33
  header: 'Header 2',
34
- level: '#',
34
+ level: 1,
35
35
  lines: [
36
36
  '# Header 2',
37
37
  'First line of the body of header 2',
@@ -39,7 +39,7 @@ const multiImports = (() => {
39
39
  ]
40
40
  }, {
41
41
  header: 'Header 2.1',
42
- level: '##',
42
+ level: 2,
43
43
  lines: [
44
44
  '## Header 2.1',
45
45
  'First line of the body of header 2.1',
@@ -58,7 +58,10 @@ const multiImports = (() => {
58
58
  }
59
59
  })()
60
60
 
61
+ const importDeep = multiImports
62
+
61
63
  export {
64
+ importDeep,
62
65
  multiBlock,
63
66
  multiImports
64
67
  }
@@ -0,0 +1,7 @@
1
+ @[import{Header 1,deep}](multiBlock.md)
2
+ # Header 2
3
+ First line of the body of header 2
4
+ Second line of the body of header 2
5
+ ## Header 2.1
6
+ First line of the body of header 2.1
7
+ Second line of the body of header 2.1