@peaceroad/markdown-it-figure-with-p-caption 0.5.2 → 0.6.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/test/test.js CHANGED
@@ -1,96 +1,158 @@
1
- const assert = require('assert');
2
- const fs = require('fs');
3
- const md = require('markdown-it')({ html: true });
4
- const mdOneImage = require('markdown-it')({ html: true });
5
- const mdWithoutCaption = require('markdown-it')({ html: true });
6
- const mdMultipleImages = require('markdown-it')({ html: true });
1
+ import assert from 'assert'
2
+ import fs from 'fs'
3
+ import path from 'path'
4
+ import mdit from 'markdown-it'
7
5
 
8
- const mdFigureWithPCaption = require('../index.js');
6
+ import mdFigureWithPCaption from '../index.js'
7
+ import attrs from 'markdown-it-attrs'
9
8
 
10
- const attrs = require('../node_modules/markdown-it-attrs');
11
-
12
- md.use(mdFigureWithPCaption, {
9
+ let opt = {
13
10
  dquoteFilename: true,
14
11
  strongFilename: true,
15
12
  oneImageWithoutCaption: false,
16
- hasNumClass: true,
17
- });
13
+ iframeWithoutCaption: false,
14
+ videoWithoutCaption: false,
15
+ hasNumClass: false,
16
+ }
18
17
 
19
- mdOneImage.use(mdFigureWithPCaption, {
20
- dquoteFilename: true,
21
- strongFilename: true,
22
- oneImageWithoutCaption: true,
23
- hasNumClass: true,
24
- }).use(attrs);
18
+ const md = mdit({ html: true }).use(mdFigureWithPCaption, opt).use(attrs);
25
19
 
26
- mdWithoutCaption.use(mdFigureWithPCaption, {
27
- dquoteFilename: true,
28
- strongFilename: true,
29
- oneImageWithoutCaption: true,
30
- iframeWithoutCaption: true,
31
- hasNumClass: true,
32
- }).use(attrs);
20
+ opt.hasNumClass = true
21
+ const mdHasNumClass = mdit({ html: true }).use(mdFigureWithPCaption, opt).use(attrs);
33
22
 
34
- mdMultipleImages.use(attrs).use(mdFigureWithPCaption, {
35
- dquoteFilename: true,
36
- strongFilename: true,
37
- oneImageWithoutCaption: true,
38
- iframeWithoutCaption: true,
39
- hasNumClass: true,
40
- multipleImages: true,
41
- })
42
-
43
- const example = __dirname + '/examples.txt';
44
- const mdPath = __dirname + '/examples.md';
45
- const exampleCont = fs.readFileSync(example, 'utf-8').trim();
46
- let ms = [];
47
- let ms0 = exampleCont.split(/\n*\[Markdown\]\n/);
48
- let n = 1;
49
- while(n < ms0.length) {
50
- let mhs = ms0[n].split(/\n+\[HTML[^\]]*?\]\n/);
51
- let i = 1;
52
- while (i < 2) {
53
- if (mhs[i] === undefined) {
54
- mhs[i] = '';
55
- } else {
56
- mhs[i] = mhs[i].replace(/$/,'\n');
23
+ opt.hasNumClass = false
24
+ opt.oneImageWithoutCaption = true
25
+ const mdOneImage = mdit({ html: true }).use(mdFigureWithPCaption, opt).use(attrs);
26
+
27
+ opt.iframeWithoutCaption = true
28
+ opt.hasNumClass = false
29
+ const mdIframeWithoutCaption = mdit({ html: true }).use(mdFigureWithPCaption, opt).use(attrs);
30
+
31
+ opt.multipleImages = true
32
+ const mdMultipleImages = mdit({ html: true }).use(mdFigureWithPCaption, opt).use(attrs)
33
+
34
+ opt.videoWithoutCaption = true
35
+ const mdVideoWithoutCaption = mdit({ html: true }).use(mdFigureWithPCaption, opt).use(attrs)
36
+
37
+
38
+ let __dirname = path.dirname(new URL(import.meta.url).pathname)
39
+ const isWindows = (process.platform === 'win32')
40
+ if (isWindows) {
41
+ __dirname = __dirname.replace(/^\/+/, '').replace(/\//g, '\\')
42
+ }
43
+
44
+
45
+ const testData = {
46
+ noOption: __dirname + path.sep + 'examples-no-option.txt',
47
+ hasNumClass: __dirname + path.sep + 'examples-has-num-class.txt',
48
+ oneImageWithoutCaption: __dirname + path.sep + 'examples-one-image-without-caption.txt',
49
+ iframeWithoutCaption: __dirname + path.sep + 'examples-iframe-without-caption.txt',
50
+ multipleImages: __dirname + path.sep + 'examples-multiple-images.txt',
51
+ videoWithoutCaption: __dirname + path.sep + 'examples-video-without-caption.txt',
52
+ mdAllOption: __dirname + path.sep + 'examples-all-option.txt',
53
+ imgAltCaption: __dirname + path.sep + 'examples-img-alt-caption.txt',
54
+ imgTitleCaption: __dirname + path.sep + 'examples-img-title-caption.txt',
55
+ }
56
+
57
+ const getTestData = (pat) => {
58
+ let ms = [];
59
+ if(!fs.existsSync(pat)) {
60
+ console.log('No exist: ' + pat)
61
+ return ms
62
+ }
63
+ const exampleCont = fs.readFileSync(pat, 'utf-8').trim();
64
+
65
+ let ms0 = exampleCont.split(/\n*\[Markdown\]\n/);
66
+ let n = 1;
67
+ while(n < ms0.length) {
68
+ let mhs = ms0[n].split(/\n+\[HTML[^\]]*?\]\n/);
69
+ let i = 1;
70
+ while (i < 2) {
71
+ if (mhs[i] === undefined) {
72
+ mhs[i] = '';
73
+ } else {
74
+ mhs[i] = mhs[i].replace(/$/,'\n');
75
+ }
76
+ i++;
57
77
  }
58
- i++;
78
+ ms[n] = {
79
+ "markdown": mhs[0],
80
+ "html": mhs[1],
81
+ };
82
+ n++;
59
83
  }
60
- ms[n] = {
61
- "markdown": mhs[0],
62
- "html": mhs[1],
63
- };
64
- n++;
84
+ return ms
65
85
  }
66
86
 
67
- n = 1;
68
- let pass = true
69
- while(n < ms.length) {
70
- //if (n !== 37) { n++; continue };
71
- //console.log(ms[n].markdown);
72
-
73
- const m = ms[n].markdown;
74
- let h = ''
75
- if (n > 44) {
76
- h = mdMultipleImages.render(m);
77
- } else if (n > 37) {
78
- h = mdWithoutCaption.render(m);
79
- } else if (n > 20) {
80
- h = mdOneImage.render(m);
81
- } else {
82
- h = md.render(m);
87
+ const runTest = (process, pat, pass, testId) => {
88
+ console.log('===========================================================')
89
+ console.log(pat)
90
+ let ms = getTestData(pat)
91
+ if (ms.length === 0) return
92
+ let n = 1;
93
+ let end = ms.length - 1
94
+ if(testId) {
95
+ if (testId[0]) n = testId[0]
96
+ if (testId[1]) {
97
+ if (ms.length >= testId[1]) {
98
+ end = testId[1]
99
+ }
100
+ }
83
101
  }
102
+ //console.log(n, end)
84
103
 
85
- try {
86
- assert.strictEqual(h, ms[n].html);
87
- } catch(e) {
88
- pass =false
104
+ while(n <= end) {
105
+
106
+ if (!ms[n]
107
+ // || n != 3
108
+ ) {
109
+ n++
110
+ continue
111
+ }
112
+
113
+ const m = ms[n].markdown;
114
+ const h = process.render(m)
89
115
  console.log('Test: ' + n + ' >>>');
90
- console.log('incorrect: ');
91
- console.log('H: ' + h +'C: ' + ms[n].html);
92
- };
93
- n++;
116
+ try {
117
+ assert.strictEqual(h, ms[n].html);
118
+ } catch(e) {
119
+ pass = false
120
+ //console.log('Test: ' + n + ' >>>');
121
+ //console.log(opt);
122
+ console.log(ms[n].markdown);
123
+ console.log('incorrect:');
124
+ console.log('H: ' + h +'C: ' + ms[n].html);
125
+ }
126
+ n++;
127
+ }
128
+ return pass
94
129
  }
95
130
 
131
+ let pass = true
132
+ pass = runTest(md, testData.noOption, pass)
133
+ pass = runTest(mdHasNumClass, testData.hasNumClass, pass)
134
+ pass = runTest(mdOneImage, testData.oneImageWithoutCaption, pass)
135
+ pass = runTest(mdIframeWithoutCaption, testData.iframeWithoutCaption, pass)
136
+ pass = runTest(mdMultipleImages, testData.multipleImages, pass)
137
+ pass = runTest(mdVideoWithoutCaption, testData.videoWithoutCaption, pass)
138
+
139
+
140
+ opt.oneImageWithoutCaption = false
141
+
142
+ opt.imgAltCaption = 'Figure'
143
+ const mdImgAltCaption = mdit({html: true}).use(mdFigureWithPCaption, opt).use(attrs)
144
+ pass = runTest(mdImgAltCaption, testData.imgAltCaption, pass, [1, 5])
145
+ opt.imgAltCaption = '図'
146
+ const mdImgAltCaptionJa = mdit({html: true}).use(mdFigureWithPCaption, opt).use(attrs)
147
+ pass = runTest(mdImgAltCaptionJa, testData.imgAltCaption, pass, [6 , 99])
148
+
149
+ opt.imgAltCaption = false
150
+
151
+ opt.imgTitleCaption = 'Figure'
152
+ const mdImgTitleCaption = mdit({html: true}).use(mdFigureWithPCaption, opt).use(attrs)
153
+ pass = runTest(mdImgTitleCaption, testData.imgTitleCaption, pass, [1, 6])
154
+ opt.imgTitleCaption = '図'
155
+ const mdImgTitleCaptionJa = mdit({html: true}).use(mdFigureWithPCaption, opt).use(attrs)
156
+ pass = runTest(mdImgTitleCaptionJa, testData.imgTitleCaption, pass, [7, 99])
157
+
96
158
  if (pass) console.log('Passed all test.')