Export pages to Markdown

Last modified by Eleni Cojocariu on 2026/06/02 17:54

cogExport a list of pages to Markdown on the file system
Type
Category
Developed by

Vincent Massol

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Export all pages under the Sandbox space to file, converting the page contents to Markdown 1.2 syntax.

Requires Programming Rights.

{{groovy}}
import org.xwiki.environment.*
import org.xwiki.model.reference.*

if (request.confirm == '1') {
  services.query.xwql("select distinct doc.fullName from Document doc where doc.space like 'Sandbox' or doc.space like 'Sandbox.%'").execute().each() {
    print "* Converting ${it} to MD..."
    def itemDoc = xwiki.getDocument(it)
    def newContent = services.rendering.render(itemDoc.getXDOM(), 'markdown/1.2')
    def tmpDir = new File(services.component.getInstance(Environment.class).temporaryDirectory, 'md-export')
    def pathSerializer = services.component.getInstance(EntityReferenceSerializer.TYPE_STRING, 'fspath')
    def outputFile = new File(tmpDir, pathSerializer.serialize(itemDoc.documentReference))
    outputFile.parentFile.mkdirs()
    outputFile << newContent
    println "Saved in ${outputFile.toString()}"
  }
}

println "[[Export>>||queryString='confirm=1']]"
{{/groovy}}

Results in the output (for ex):

Converting Sandbox.TestPage1 to MD...Saved in /var/cache/tomcat9/Catalina/localhost/xwiki/xwiki-temp/md-export/playground/Sandbox/TestPage1
Converting Sandbox.WebHome to MD...Saved in /var/cache/tomcat9/Catalina/localhost/xwiki/xwiki-temp/md-export/playground/Sandbox/WebHome
Converting Sandbox.TestPage3 to MD...Saved in /var/cache/tomcat9/Catalina/localhost/xwiki/xwiki-temp/md-export/playground/Sandbox/TestPage3
Converting Sandbox.TestPage2 to MD...Saved in /var/cache/tomcat9/Catalina/localhost/xwiki/xwiki-temp/md-export/playground/Sandbox/TestPage2
Converting Sandbox.ApplicationsPanelEntry to MD...Saved in /var/cache/tomcat9/Catalina/localhost/xwiki/xwiki-temp/md-export/playground/Sandbox/ApplicationsPanelEntry
Converting Sandbox.WebPreferences to MD...Saved in /var/cache/tomcat9/Catalina/localhost/xwiki/xwiki-temp/md-export/playground/Sandbox/WebPreferences

Get Connected