@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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @podlite/image
2
2
 
3
+ ## Upcoming
4
+ ## 0.0.8
5
+ - add :link attribute support
6
+ - change =Image format ( use text after image link as caption)
7
+ - add :caption support
8
+ - udpdate package
3
9
  ## 0.0.7
4
10
  - handle empty content
5
11
  ## 0.0.6
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
- var schema_1 = require("@podlite/schema");
4
- var Image = {
5
- toAst: function () { return function (node) {
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
- var content = node.content[0];
13
+ const content = node.content[0];
8
14
  if (content && typeof content !== "string" && 'location' in node && 'value' in content) {
9
- // get link and alt text
10
- var data = content.value;
11
- var altRegexp = /\s*\[([^\[\]]*)\]\s*/;
12
- var altText = (data.match(altRegexp) || [, ''])[1];
13
- var link = altText ? data.replace(altRegexp, '') : data;
14
- var props = {
15
- src: '',
16
- location: node.location,
17
- margin: node.margin
18
- };
19
- if (link) {
20
- // clear all \n or spaces
21
- props.src = link.split(/\s+/)[0];
22
- }
23
- if (altText) {
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 schema_1.mkBlockImage(props);
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.7",
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.6"
20
+ "@podlite/schema": "^0.0.7"
18
21
  },
19
22
  "devDependencies": {
20
- "@podlite/schema": "*",
21
- "podlite": "^0.0.11",
22
- "typescript": "4.2.3"
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 {mkBlockImage, mkBlockImageParams, Plugin} from '@podlite/schema'
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 link and alt text
11
- const data = content.value
12
- const altRegexp = /\s*\[([^\[\]]*)\]\s*/
13
- const altText = (data.match(altRegexp) || [,''])[1];
14
- const link = altText ? data.replace(altRegexp, '') : data
15
- const props:mkBlockImageParams = {
16
- src:'',
17
- location:node.location,
18
- margin:node.margin
19
- }
20
- if (link) {
21
- // clear all \n or spaces
22
- props.src = link.split(/\s+/)[0]
23
- }
24
- if (altText) {
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
- return mkBlockImage(props)
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 { AstTree, isValidateError, PodliteDocument, validateAst} from '@podlite/schema'
3
- import { podlite as podlite_core } from 'podlite'
4
- import Image from '../src/index'
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
- let podlite = podlite_core({ importPlugins: false }).use({ Image })
8
- let tree = podlite.parse(str)
9
- const asAst = podlite.toAst(tree)
10
- return asAst
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
- let podlite = podlite_core({ importPlugins: false }).use({ Image })
15
- let tree = podlite.parse(str)
16
- const asAst = podlite.toAst(tree)
17
- return podlite.toHtml(asAst).toString()
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
- return html.replace(/\n+/g, '')
22
- }
29
+ return html.replace(/\n+/g, "");
30
+ };
23
31
 
24
- const FIXTURES = [
25
- [`=begin pod
26
- =Image https://example.com/test.png
27
- =end pod`,
28
- "<img src=\"https://example.com/test.png\"/>",
29
- '=Image url'
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
- [`=Image test`,
33
- "<img src=\"test\"/>",
34
- '=Image url'
35
- ],
36
-
37
- [`=Image [alt text] test`,
38
- "<img src=\"test\" alt=\"alt text\"/>",
39
- '=Image [alt] url'
40
- ],
41
-
42
- [`=for Image
43
- test`,
44
- "<img src=\"test\"/>"
45
- ],
46
-
47
- ]
48
-
49
- describe("ast validator", () => {
50
- FIXTURES.map((i, idx) => test(`${i[2] || idx}`, () => {
51
- const pod = i[0]
52
- const result = parse(pod)
53
- const r = validateAst(result, 'PodliteDocument')
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
@@ -3,11 +3,15 @@
3
3
  "include": ["src/**/*"],
4
4
  "compilerOptions": {
5
5
  "outDir": "./lib",
6
- "rootDir": "./src"
6
+ "rootDir": "./src",
7
+ "baseUrl": "."
7
8
  },
8
9
  "references": [
9
10
  {
10
11
  "path": "../core"
12
+ },
13
+ {
14
+ "path": "../podlite-schema"
11
15
  }
12
16
  ]
13
17
  }