League\HTMLToMarkdown\HtmlConverter::convert PHP Method

convert() public method

Loads HTML and passes to getMarkdown()
public convert ( $html ) : string
$html
return string The Markdown version of the html
    public function convert($html)
    {
        if (trim($html) === '') {
            return '';
        }
        $document = $this->createDOMDocument($html);
        // Work on the entire DOM tree (including head and body)
        if (!($root = $document->getElementsByTagName('html')->item(0))) {
            throw new \InvalidArgumentException('Invalid HTML was provided');
        }
        $rootElement = new Element($root);
        $this->convertChildren($rootElement);
        // Store the now-modified DOMDocument as a string
        $markdown = $document->saveHTML();
        $markdown = $this->sanitize($markdown);
        return $markdown;
    }

Usage Example

コード例 #1
0
ファイル: ImportController.php プロジェクト: zettlr/zettlr
 /**
  *  Parses uploaded Markdown files and displays them (does NOT insert)
  *
  *  @param   Request  $request
  *
  *  @return  mixed  Depending on validation: Redirect or Response
  */
 public function postImport(Request $request)
 {
     // Check if files have been uploaded
     $store = Storage::disk('local');
     $files = $store->files('import');
     if (count($files) <= 0) {
         return redirect('/import')->withErrors(['content' => 'Please input something to import!']);
     }
     // Now loop through all found files and extract the notes
     $notes = new Collection();
     $converter = new HtmlConverter(array('strip_tags' => true));
     foreach ($files as $file) {
         $fcontents = $store->get($file);
         if (in_array(pathinfo($file, PATHINFO_EXTENSION), $this->html_extensions)) {
             // We need to convert it to markdown first.
             $fcontents = $converter->convert($fcontents);
         }
         $notes = $notes->merge($this->retrieveNotes($fcontents, $request->suggestTags, $request->headingType));
     }
     // Now clear the directory
     $store->delete($files);
     // TODO: Check for custom elements (h2, h3, ps, etc.)
     // Now in $notes all h4s with trailing stuff should reside
     return view('imports.confirm', compact('notes'));
 }
All Usage Examples Of League\HTMLToMarkdown\HtmlConverter::convert