Skip to content

Commit

Permalink
prettify html and opml export
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci authored and tiensonqin committed Apr 3, 2023
1 parent f0a8ddf commit ab2f147
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/main/frontend/handler/export/html.cljs
@@ -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 @@ -403,7 +404,7 @@
(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 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
@@ -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
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;
};

0 comments on commit ab2f147

Please sign in to comment.