Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #202 from adobe/wrong-paragraph
Browse files Browse the repository at this point in the history
Section and subitems type is wrong for paragraph with a link
  • Loading branch information
trieloff authored Mar 8, 2019
2 parents 9f325b6 + 2f58fd9 commit b4edf8b
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 176 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ The `types` property is an array of string values that describes the type of the

- `has-<type>`: for each type of content that occurs at least once in the section, e.g. has-heading
- `has-only-<type>`: for sections that only have content of a single type, e.g. has-only-image
- `is-<type-1>-<type-2>-<type3>`, `is-<type-1>-<type-2>`, and `is-<type-1>` for the top 3 most frequent types of children in the section. For instance a gallery with a heading and description would be `is-image-paragraph-heading`. You can infer additional types using [`utils.types`](#infer-content-types-with-utilstypes).
- `is-<type-1>-<type-2>-<type3>`, `is-<type-1>-<type-2>`, and `is-<type-1>` for the top 3 most frequent types of children in the section. For instance a gallery with a heading and description would be `is-image-text-heading`. You can infer additional types using [`utils.types`](#infer-content-types-with-utilstypes).
- `nb-<type>-<occurences>`: number of occurences of each type in the section

Each section has additional content-derived metadata properties, in particular:
Expand Down Expand Up @@ -579,8 +579,8 @@ Step 5 (diff only):
+ ],
+ "title": "Hello World",
+ "types": [
+ "has-paragraph",
+ "has-only-paragraph"
+ "has-text",
+ "has-only-text"
+ ],
+ "intro": "Hello World",
+ "meta": {}
Expand Down
19 changes: 13 additions & 6 deletions src/html/get-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ function sectiontype(section) {
if (type === 'paragraph' && pChildren && pChildren.length > 0) {
// if child is a paragraph, check its children, it might contain an image or a list
// which are always wrapped by default.
pChildren.forEach(({ type: subType }) => {
// exclude text nodes which are default paragraph content
if (subType !== 'text') {
const mycount = mycounter[subType] || 0;
mycounter[subType] = mycount + 1;
node.data.types.push(`is-${subType}`);
pChildren.forEach((p) => {
let prefix = 'has';
if (p.type === 'text') {
// do not count "empty" paragraphs
if (p.value === '\n' || p.value === '') return;

// paragraph with type text "is" a text
prefix = 'is';
}
if (!node.data.types.includes(`${prefix}-${p.type}`)) {
node.data.types.push(`${prefix}-${p.type}`);
}
const mycount = mycounter[p.type] || 0;
mycounter[p.type] = mycount + 1;
});
}

Expand Down
42 changes: 42 additions & 0 deletions test/fixtures/sections/2images.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"title": "helix-logo\nhelix-logo",
"types": [
"has-image",
"nb-image-2",
"has-only-image"
],
"image": "./helix_logo.png",
"intro": "helix-logo\nhelix-logo",
"meta": {},
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "image",
"title": null,
"url": "./helix_logo.png",
"alt": "helix-logo"
},
{
"type": "text",
"value": "\n"
},
{
"type": "image",
"title": null,
"url": "./helix_logo.png",
"alt": "helix-logo"
}
],
"data": {
"types": [
"has-image"
]
}
}
]
}
]
2 changes: 2 additions & 0 deletions test/fixtures/sections/2images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
![helix-logo](./helix_logo.png)
![helix-logo](./helix_logo.png)
Loading

0 comments on commit b4edf8b

Please sign in to comment.