]> BookStack Code Mirror - bookstack/commitdiff
support exporting WYSIWYG pages as Markdown
authorNikhil Jha <redacted>
Thu, 14 May 2020 01:34:22 +0000 (18:34 -0700)
committerNikhil Jha <redacted>
Thu, 14 May 2020 01:34:22 +0000 (18:34 -0700)
app/Entities/ExportService.php
composer.json
composer.lock

index 29df1e82d450df1342959847fd223830a460866b..1b294d8b17f0b78317375a35e5f9010770f02b5a 100644 (file)
@@ -6,6 +6,7 @@ use BookStack\Uploads\ImageService;
 use DomPDF;
 use Exception;
 use SnappyPDF;
+use League\HTMLToMarkdown\HtmlConverter;
 use Throwable;
 
 class ExportService
@@ -232,11 +233,11 @@ class ExportService
      */
     public function pageToMarkdown(Page $page)
     {
-        if (property_exists($page, 'markdown') || $page->markdown != '') {
-            return "#" . $page->name . "\n\n" . $page->markdown;
+        if (property_exists($page, 'markdown') && $page->markdown != '') {
+            return "# " . $page->name . "\n\n" . $page->markdown;
         } else {
-            // TODO: Implement this feature.
-            return "# Unimplemented Feature\nidk how to turn html into markdown";
+            $converter = new HtmlConverter();
+            return "# " . $page->name . "\n\n" . $converter->convert($page->html);
         }
     }
 
@@ -246,7 +247,7 @@ class ExportService
      */
     public function chapterToMarkdown(Chapter $chapter)
     {
-        $text = "#" . $chapter->name . "\n\n";
+        $text = "# " . $chapter->name . "\n\n";
         $text .= $chapter->description . "\n\n";
         foreach ($chapter->pages as $page) {
             $text .= $this->pageToMarkdown($page);
@@ -260,7 +261,7 @@ class ExportService
     public function bookToMarkdown(Book $book): string
     {
         $bookTree = (new BookContents($book))->getTree(false, true);
-        $text = "#" . $book->name . "\n\n";
+        $text = "# " . $book->name . "\n\n";
         foreach ($bookTree as $bookChild) {
             if ($bookChild->isA('chapter')) {
                 $text .= $this->chapterToMarkdown($bookChild);
index 80e8b0a619036d9a460452fb1fd62cadb0e99a45..68802e935ce41c6e4b53d45e8985654e7d562184 100644 (file)
@@ -24,6 +24,7 @@
         "laravel/socialite": "^4.3.2",
         "league/commonmark": "^1.4",
         "league/flysystem-aws-s3-v3": "^1.0",
+        "league/html-to-markdown": "^4.9",
         "nunomaduro/collision": "^3.0",
         "onelogin/php-saml": "^3.3",
         "predis/predis": "^1.1",
index 3ddd28e5a857542eb14c3cf77ad19e07db10eb85..7f58ffaee82dd0176019cac612f1a77ed2dcc0be 100644 (file)
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "bbe47cff4f167fd6ce7047dff4602a78",
+    "content-hash": "280a7e2fe2a6f65812594d73df2ccc0f",
     "packages": [
         {
             "name": "aws/aws-sdk-php",
             "description": "Flysystem adapter for the AWS S3 SDK v3.x",
             "time": "2020-02-23T13:31:58+00:00"
         },
+        {
+            "name": "league/html-to-markdown",
+            "version": "4.9.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/thephpleague/html-to-markdown.git",
+                "reference": "1dcd0f85de786f46a7f224a27cc3d709ddd2a68c"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/thephpleague/html-to-markdown/zipball/1dcd0f85de786f46a7f224a27cc3d709ddd2a68c",
+                "reference": "1dcd0f85de786f46a7f224a27cc3d709ddd2a68c",
+                "shasum": ""
+            },
+            "require": {
+                "ext-dom": "*",
+                "ext-xml": "*",
+                "php": ">=5.3.3"
+            },
+            "require-dev": {
+                "mikehaertl/php-shellcommand": "~1.1.0",
+                "phpunit/phpunit": "^4.8|^5.7",
+                "scrutinizer/ocular": "~1.1"
+            },
+            "bin": [
+                "bin/html-to-markdown"
+            ],
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "4.10-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "League\\HTMLToMarkdown\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Colin O'Dell",
+                    "email": "colinodell@gmail.com",
+                    "homepage": "https://www.colinodell.com",
+                    "role": "Lead Developer"
+                },
+                {
+                    "name": "Nick Cernis",
+                    "email": "nick@cern.is",
+                    "homepage": "http://modernnerd.net",
+                    "role": "Original Author"
+                }
+            ],
+            "description": "An HTML-to-markdown conversion helper for PHP",
+            "homepage": "https://github.com/thephpleague/html-to-markdown",
+            "keywords": [
+                "html",
+                "markdown"
+            ],
+            "time": "2019-12-28T01:32:28+00:00"
+        },
         {
             "name": "league/oauth1-client",
             "version": "1.7.0",
Morty Proxy This is a proxified and sanitized view of the page, visit original site.