Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(export/copy-as): prettify html export #8968

Merged
merged 3 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/main/frontend/handler/export/html.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns frontend.handler.export.html
"export blocks/pages as html"
(:require [clojure.edn :as edn]
(:require ["/frontend/utils" :as utils]
[clojure.edn :as edn]
[clojure.string :as string]
[clojure.zip :as z]
[frontend.db :as db]
Expand Down Expand Up @@ -87,7 +88,10 @@

(defn- inline-link
[{:keys [url label full_text]}]
(let [href (when (= "Search" (first url)) (second url))]
(let [href (case (first url)
"Search" (second url)
"Complex" (str (:protocol (second url)) "://" (:link (second url)))
nil)]
(cond-> [:a]
href (conj {:href href})
href (concatv (mapv inline-ast->hiccup label))
Expand Down Expand Up @@ -402,8 +406,10 @@
ast*** (if-not (empty? config-for-walk-block-ast)
(util/profile :walk-block-ast (mapv (partial common/walk-block-ast config-for-walk-block-ast) ast**))
ast**)
hiccup (util/profile :block-ast->hiccup (z/root (reduce block-ast->hiccup empty-ul-hiccup ast***)))]
(h/render-html hiccup)))))
hiccup (util/profile :block-ast->hiccup (z/root (reduce block-ast->hiccup empty-ul-hiccup ast***)))
;; remove placeholder tag
hiccup* (vec (cons :ul (drop 2 hiccup)))]
(-> hiccup* h/render-html utils/prettifyXml)))))

(defn export-blocks-as-html
"options: see also `export-blocks-as-markdown`"
Expand Down
12 changes: 7 additions & 5 deletions src/main/frontend/handler/export/opml.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns frontend.handler.export.opml
"export blocks/pages as opml"
(:refer-clojure :exclude [map filter mapcat concat remove newline])
(:require [clojure.string :as string]
(:require ["/frontend/utils" :as utils]
[clojure.string :as string]
[clojure.zip :as z]
[frontend.db :as db]
[frontend.extensions.zip :as zip]
Expand Down Expand Up @@ -90,10 +91,11 @@
(let [[_ _ & body] hiccup]
(str
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
(h/render-html
[:opml {:version "2.0"}
[:head [:title title]]
(concatv [:body] body)]))))
(utils/prettifyXml
(h/render-html
[:opml {:version "2.0"}
[:head [:title title]]
(concatv [:body] body)])))))

;;; utils for construct opml hiccup (ends)

Expand Down
25 changes: 25 additions & 0 deletions src/main/frontend/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,28 @@ export const nodePath = Object.assign({}, path, {
return (orURI ? (orURI.protocol + '//') : '') + input
}
})

// https://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript
export const prettifyXml = function(sourceXml)
{
var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
var xsltDoc = new DOMParser().parseFromString([
// describes how we want to modify the XML - indent everything
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
' <xsl:strip-space elements="*"/>',
' <xsl:template match="para[content-style][not(text())]">', // change to just text() to strip space in text nodes
' <xsl:value-of select="normalize-space(.)"/>',
' </xsl:template>',
' <xsl:template match="node()|@*">',
' <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
' </xsl:template>',
' <xsl:output indent="yes"/>',
'</xsl:stylesheet>',
].join('\n'), 'application/xml');

var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);
var resultDoc = xsltProcessor.transformToDocument(xmlDoc);
var resultXml = new XMLSerializer().serializeToString(resultDoc);
return resultXml;
};