 			guard let me = self else { return }
 			me.parseContent()
 		}
-	}
-	
+		parseContent()
+	}
+	
+	/// parses the parsed document and loads it via javascript
 	private func parseContent() {
-		guard let curDoc = context.value?.currentDocument.value,
-			let newText = curDoc.currentContents
-		else {
+		guard !parseInProgress else { return }
+		guard let curDoc = context.value?.parsedDocument.value else {
 			outputView?.loadHTMLString("", baseURL: nil)
 			return
 		}
-		let content = """
-			document.getElementById('foobar').innerText = <b>blow me</b>;
-		"""
-		outputView?.evaluateJavaScript(content, completionHandler: nil)
-		// TODO: send file directory as baseURL
-		//outputView.loadHTMLString(newText, baseURL: nil)
+		parseInProgress = true
+		defer { parseInProgress = false }
+		var html = ""
+		let allChunks = curDoc.chunks
+		for (chunkNumber, chunk) in allChunks.enumerated() {
+			html += "<section index=\"\(chunkNumber)\">\n"
+			html += htmlFor(chunk: chunk)
+			html += "\n</section>\n"
+		}
+		html = escapeForJavascript(html)
+		let command = "document.getElementsByTagName('body')[0].innerHTML = '\(html)'; MathJax.Hub.Queue([\"Typeset\",MathJax.Hub]);"
+		outputView?.evaluateJavaScript(command, completionHandler: nil)
+	}
+	
+	private func htmlFor(chunk: RmdChunk) -> String {
+		switch chunk.chunkType {
+		case .code:
+			return htmlFor(code: chunk)
+		case .docs:
+			return htmlFor(markdown: chunk.rawText)
+		case .equation:
+			return htmlForEquation(chunk: chunk)
+		}
+	}
+	
+	private func htmlFor(markdown: String) -> String {
+		let options: DownOptions = [.sourcePos, .unsafe]
+		do {
+			return try markdown.toHTML(options)
+		} catch {
+			Log.error("Failed to convert markdown to HTML: \(error)", .app)
+		}
+		return "<p>failed to parse markdown</p>\n"
+	}
+	
+	private func htmlFor(code: RmdChunk) -> String {
+		return "<p>R code/results will go here</p>\n"
+	}
+	
+	private func htmlForEquation(chunk: RmdChunk) -> String {
+		var equationText = chunk.rawText
+		equationText = lineContiuationRegex.stringByReplacingMatches(in: equationText, range: equationText.fullNSRange, withTemplate: "")
+		equationText = openDoubleDollarRegex.stringByReplacingMatches(in: equationText, range: equationText.fullNSRange, withTemplate: "\\\\[")
+		equationText = closeDoubleDollarRegex.stringByReplacingMatches(in: equationText, range: equationText.fullNSRange, withTemplate: "\\\\]")
+		equationText = equationText.addingUnicodeEntities
+		return "\n<p>\(equationText)</p>\n"
+
 	}
 	
 	private func escapeForJavascript(_ source: String) -> String {

	func contentsEdited(range: NSRange, delta: Int) -> Bool {
		/// FIXME: implement
		// need to update the changed chunk if possible. If not, then reparse document
		return false
	}
	
	private func contextChanged() {
		contextDisposables.dispose()
		contentsDisposable?.dispose()


import Cocoa
import WebKit
import Rc2Common
import ClientCore
import SyntaxParsing
import ReactiveSwift
import Down
import MJLLogger

// code to extract a zip file with preview_support locally is commented out because of a bug where WKWebView tries to load with %20 in app support url, which fails

protocol LivePreviewOutputController {
	/// might not need to be accessible
