import m from 'mithril' import render from './lib/mithril-node-render' import isNode from 'detect-node' import fetch from './fetch' import { tidyOptions, youtubeKey } from './constants' let tidy if (!isNode) { tidy = require('exports?tidy_html5!tidy-html5') } else { tidy = process.tidy } export function cleanMarkup (html, callback) { // fix center tags html = html.replace(/
') html = html.replace(/<\/center>/g, '
') // replace HTML non-breaking spaces with normal spaces html = html.replace(/ /g, ' ') html = fixParagraphIndent(html) html = fixDoubleSpacing(html) let cache = new Map() let completeCount = 0 function getYoutubeInfo (ids) { fetch('https://www.googleapis.com/youtube/v3/videos?id=' + ids + '&part=snippet&maxResults=50&key=' + youtubeKey, (raw, type) => { let data try { data = JSON.parse(raw).items } catch (e) { } data.forEach((video) => { cache.set(video.id, video.snippet) completeCount++ }) if (completeCount === cache.size) { continueParsing() } }) } let matchYoutube = /', '') html = html.replace('', '') html = tidy(`\n` + html, tidyOptions) callback(html) } } export function fixDoubleSpacing (html) { // from FimFictionConverter by Nyerguds html = html.replace(/\s\s+/g, ' ') // push spaces to the closed side of tags html = html.replace(/\s+(<[a-z][^>]*>)\s+/g, ' $1') html = html.replace(/\s+(<\/[a-z][^>]*>)\s+/g, '$1 ') return html } export function fixParagraphIndent (html) { // from FimFictionConverter by Nyerguds// from FimFictionConverter by Nyerguds let fixIndent = 2 if (fixIndent > 0) { // only trigger indenting when finding as many whitespace characters in a row as indicated by the FixIndent setting. // Add indented class, with the search keeping into account that there could be opening tags behind the p tag. html = html.replace(new RegExp('((<([^>]+)>)*)\\s{' + fixIndent + '}\\s*', 'g'), '
$1') html = html.replace(new RegExp('
((<([^>]+)>)*)\\s{' + fixIndent + '}\\s*', 'g'), '
$3') // Cleanup of remaining start whitespace in already indented paragraphs: html = html.replace(/
]*)>((<[^>]+>)*)\\s+/g, '
$2') } return html }