Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Clearly/PDFExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ final class PDFExporter: NSObject, WKNavigationDelegate {

let rawBody = MarkdownRenderer.renderHTML(markdown)
let htmlBody = LocalImageSupport.resolveImageSources(in: rawBody, relativeTo: documentURL)
let softWrapIndent = MarkdownSyntaxHighlighter.indentValues(from: markdown)
// Both paths use forExport: false so @media print rules (including page-break) apply
let html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>\(PreviewCSS.css(fontSize: fontSize, fontFamily: fontFamily, forExport: false))</style>
<style>\(PreviewCSS.css(fontSize: fontSize, fontFamily: fontFamily, forExport: false, softWrapIndent: softWrapIndent))</style>
</head>
<body>\(htmlBody)</body>
\(MathSupport.scriptHTML(for: htmlBody))
Expand Down
3 changes: 2 additions & 1 deletion Clearly/PreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ struct PreviewView: NSViewRepresentable {
context.coordinator.isLoadingContent = true
let rawBody = MarkdownRenderer.renderHTML(markdown, includeFrontmatter: !hideFrontmatterInPreview)
let htmlBody = LocalImageSupport.resolveImageSources(in: rawBody, relativeTo: fileURL)
let softWrapIndent = MarkdownSyntaxHighlighter.indentValues(from: markdown)
let scrollJS = """
// Track scroll fraction for position sync between editor and preview.
var _scrollTicking = false;
Expand Down Expand Up @@ -178,7 +179,7 @@ struct PreviewView: NSViewRepresentable {
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>\(PreviewCSS.css(fontSize: fontSize, fontFamily: fontFamily, bodyMaxWidth: bodyMaxWidthCSS))
<style>\(PreviewCSS.css(fontSize: fontSize, fontFamily: fontFamily, bodyMaxWidth: bodyMaxWidthCSS, softWrapIndent: softWrapIndent))
mark.clearly-find { background-color: rgba(255, 230, 0, 0.4); border-radius: 2px; padding: 0 1px; }
mark.clearly-mode-highlight { background: rgba(255, 210, 50, 0.5); border-radius: 3px; padding: 0 1px; transition: background 1.5s ease; }
mark.clearly-mode-highlight.fade { background: transparent; }
Expand Down
3 changes: 2 additions & 1 deletion Clearly/iOS/PreviewExtension/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ final class PreviewViewController: UIViewController, QLPreviewingController {
let markdownText = try String(contentsOf: url, encoding: .utf8)
let rawBody = MarkdownRenderer.renderHTML(markdownText)
let htmlBody = LocalImageSupport.resolveImageSources(in: rawBody, relativeTo: url)
let softWrapIndent = MarkdownSyntaxHighlighter.indentValues(from: markdownText)

let html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>\(PreviewCSS.css())</style>
<style>\(PreviewCSS.css(softWrapIndent: softWrapIndent))</style>
<style>
@media (max-width: 400px) {
body { font-size: 14px; padding: 10px 20px 20px; }
Expand Down
3 changes: 2 additions & 1 deletion Clearly/iOS/PreviewView_iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,15 @@ struct PreviewView_iOS: UIViewRepresentable {
context.coordinator.lastContentKey = contentKey
let rawBody = MarkdownRenderer.renderHTML(markdown, includeFrontmatter: !hideFrontmatter)
let htmlBody = LocalImageSupport.resolveImageSources(in: rawBody, relativeTo: fileURL)
let softWrapIndent = MarkdownSyntaxHighlighter.indentValues(from: markdown)

let html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>\(PreviewCSS.css(fontSize: fontSize, fontFamily: fontFamily, bodyMaxWidth: "100%"))
<style>\(PreviewCSS.css(fontSize: fontSize, fontFamily: fontFamily, bodyMaxWidth: "100%", softWrapIndent: softWrapIndent))
body {
padding-top: max(16px, env(safe-area-inset-top));
padding-right: max(20px, env(safe-area-inset-right));
Expand Down
3 changes: 2 additions & 1 deletion ClearlyQuickLook/PreviewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ class PreviewViewController: NSViewController, QLPreviewingController {
do {
let markdownText = try String(contentsOf: url, encoding: .utf8)
let htmlBody = MarkdownRenderer.renderHTML(markdownText)
let softWrapIndent = MarkdownSyntaxHighlighter.indentValues(from: markdownText)

let html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>\(PreviewCSS.css())</style>
<style>\(PreviewCSS.css(softWrapIndent: softWrapIndent))</style>
<style>
@media (max-width: 400px) {
body { font-size: 14px; padding: 10px 20px 20px; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,26 @@ public enum MarkdownRenderer {
private static func frontmatterHTML(from block: FrontmatterSupport.Block) -> String {
let sourcepos = "1:1-\(block.lineCount):1"

if block.fields.isEmpty {
if block.rawText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return "<div class=\"frontmatter-anchor\" data-sourcepos=\"\(sourcepos)\"></div>\n"
// Layout-directive keys (wrapIndent/firstLineIndent) drive rendering and
// are not content metadata, so omit them from the preview's block.
let visibleFields = block.fields.filter {
!MarkdownSyntaxHighlighter.layoutFrontmatterKeys.contains($0.key)
}

if visibleFields.isEmpty {
// Fall back to the raw <pre> only when nothing parsed as fields at all.
// A block whose only keys were layout directives renders as a silent
// anchor (no visible box, but the sourcepos mapping is preserved).
if block.fields.isEmpty,
!block.rawText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
let escapedRaw = escapeHTML(block.rawText)
return "<div class=\"frontmatter\" data-sourcepos=\"\(sourcepos)\"><pre>\(escapedRaw)</pre></div>\n"
}
let escapedRaw = escapeHTML(block.rawText)
return "<div class=\"frontmatter\" data-sourcepos=\"\(sourcepos)\"><pre>\(escapedRaw)</pre></div>\n"
return "<div class=\"frontmatter-anchor\" data-sourcepos=\"\(sourcepos)\"></div>\n"
}

var rows = ""
for field in block.fields {
for field in visibleFields {
rows += "<div class=\"frontmatter-row\"><dt>\(escapeHTML(field.key))</dt><dd>\(escapeHTML(field.value))</dd></div>"
}
return "<div class=\"frontmatter\" data-sourcepos=\"\(sourcepos)\"><dl>\(rows)</dl></div>\n"
Expand Down
Loading