Skip to content

Commit

Permalink
fix(HTML): Add data-itemscope=root to top level node
Browse files Browse the repository at this point in the history
The current use for this is to scope CSS variables in Thema to the lop-level Stencila node.
  • Loading branch information
nokome committed Feb 19, 2020
1 parent 2b7a011 commit 2859e32
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/codecs/html/__file_snapshots__/article-math.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<article itemscope="" itemtype="http://schema.org/Article">
<article itemscope="" itemtype="http://schema.org/Article" data-itemscope="root">
<div itemscope="" itemtype="http://schema.org/Organization" itemprop="publisher">
<meta itemprop="name" content="Unknown">
<div itemscope="" itemtype="http://schema.org/ImageObject" itemprop="logo">
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/html/__file_snapshots__/elife-50356.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<article itemscope="" itemtype="http://schema.org/Article">
<article itemscope="" itemtype="http://schema.org/Article" data-itemscope="root">
<div itemscope="" itemtype="http://schema.org/Organization" itemprop="publisher">
<meta itemprop="name" content="Unknown">
<div itemscope="" itemtype="http://schema.org/ImageObject" itemprop="logo">
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/html/__file_snapshots__/kitchen-sink.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<article itemscope="" itemtype="http://schema.org/Article">
<article itemscope="" itemtype="http://schema.org/Article" data-itemscope="root">
<div itemscope="" itemtype="http://schema.org/Organization" itemprop="publisher">
<meta itemprop="name" content="Unknown">
<div itemscope="" itemtype="http://schema.org/ImageObject" itemprop="logo">
Expand Down
13 changes: 9 additions & 4 deletions src/codecs/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,13 @@ export class HTMLCodec extends Codec implements Codec {
}

/**
* Encode a `stencila.Node` to a `VFile` with HTML contents.
* Encode a Stencila `Node` to a `VFile` with HTML contents.
*
* @param node The `stencila.Node` to encode. Will be mutated to an `Node`.
* The attribute `data-itemscope="root"` is added to the root node
* of the encoded HTML. This attribute can be used to scope CSS variables
* to within the top-level Stencila Node.
*
* @param node The `Node` to encode.
* @returns A promise that resolves to a `VFile`
*/
public readonly encode = async (
Expand All @@ -259,12 +263,13 @@ export class HTMLCodec extends Codec implements Codec {
const nodeToEncode = isBundle
? await bundle(node)
: await toFiles(node, filePath, ['data', 'file'])
let dom: HTMLHtmlElement = encodeNode(nodeToEncode) as HTMLHtmlElement

let dom = encodeNode(nodeToEncode) as HTMLElement
dom.setAttribute('data-itemscope', 'root')

const mathjaxCss = await mathJaxFinish()

if (isStandalone) {
const nodeToEncode = isBundle ? await bundle(node) : node
const { title = 'Untitled' } = getArticleMetaData(nodeToEncode)
dom = await generateHtmlElement(
stringifyContent(title),
Expand Down

0 comments on commit 2859e32

Please sign in to comment.