Skip to content

Instantly share code, notes, and snippets.

@rodebert
Forked from JayPanoz/index.js
Last active April 3, 2018 11:48
Show Gist options
  • Save rodebert/81837a2676cf2c04819a582c3eb49c13 to your computer and use it in GitHub Desktop.
Save rodebert/81837a2676cf2c04819a582c3eb49c13 to your computer and use it in GitHub Desktop.
A proof of concept script to append aria doc roles based on epub:type
"use strict";
// Get all the elements with epub:type
// Since querySelectorAll can’t be used with XHTML attributes,
// we create an array in which we’ll push elements with an epub:type attribute
// Mappings table (epub → role)
var mappings = {
"abstract":"doc-abstract",
"acknowledgments":"doc-acknowledgments",
"afterword":"doc-afterword",
"appendix":"doc-appendix",
"biblioentry":"doc-biblioentry",
"bibliography":"doc-bibliography",
"biblioref":"doc-biblioref",
"chapter":"doc-chapter",
"colophon":"doc-colophon",
"conclusion":"doc-conclusion",
"cover":"doc-cover",
"credit":"doc-credit",
"credits":"doc-credits",
"dedication":"doc-dedication",
"endnote":"doc-endnote",
"endnotes":"doc-endnotes",
"epigraph":"doc-epigraph",
"epilogue":"doc-epilogue",
"errata":"doc-errata",
"figure":"figure",
"footnote":"doc-footnote",
"foreword":"doc-foreword",
"glossary":"doc-glossary",
"glossdef":"definition",
"glossref":"doc-glossref",
"glossterm":"term",
"index":"doc-index",
"introduction":"doc-introduction",
"landmarks":"directory",
"list":"list",
"list-item":"listitem",
"noteref":"doc-noteref",
"notice":"doc-notice",
"pagebreak":"doc-pagebreak",
"page-list":"doc-pagelist",
"part":"doc-part",
"preface":"doc-preface",
"prologue":"doc-prologue",
"pullquote":"doc-pullquote",
"qna":"doc-qna",
"referrer":"doc-backlink",
"subtitle":"doc-subtitle",
"table":"table",
"table-row":"row",
"table-cell":"cell",
"tip":"doc-tip",
"toc":"doc-toc",
};
var epubTypes = [].slice.call(document.querySelectorAll("*"))
// Check if the current element has an epub:type attribute and if element hasn’t a role attribute
.filter(el => el.hasAttribute("epub:type") && !el.hasAttribute("role"));
// Init append role="" loop
epubTypes.forEach(element => {
var roles = element.getAttribute("epub:type") // Get the value for epub:type - in tagsoup HTML, should be epub\\\:type
.split(" ", 2)// Split the string (we only check 2 = 1 space separator)
.map(inflection => mappings[inflection]) // check if the value (string) is in the mappings table
.filter(el => !!el);
element.setAttribute("role", roles.shift()); // first element since role can only have one value
});
@civodulab
Copy link

Very good script.
But wouldn't that be ...
epubTypes.forEach(element... and not epubTypes.forEach(el ?

@rodebert
Copy link
Author

rodebert commented Mar 5, 2018

Thx.

@civodulab
Copy link

Thanks for the script I helped myself for my project vscode - EpubTools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment