@podlite/image 0.0.7 → 0.0.9

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,13 @@
1
1
  # @podlite/image
2
2
 
3
+ ## Upcoming
4
+ ## 0.0.9
5
+ - update register plugin api
6
+ ## 0.0.8
7
+ - add :link attribute support
8
+ - change =Image format ( use text after image link as caption)
9
+ - add :caption support
10
+ - udpdate package
3
11
  ## 0.0.7
4
12
  - handle empty content
5
13
  ## 0.0.6
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- import { Plugin } from '@podlite/schema';
1
+ import { Plugin, Plugins } from '@podlite/schema';
2
2
  declare const Image: Plugin;
3
+ export declare const PluginRegister: Plugins;
3
4
  export default Image;
package/lib/index.js CHANGED
@@ -1,32 +1,66 @@
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
+ exports.PluginRegister = void 0;
7
+ const schema_1 = require("@podlite/schema");
8
+ const config_1 = __importDefault(require("pod6/built/helpers/config"));
9
+ const handlers_1 = require("pod6/built/helpers/handlers");
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
+ })),
62
+ };
63
+ exports.PluginRegister = {
64
+ Image: Image
31
65
  };
32
66
  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.9",
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.10"
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.21"
24
+ }
25
+ }
package/src/index.ts CHANGED
@@ -1,36 +1,67 @@
1
1
 
2
- import {mkBlockImage, mkBlockImageParams, Plugin} from '@podlite/schema'
3
-
2
+ import {getNodeId, mkCaption, mkImage, Plugin, Plugins, PodNode} from '@podlite/schema'
3
+ import makeAttrs from 'pod6/built/helpers/config'
4
+ import { setFn, subUse, wrapContent } from 'pod6/built/helpers/handlers'
4
5
  const Image:Plugin = {
5
- toAst: () => (node) => {
6
+ toAst: (_, processor) => (node, ctx) => {
6
7
  if (typeof node !== "string" && 'type' in node && 'content' in node && node.type === 'block') {
7
8
 
8
9
  const content = node.content[0]
9
10
  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
11
+ // get src and alt text
12
+ const lines = content.value.split('\n')
13
+ const [data, ...caption ] = lines
14
+ const captionText = caption.join('\n')
15
+ const altRegexp = /\s*((?<altText>.+)\s+)?(?<src>[^\s]+)/
16
+ const { altText, src } = (data.match(altRegexp) || {groups:{ altText: undefined, src: undefined }} ).groups
17
+ const conf = makeAttrs(node, ctx)
18
+ // make inline image
19
+ const imageSrc = conf.exists('src') ? conf.getFirstValue('src') : src
20
+ const imageAlt = conf.exists('alt') ? conf.getFirstValue('alt') : altText
21
+ const resultContent: Array<PodNode>= [ mkImage(imageSrc, imageAlt)]
22
+ // make caption
23
+ const captionContent = conf.exists('caption') ? conf.getFirstValue('caption') : captionText
24
+ if ( captionContent ) {
25
+ resultContent.push( mkCaption(processor(captionContent)) )
26
26
  }
27
- return mkBlockImage(props)
27
+
28
+ return { ...node, content:resultContent ,/* content_: node.content */}
28
29
  }
29
30
  return node
30
31
  }
31
32
 
32
33
  },
33
-
34
+ toHtml: subUse({
35
+ // inside head don't wrap into <p>
36
+ ':image' : setFn(( node, ctx ) => {
37
+ return (writer) => (node)=>{
38
+ const linkTo = ctx.link
39
+ if (linkTo) {
40
+ writer.writeRaw('<a href="')
41
+ writer.write(linkTo)
42
+ writer.writeRaw('">')
43
+ }
44
+ writer.writeRaw(`<img src="${node.src}" alt="${node.alt}"/>`)
45
+ if (linkTo) {
46
+ writer.writeRaw('</a>')
47
+ }
48
+ }
49
+ }),
50
+ 'caption': wrapContent('<div class="caption">', '</div>'),
51
+ },
52
+ setFn(( node, ctx ) => {
53
+ const {level} = node
54
+ const id = getNodeId(node, ctx)
55
+ const conf = makeAttrs(node, ctx)
56
+ if ( conf.exists('link') ) {
57
+ ctx.link = conf.getFirstValue('link')
58
+ }
59
+ return wrapContent( `<div class="image_block" ${ id ? ` id="${id}"` : ''}>`, `</div>` )
60
+ })
61
+ ),
62
+ }
63
+ export const PluginRegister:Plugins = {
64
+ Image: Image
34
65
  }
35
66
  export default Image
36
67
 
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
  }