' +
'ERROR: The attempt to fetch ' + url + ' failed with HTTP status ' + xhr.status + '.' +
'Check your browser\'s JavaScript console for more details.' +
'Remember that you need to serve the presentation HTML from a HTTP server.
' +
''
}
}
}
xhr.open('GET', url, false)
try {
xhr.send()
} catch (e) {
alert('Failed to get the Markdown file ' + url + '. Make sure that the presentation and the file are served by a HTTP server and the file can be found there. ' + e)
}
} else if (section.getAttribute('data-separator') || section.getAttribute('data-separator-vertical') || section.getAttribute('data-separator-notes')) {
section.outerHTML = slidify(getMarkdownFromSlide(section), {
separator: section.getAttribute('data-separator'),
verticalSeparator: section.getAttribute('data-separator-vertical'),
notesSeparator: section.getAttribute('data-separator-notes'),
attributes: getForwardedAttributes(section)
})
} else {
section.innerHTML = createMarkdownSlide(getMarkdownFromSlide(section))
}
}
}
/**
* Check if a node value has the attributes pattern.
* If yes, extract it and add that value as one or several attributes
* the the terget element.
*
* You need Cache Killer on Chrome to see the effect on any FOM transformation
* directly on refresh (F5)
* http://stackoverflow.com/questions/5690269/disabling-chrome-cache-for-website-development/7000899#answer-11786277
*/
function addAttributeInElement (node, elementTarget, separator) {
const mardownClassesInElementsRegex = new RegExp(separator, 'mg')
const mardownClassRegex = /([^"= ]+?)="([^"=]+?)"/mg
let nodeValue = node.nodeValue
let matches
let matchesClass
if ((matches = mardownClassesInElementsRegex.exec(nodeValue))) {
const classes = matches[1]
nodeValue = nodeValue.substring(0, matches.index) + nodeValue.substring(mardownClassesInElementsRegex.lastIndex)
node.nodeValue = nodeValue
while ((matchesClass = mardownClassRegex.exec(classes))) {
const name = matchesClass[1]
const value = matchesClass[2]
if (name.substr(0, 5) === 'data-' || window.whiteListAttr.indexOf(name) !== -1) { elementTarget.setAttribute(name, escapeAttrValue(value)) }
}
return true
}
return false
}
/**
* Add attributes to the parent element of a text node,
* or the element of an attribute node.
*/
function addAttributes (section, element, previousElement, separatorElementAttributes, separatorSectionAttributes) {
if (element != null && element.childNodes !== undefined && element.childNodes.length > 0) {
let previousParentElement = element
for (let i = 0; i < element.childNodes.length; i++) {
const childElement = element.childNodes[i]
if (i > 0) {
let j = i - 1
while (j >= 0) {
const aPreviousChildElement = element.childNodes[j]
if (typeof aPreviousChildElement.setAttribute === 'function' && aPreviousChildElement.tagName !== 'BR') {
previousParentElement = aPreviousChildElement
break
}
j = j - 1
}
}
let parentSection = section
if (childElement.nodeName === 'section') {
parentSection = childElement
previousParentElement = childElement
}
if (typeof childElement.setAttribute === 'function' || childElement.nodeType === Node.COMMENT_NODE) {
addAttributes(parentSection, childElement, previousParentElement, separatorElementAttributes, separatorSectionAttributes)
}
}
}
if (element.nodeType === Node.COMMENT_NODE) {
if (addAttributeInElement(element, previousElement, separatorElementAttributes) === false) {
addAttributeInElement(element, section, separatorSectionAttributes)
}
}
}
/**
* Converts any current data-markdown slides in the
* DOM to HTML.
*/
function convertSlides () {
const sections = document.querySelectorAll('[data-markdown]')
for (let i = 0, len = sections.length; i < len; i++) {
const section = sections[i]
// Only parse the same slide once
if (!section.getAttribute('data-markdown-parsed')) {
section.setAttribute('data-markdown-parsed', true)
const notes = section.querySelector('aside.notes')
let markdown = getMarkdownFromSlide(section)
markdown = markdown.replace(/</g, '<').replace(/>/g, '>')
let rendered = md.render(markdown)
rendered = preventXSS(rendered)
const result = window.postProcess(rendered)
section.innerHTML = result[0].outerHTML
addAttributes(section, section, null, section.getAttribute('data-element-attributes') ||
section.parentNode.getAttribute('data-element-attributes') ||
DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR,
section.getAttribute('data-attributes') ||
section.parentNode.getAttribute('data-attributes') ||
DEFAULT_SLIDE_ATTRIBUTES_SEPARATOR)
// If there were notes, we need to re-add them after
// having overwritten the section's HTML
if (notes) {
section.appendChild(notes)
}
}
}
}
// API
return {
initialize: function () {
processSlides()
convertSlides()
},
// TODO: Do these belong in the API?
processSlides,
convertSlides,
slidify
}
}))