Skip to content

Commit

Permalink
Override content used during conversion (#3)
Browse files Browse the repository at this point in the history
Believe there is a use case for this with one of the 8fold client-sites - merging and looking forward to more feedback and possibly submitting back to CommonMark

* initial

* sunset convertedContent - use convert() instead

* Update Markdown.php
  • Loading branch information
joshbruce authored Oct 16, 2021
1 parent b45d73d commit 62a00e7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Eightfold\Markdown;

use League\CommonMark\MarkdownConverterInterface;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Output\RenderedContentInterface;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension;
use League\CommonMark\MarkdownConverter;

class Markdown
class Markdown implements MarkdownConverterInterface
{
use FluentApi;
use ExtensionsCommonMark;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function content(): string
)->getContent();
}

public function convertToHtml(): RenderedContentInterface
public function convertToHtml(string $content = ''): RenderedContentInterface
{
$environment = new Environment($this->configuration());
$environment->addExtension(new CommonMarkCoreExtension());
Expand All @@ -69,12 +70,24 @@ public function convertToHtml(): RenderedContentInterface
}

$converter = new MarkdownConverter($environment);

if (strlen($content) > 0) {
return $converter->convertToHtml($content);
}
return $converter->convertToHtml($this->content());
}

public function convertedContent(): string
/**
* @deprecated Use `convert()` instead.
*/
public function convertedContent(string $content = ''): string
{
return $this->convert($content);
}

public function convert(string $content = ''): string
{
$html = $this->convertToHtml()->getContent();
$html = $this->convertToHtml($content)->getContent();

if ($this->shouldBeMinified()) {
return str_replace([
Expand All @@ -90,7 +103,7 @@ public function convertedContent(): string

public function __toString(): string
{
return $this->convertedContent();
return $this->convert();
}

/**
Expand Down
65 changes: 65 additions & 0 deletions tests/MarkdownBaselineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,71 @@

use Eightfold\Markdown\Markdown;

test('Markdown has multiple ways to approach rendering content', function() {
expect(
(string) Markdown::create('# Shortest form')
)->toBe(<<<md
<h1>Shortest form</h1>
md
);

expect(
Markdown::create('# Method call')
->convert()
)->toBe(<<<md
<h1>Method call</h1>
md
);

expect(
Markdown::create('# Standard CommonMark flow')
->convertToHtml()
->getContent()
)->toBe(<<<md
<h1>Standard CommonMark flow</h1>
md
);

// Using this instance for the next three expectations
$markdown = Markdown::create('- [ ] Short, reusable w/ extension')
->gfm();

expect(
(string) $markdown
)->toBe(<<<md
<ul>
<li><input disabled="" type="checkbox"> Short, reusable w/ extension</li>
</ul>
md
);

expect(
$markdown->convert('- [ ] Testing content override')
)->toBe(<<<md
<ul>
<li><input disabled="" type="checkbox"> Testing content override</li>
</ul>
md
);

expect(
$markdown
->convertToHtml('- [ ] Testing content override, using ComonMark standard')
->getContent()
)->toBe(<<<md
<ul>
<li><input disabled="" type="checkbox"> Testing content override, using ComonMark standard</li>
</ul>
md
);
});

test('Markdown is stringable', function() {
$markdown = <<<md
[.8fold](eightfold)
Expand Down

0 comments on commit 62a00e7

Please sign in to comment.