@podlite/image 0.0.7 → 0.0.8
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/CHANGELOG.md +6 -0
- package/lib/index.js +53 -22
- package/package.json +10 -10
- package/src/index.ts +50 -21
- package/t/Image.test.ts +66 -53
- package/tsconfig.json +5 -1
- package/tsconfig.tsbuildinfo +1 -1888
- package/yarn-error.log +4367 -0
package/CHANGELOG.md
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,32 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
const schema_1 = require("@podlite/schema");
|
|
7
|
+
const config_1 = __importDefault(require("pod6/built/helpers/config"));
|
|
8
|
+
const handlers_1 = require("pod6/built/helpers/handlers");
|
|
9
|
+
const Image1 = {};
|
|
10
|
+
const Image = {
|
|
11
|
+
toAst: (_, processor) => (node, ctx) => {
|
|
6
12
|
if (typeof node !== "string" && 'type' in node && 'content' in node && node.type === 'block') {
|
|
7
|
-
|
|
13
|
+
const content = node.content[0];
|
|
8
14
|
if (content && typeof content !== "string" && 'location' in node && 'value' in content) {
|
|
9
|
-
// get
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
props.alt = altText;
|
|
15
|
+
// get src and alt text
|
|
16
|
+
const lines = content.value.split('\n');
|
|
17
|
+
const [data, ...caption] = lines;
|
|
18
|
+
const captionText = caption.join('\n');
|
|
19
|
+
const altRegexp = /\s*((?<altText>.+)\s+)?(?<src>[^\s]+)/;
|
|
20
|
+
const { altText, src } = (data.match(altRegexp) || { groups: { altText: undefined, src: undefined } }).groups;
|
|
21
|
+
const conf = (0, config_1.default)(node, ctx);
|
|
22
|
+
// make inline image
|
|
23
|
+
const imageSrc = conf.exists('src') ? conf.getFirstValue('src') : src;
|
|
24
|
+
const imageAlt = conf.exists('alt') ? conf.getFirstValue('alt') : altText;
|
|
25
|
+
const resultContent = [(0, schema_1.mkImage)(imageSrc, imageAlt)];
|
|
26
|
+
// make caption
|
|
27
|
+
const captionContent = conf.exists('caption') ? conf.getFirstValue('caption') : captionText;
|
|
28
|
+
if (captionContent) {
|
|
29
|
+
resultContent.push((0, schema_1.mkCaption)(processor(captionContent)));
|
|
25
30
|
}
|
|
26
|
-
return
|
|
31
|
+
return { ...node, content: resultContent, /* content_: node.content */ };
|
|
27
32
|
}
|
|
28
33
|
return node;
|
|
29
34
|
}
|
|
30
|
-
}
|
|
35
|
+
},
|
|
36
|
+
toHtml: (0, handlers_1.subUse)({
|
|
37
|
+
// inside head don't wrap into <p>
|
|
38
|
+
':image': (0, handlers_1.setFn)((node, ctx) => {
|
|
39
|
+
return (writer) => (node) => {
|
|
40
|
+
const linkTo = ctx.link;
|
|
41
|
+
if (linkTo) {
|
|
42
|
+
writer.writeRaw('<a href="');
|
|
43
|
+
writer.write(linkTo);
|
|
44
|
+
writer.writeRaw('">');
|
|
45
|
+
}
|
|
46
|
+
writer.writeRaw(`<img src="${node.src}" alt="${node.alt}"/>`);
|
|
47
|
+
if (linkTo) {
|
|
48
|
+
writer.writeRaw('</a>');
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}),
|
|
52
|
+
'caption': (0, handlers_1.wrapContent)('<div class="caption">', '</div>'),
|
|
53
|
+
}, (0, handlers_1.setFn)((node, ctx) => {
|
|
54
|
+
const { level } = node;
|
|
55
|
+
const id = (0, schema_1.getNodeId)(node, ctx);
|
|
56
|
+
const conf = (0, config_1.default)(node, ctx);
|
|
57
|
+
if (conf.exists('link')) {
|
|
58
|
+
ctx.link = conf.getFirstValue('link');
|
|
59
|
+
}
|
|
60
|
+
return (0, handlers_1.wrapContent)(`<div class="image_block" ${id ? ` id="${id}"` : ''}>`, `</div>`);
|
|
61
|
+
})),
|
|
31
62
|
};
|
|
32
63
|
exports.default = Image;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@podlite/image",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "image",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -8,18 +8,18 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"clean": "rm -rf dist lib tsconfig.tsbuildinfo",
|
|
10
10
|
"build": "tsc",
|
|
11
|
-
"test": "jest"
|
|
11
|
+
"test": "yarn g:jest --passWithNoTests",
|
|
12
|
+
"watch:update": " yarn build && yarn test",
|
|
13
|
+
"watch": "yarn g:watch"
|
|
12
14
|
},
|
|
13
15
|
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
16
|
+
"access": "public",
|
|
17
|
+
"main": "index.js"
|
|
15
18
|
},
|
|
16
19
|
"dependencies": {
|
|
17
|
-
"@podlite/schema": "^0.0.
|
|
20
|
+
"@podlite/schema": "^0.0.7"
|
|
18
21
|
},
|
|
19
22
|
"devDependencies": {
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
},
|
|
24
|
-
"gitHead": "eaae7ef7bee8588d8dbe3abbff3a3a03e43b849e"
|
|
25
|
-
}
|
|
23
|
+
"podlite": "0.0.12"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,36 +1,65 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {getNodeId, mkCaption, mkImage, Plugin, PodNode} from '@podlite/schema'
|
|
3
|
+
import makeAttrs from 'pod6/built/helpers/config'
|
|
4
|
+
import { setFn, subUse, wrapContent } from 'pod6/built/helpers/handlers'
|
|
5
|
+
const Image1:Plugin = {}
|
|
4
6
|
const Image:Plugin = {
|
|
5
|
-
toAst: () => (node) => {
|
|
7
|
+
toAst: (_, processor) => (node, ctx) => {
|
|
6
8
|
if (typeof node !== "string" && 'type' in node && 'content' in node && node.type === 'block') {
|
|
7
9
|
|
|
8
10
|
const content = node.content[0]
|
|
9
11
|
if (content && typeof content !== "string" && 'location' in node && 'value' in content) {
|
|
10
|
-
// get
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
props.alt = altText
|
|
12
|
+
// get src and alt text
|
|
13
|
+
const lines = content.value.split('\n')
|
|
14
|
+
const [data, ...caption ] = lines
|
|
15
|
+
const captionText = caption.join('\n')
|
|
16
|
+
const altRegexp = /\s*((?<altText>.+)\s+)?(?<src>[^\s]+)/
|
|
17
|
+
const { altText, src } = (data.match(altRegexp) || {groups:{ altText: undefined, src: undefined }} ).groups
|
|
18
|
+
const conf = makeAttrs(node, ctx)
|
|
19
|
+
// make inline image
|
|
20
|
+
const imageSrc = conf.exists('src') ? conf.getFirstValue('src') : src
|
|
21
|
+
const imageAlt = conf.exists('alt') ? conf.getFirstValue('alt') : altText
|
|
22
|
+
const resultContent: Array<PodNode>= [ mkImage(imageSrc, imageAlt)]
|
|
23
|
+
// make caption
|
|
24
|
+
const captionContent = conf.exists('caption') ? conf.getFirstValue('caption') : captionText
|
|
25
|
+
if ( captionContent ) {
|
|
26
|
+
resultContent.push( mkCaption(processor(captionContent)) )
|
|
26
27
|
}
|
|
27
|
-
|
|
28
|
+
|
|
29
|
+
return { ...node, content:resultContent ,/* content_: node.content */}
|
|
28
30
|
}
|
|
29
31
|
return node
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
},
|
|
33
|
-
|
|
35
|
+
toHtml: subUse({
|
|
36
|
+
// inside head don't wrap into <p>
|
|
37
|
+
':image' : setFn(( node, ctx ) => {
|
|
38
|
+
return (writer) => (node)=>{
|
|
39
|
+
const linkTo = ctx.link
|
|
40
|
+
if (linkTo) {
|
|
41
|
+
writer.writeRaw('<a href="')
|
|
42
|
+
writer.write(linkTo)
|
|
43
|
+
writer.writeRaw('">')
|
|
44
|
+
}
|
|
45
|
+
writer.writeRaw(`<img src="${node.src}" alt="${node.alt}"/>`)
|
|
46
|
+
if (linkTo) {
|
|
47
|
+
writer.writeRaw('</a>')
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}),
|
|
51
|
+
'caption': wrapContent('<div class="caption">', '</div>'),
|
|
52
|
+
},
|
|
53
|
+
setFn(( node, ctx ) => {
|
|
54
|
+
const {level} = node
|
|
55
|
+
const id = getNodeId(node, ctx)
|
|
56
|
+
const conf = makeAttrs(node, ctx)
|
|
57
|
+
if ( conf.exists('link') ) {
|
|
58
|
+
ctx.link = conf.getFirstValue('link')
|
|
59
|
+
}
|
|
60
|
+
return wrapContent( `<div class="image_block" ${ id ? ` id="${id}"` : ''}>`, `</div>` )
|
|
61
|
+
})
|
|
62
|
+
),
|
|
34
63
|
}
|
|
35
64
|
export default Image
|
|
36
65
|
|
package/t/Image.test.ts
CHANGED
|
@@ -1,62 +1,75 @@
|
|
|
1
1
|
// import * as Image from '../src';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import {
|
|
3
|
+
AstTree,
|
|
4
|
+
getFromTree,
|
|
5
|
+
isValidateError,
|
|
6
|
+
PodliteDocument,
|
|
7
|
+
validateAst,
|
|
8
|
+
validateAstTree,
|
|
9
|
+
validatePodliteAst,
|
|
10
|
+
} from "@podlite/schema";
|
|
11
|
+
import { frozenIds, podlite as podlite_core } from "podlite";
|
|
12
|
+
import Image from "../src/index";
|
|
5
13
|
|
|
6
14
|
const parse = (str: string): PodliteDocument => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
15
|
+
let podlite = podlite_core({ importPlugins: false }).use({ Image });
|
|
16
|
+
let tree = podlite.parse(str);
|
|
17
|
+
const asAst = podlite.toAst(tree);
|
|
18
|
+
return asAst;
|
|
19
|
+
};
|
|
12
20
|
|
|
13
21
|
const parseToHtml = (str: string): string => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
22
|
+
let podlite = podlite_core({ importPlugins: false }).use({ Image });
|
|
23
|
+
let tree = podlite.parse(str);
|
|
24
|
+
const asAst = podlite.toAst(tree);
|
|
25
|
+
return podlite.toHtml(frozenIds()(asAst)).toString();
|
|
26
|
+
};
|
|
19
27
|
|
|
20
28
|
const cleanHTML = (html: string) => {
|
|
21
|
-
|
|
22
|
-
}
|
|
29
|
+
return html.replace(/\n+/g, "");
|
|
30
|
+
};
|
|
23
31
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
=
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
it("AST validate", () => {
|
|
33
|
+
const p = parse(`=Image test.png
|
|
34
|
+
short caption`);
|
|
35
|
+
const Image = getFromTree(p, "Image")[0];
|
|
36
|
+
const r = validateAstTree([Image]);
|
|
37
|
+
expect(r).toEqual([]);
|
|
38
|
+
});
|
|
39
|
+
it("=Image minimal", () => {
|
|
40
|
+
const pod = `=for Image
|
|
41
|
+
test`;
|
|
42
|
+
const html = parseToHtml(pod);
|
|
43
|
+
expect(html).toMatchInlineSnapshot(`
|
|
44
|
+
<div class="image_block"
|
|
45
|
+
id="id"
|
|
46
|
+
>
|
|
47
|
+
<img src="test"
|
|
48
|
+
alt="undefined"
|
|
49
|
+
>
|
|
50
|
+
</div>
|
|
51
|
+
`);
|
|
52
|
+
});
|
|
31
53
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const vres = isValidateError(r, result);
|
|
55
|
-
expect(r).toEqual([]);
|
|
56
|
-
}
|
|
57
|
-
))
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
describe("run parser tests", () => {
|
|
61
|
-
FIXTURES.map((i, idx) => test(`${i[2] || idx}`, () => expect(cleanHTML(parseToHtml(i[0]))).toEqual(cleanHTML(i[1]))))
|
|
62
|
-
})
|
|
54
|
+
it("Image", () => {
|
|
55
|
+
const pod = `=for Image :id<111> :alt('testAlt') :caption('testCaption') :link('testLink')
|
|
56
|
+
test.png
|
|
57
|
+
`;
|
|
58
|
+
const html = parseToHtml(pod);
|
|
59
|
+
expect(html).toMatchInlineSnapshot(`
|
|
60
|
+
<div class="image_block"
|
|
61
|
+
id="111"
|
|
62
|
+
>
|
|
63
|
+
<a href="testLink">
|
|
64
|
+
<img src="test.png"
|
|
65
|
+
alt="testAlt"
|
|
66
|
+
>
|
|
67
|
+
</a>
|
|
68
|
+
<div class="caption">
|
|
69
|
+
<p>
|
|
70
|
+
testCaption
|
|
71
|
+
</p>
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
`);
|
|
75
|
+
});
|
package/tsconfig.json
CHANGED