@mindful-web/embedded-media 1.0.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/LICENSE +21 -0
- package/package.json +25 -0
- package/src/constants.js +25 -0
- package/src/extract.js +11 -0
- package/src/index.js +5 -0
- package/src/tag.js +62 -0
- package/src/utils/create-attribute.js +12 -0
- package/src/utils/extract-attributes.js +20 -0
- package/src/utils/get-tag-matches.js +15 -0
- package/src/utils/tag-match-pattern.js +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Parameter1 LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mindful-web/embedded-media",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Mindful Web embedded media parsing.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"author": "Jacob Bare <jacob@parameter1.com>",
|
|
7
|
+
"repository": "https://github.com/parameter1/mindful-web/tree/main/packages/embedded-media",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"lint:fix": "yarn lint --fix",
|
|
11
|
+
"lint": "eslint --ext .js --max-warnings 5 ./",
|
|
12
|
+
"test": "yarn lint"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@mindful-web/html": "^1.0.0",
|
|
16
|
+
"@mindful-web/image": "^1.0.0",
|
|
17
|
+
"@mindful-web/inflector": "^1.0.0",
|
|
18
|
+
"cheerio": "^1.0.0-rc.12",
|
|
19
|
+
"escape-string-regexp": "^1.0.5"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"gitHead": "0b77cab713eb5841202bb86c7119949866bc68b5"
|
|
25
|
+
}
|
package/src/constants.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const TAG_ATTR_PREFIX = 'data-embed';
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
/**
|
|
5
|
+
* The ending database tag.
|
|
6
|
+
*/
|
|
7
|
+
DB_TAG_END: ']}%',
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The starting database tag.
|
|
11
|
+
*/
|
|
12
|
+
DB_TAG_START: '%{[',
|
|
13
|
+
|
|
14
|
+
TAG_ATTR_PREFIX,
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The attribute name that siginfies the tag id.
|
|
18
|
+
*/
|
|
19
|
+
TAG_ID_ATTR_NAME: `${TAG_ATTR_PREFIX}-id`,
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The attribute name that siginfies the tag type.
|
|
23
|
+
*/
|
|
24
|
+
TAG_TYPE_ATTR_NAME: `${TAG_ATTR_PREFIX}-type`,
|
|
25
|
+
};
|
package/src/extract.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const getTagMatches = require('./utils/get-tag-matches');
|
|
2
|
+
const Tag = require('./tag');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} html The HTML/text to extract embedded items from.
|
|
6
|
+
* @returns {Tag[]} An array of Tag instances.
|
|
7
|
+
*/
|
|
8
|
+
module.exports = (html) => {
|
|
9
|
+
if (!html) return [];
|
|
10
|
+
return getTagMatches(html).map((match) => new Tag(match));
|
|
11
|
+
};
|
package/src/index.js
ADDED
package/src/tag.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const escapeRegex = require('escape-string-regexp');
|
|
2
|
+
const extractAttributes = require('./utils/extract-attributes');
|
|
3
|
+
const createAttribute = require('./utils/create-attribute');
|
|
4
|
+
const {
|
|
5
|
+
DB_TAG_END,
|
|
6
|
+
DB_TAG_START,
|
|
7
|
+
} = require('./constants');
|
|
8
|
+
|
|
9
|
+
class Tag {
|
|
10
|
+
constructor(value) {
|
|
11
|
+
const attrs = extractAttributes(value);
|
|
12
|
+
this.attrs = attrs;
|
|
13
|
+
this.value = value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get id() {
|
|
17
|
+
return this.attrs.id;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get type() {
|
|
21
|
+
return this.attrs.type;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
isValid() {
|
|
25
|
+
if (this.valid === false) return false;
|
|
26
|
+
if (this.id && this.type) return true;
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
set(key, value) {
|
|
31
|
+
this.attrs[key] = value;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get(key, def = undefined) {
|
|
36
|
+
return this.attrs[key] || def;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setValid(bit = true) {
|
|
40
|
+
this.valid = Boolean(bit);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
build() {
|
|
44
|
+
const contents = Object.entries(this.attrs).reduce((arr, [key, value]) => {
|
|
45
|
+
const attr = createAttribute(key, value);
|
|
46
|
+
if (!attr) return arr;
|
|
47
|
+
arr.push(attr);
|
|
48
|
+
return arr;
|
|
49
|
+
}, []).join(' ');
|
|
50
|
+
return `${DB_TAG_START} ${contents} ${DB_TAG_END}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getRegExp(flags = 'g') {
|
|
54
|
+
return new RegExp(escapeRegex(this.value), flags);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
toString() {
|
|
58
|
+
return this.build();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = Tag;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { dasherize } = require('@mindful-web/inflector');
|
|
2
|
+
const { htmlEntities } = require('@mindful-web/html');
|
|
3
|
+
const {
|
|
4
|
+
TAG_ATTR_PREFIX,
|
|
5
|
+
} = require('../constants');
|
|
6
|
+
|
|
7
|
+
module.exports = (key, value) => {
|
|
8
|
+
if (!key || value == null) return null; // ignore null and undefined
|
|
9
|
+
const k = `${TAG_ATTR_PREFIX}-${dasherize(key)}`;
|
|
10
|
+
const v = htmlEntities.encode(value);
|
|
11
|
+
return `${k}="${v}"`;
|
|
12
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const cheerio = require('cheerio');
|
|
2
|
+
const { htmlEntities } = require('@mindful-web/html');
|
|
3
|
+
const { camelize } = require('@mindful-web/inflector');
|
|
4
|
+
const {
|
|
5
|
+
DB_TAG_END,
|
|
6
|
+
DB_TAG_START,
|
|
7
|
+
} = require('../constants');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} match The database tag match, e.g. `%{[ ... ]}%`.
|
|
11
|
+
*/
|
|
12
|
+
module.exports = (match) => {
|
|
13
|
+
const html = match.replace(DB_TAG_START, '<span').replace(DB_TAG_END, '></span>');
|
|
14
|
+
const $ = cheerio.load(html, { decodeEntities: false });
|
|
15
|
+
const obj = $('span').data() || {};
|
|
16
|
+
return Object.keys(obj).reduce((o, k) => {
|
|
17
|
+
const key = camelize(k.replace('embed', ''));
|
|
18
|
+
return ({ ...o, [key]: htmlEntities.decode(String(obj[k])) });
|
|
19
|
+
}, {});
|
|
20
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const pattern = require('./tag-match-pattern');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} html The HTML to extract embedded tags from.
|
|
5
|
+
* @returns {string[]} An array of matched embedded tag strings.
|
|
6
|
+
*/
|
|
7
|
+
module.exports = (html) => {
|
|
8
|
+
const matches = [];
|
|
9
|
+
let match;
|
|
10
|
+
do {
|
|
11
|
+
match = pattern.exec(html);
|
|
12
|
+
if (match && match[0]) matches.push(match[0]);
|
|
13
|
+
} while (match);
|
|
14
|
+
return matches;
|
|
15
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const escapeRegex = require('escape-string-regexp');
|
|
2
|
+
const {
|
|
3
|
+
DB_TAG_END,
|
|
4
|
+
DB_TAG_START,
|
|
5
|
+
TAG_TYPE_ATTR_NAME,
|
|
6
|
+
} = require('../constants');
|
|
7
|
+
|
|
8
|
+
const start = escapeRegex(DB_TAG_START);
|
|
9
|
+
const end = escapeRegex(DB_TAG_END);
|
|
10
|
+
const type = escapeRegex(TAG_TYPE_ATTR_NAME);
|
|
11
|
+
module.exports = new RegExp(`${start}(?=\\s${type}=).+?(?<=\\s)${end}`, 'gi');
|