This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Shared scripts): Add DOM manipulation fixes
- Loading branch information
Showing
2 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { after, before, create, replace, select} from "./dom" | ||
|
||
/** | ||
* Makes DOM modifications to account for issues | ||
* with the HTML generated by current version of Encoda | ||
* that this repo is using. | ||
* | ||
* Over time these fixes should go into Encoda's `html` codec. | ||
*/ | ||
export function fixes() { | ||
personNames() | ||
datePublished() | ||
} | ||
|
||
/** | ||
* Encoda encodes the names of a `Person` as | ||
* | ||
* ```html | ||
* <span itemprop="name" content="Sarel J. Fleishman"> | ||
* <span itemprop="givenName">Sarel J.</span> | ||
* <span itemprop="familyName">Fleishman</span> | ||
* </span> | ||
* ``` | ||
* | ||
* Note: the `itemprop` name with `content` ensures | ||
* conformance with GSDTT. | ||
* | ||
* So that we can style the given names and family names, | ||
* and consistent with other array properties, a better encoding would be: | ||
* | ||
* ```html | ||
* <meta itemprop="name" content="Sarel J. Fleishman"> | ||
* <ol data-itemprop="givenNames"> | ||
* <li itemprop="givenName">Sarel</li> | ||
* <li itemprop="givenName">J.</li> | ||
* </ol> | ||
* <ol data-itemprop="familyNames"> | ||
* <li itemprop="familyName">Fleishman</li> | ||
* </ol> | ||
* ``` | ||
*/ | ||
function personNames() { | ||
select('[itemtype="http://schema.org/Person"] span[itemprop=name]').forEach(span => { | ||
const name = span.getAttribute('content') | ||
const givenNames = select(span, '[itemprop=givenName]').map(item => item.textContent).join(' ') | ||
const familyNames = select(span, '[itemprop=familyName]').map(item => item.textContent).join(' ') | ||
|
||
after( | ||
span, | ||
create( | ||
'ol[data-itemprop=givenNames]', | ||
...givenNames.split(/\s+/).map(givenName => create('li[itemprop=givenName]', givenName)) | ||
), | ||
create( | ||
'ol[data-itemprop=familyNames]', | ||
...familyNames.split(/\s+/).map(familyName => create('li[itemprop=familyName]', familyName)) | ||
) | ||
) | ||
replace( | ||
span, | ||
`meta[itemprop=name][content=${name}]` | ||
) | ||
}) | ||
} | ||
|
||
|
||
/** | ||
* Encoda encodes a `CreativeWork.datePublished` as | ||
* | ||
* ```html | ||
* <span> | ||
* <meta itemprop="datePublished" content="2019-08-23"> | ||
* <time datetime="2019-08-23" itemscope="" itemtype="http://schema.org/Date">2019-08-23</time> | ||
* </span> | ||
* ``` | ||
* | ||
* So that we can style the date, a better encoding would simply be: | ||
* | ||
* ```html | ||
* <time datetime="2019-08-23" itemscope="" itemtype="http://schema.org/Date" itemprop="datePublished" >2019-08-23</time> | ||
* ``` | ||
* | ||
* Noting that for `<time>` elements the `datetime` attribute is used as the property value: | ||
* https://www.w3.org/TR/microdata/#values | ||
*/ | ||
function datePublished() { | ||
select('span > meta[itemprop="datePublished"]').forEach(meta => { | ||
const date = meta.getAttribute('content') | ||
replace( | ||
meta.parentElement as Element, | ||
create( | ||
`time[datetime=${date}][itemscope][itemtype="http://schema.org/Date"][itemprop=datePublished]`, | ||
date as string | ||
) | ||
) | ||
}) | ||
} |