diff --git a/libs/ContentTypes/Markdown/CommonMarkConverter.php b/libs/ContentTypes/Markdown/CommonMarkConverter.php index 3f80bbe..d145c34 100644 --- a/libs/ContentTypes/Markdown/CommonMarkConverter.php +++ b/libs/ContentTypes/Markdown/CommonMarkConverter.php @@ -29,8 +29,7 @@ class CommonMarkConverter extends \League\CommonMark\CommonMarkConverter $config['daux']['processor_instance']->extendCommonMarkEnvironment($environment); } - $this->docParser = new DocParser($environment); - $this->htmlRenderer = new HtmlRenderer($environment); + parent::__construct($config, $environment); } protected function getLinkRenderer(Environment $environment) diff --git a/libs/ContentTypes/Markdown/LinkRenderer.php b/libs/ContentTypes/Markdown/LinkRenderer.php index b5bc7d9..38b3a07 100644 --- a/libs/ContentTypes/Markdown/LinkRenderer.php +++ b/libs/ContentTypes/Markdown/LinkRenderer.php @@ -30,53 +30,6 @@ class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterfa $this->parent = new \League\CommonMark\Inline\Renderer\LinkRenderer(); } - /** - * @param string $url - * @return Entry - * @throws LinkNotFoundException - */ - protected function resolveInternalFile($url) - { - $triedAbsolute = false; - - // Legacy absolute paths could start with - // "!" In this case we will try to find - // the file starting at the root - if ($url[0] == '!' || $url[0] == '/') { - $url = ltrim($url, '!/'); - - if ($file = DauxHelper::getFile($this->daux['tree'], $url)) { - return $file; - } - - $triedAbsolute = true; - } - - // Seems it's not an absolute path or not found, - // so we'll continue with the current folder - if ($file = DauxHelper::getFile($this->daux->getCurrentPage()->getParent(), $url)) { - return $file; - } - - // If we didn't already try it, we'll - // do a pass starting at the root - if (!$triedAbsolute && $file = DauxHelper::getFile($this->daux['tree'], $url)) { - return $file; - } - - throw new LinkNotFoundException("Could not locate file '$url'"); - } - - protected function isValidUrl($url) - { - return !empty($url) && $url[0] != '#'; - } - - protected function isExternalUrl($url) - { - return preg_match('#^(?:[a-z]+:)?//|^mailto:#', $url); - } - /** * @param AbstractInline|Link $inline * @param ElementRendererInterface $htmlRenderer @@ -102,12 +55,12 @@ class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterfa // empty urls and anchors should // not go through the url resolver - if (!$this->isValidUrl($url)) { + if (!DauxHelper::isValidUrl($url)) { return $element; } // Absolute urls, shouldn't either - if ($this->isExternalUrl($url)) { + if (DauxHelper::isExternalUrl($url)) { $element->setAttribute('class', 'Link--external'); return $element; @@ -121,16 +74,13 @@ class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterfa $foundWithHash = false; try { - $file = $this->resolveInternalFile($url); + $file = DauxHelper::resolveInternalFile($this->daux, $url); $url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl()); } catch (LinkNotFoundException $e) { - - - // For some reason, the filename could contain a # and thus the link needs to resolve to that. try { if (strlen($urlAndHash[1] ?? "") > 0) { - $file = $this->resolveInternalFile($url . '#' . $urlAndHash[1]); + $file = DauxHelper::resolveInternalFile($this->daux, $url . '#' . $urlAndHash[1]); $url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl()); $foundWithHash = true; } diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index a6a73cf..f10ea20 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -1,5 +1,6 @@ getCurrentPage()->getParent(), $url)) { + return $file; + } + + // If we didn't already try it, we'll + // do a pass starting at the root + if (!$triedAbsolute && $file = DauxHelper::getFile($config['tree'], $url)) { + return $file; + } + + throw new LinkNotFoundException("Could not locate file '$url'"); + } + + public static function isValidUrl($url) + { + return !empty($url) && $url[0] != '#'; + } + + public static function isExternalUrl($url) + { + return preg_match('#^(?:[a-z]+:)?//|^mailto:#', $url); + } } diff --git a/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php b/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php index 42217d0..b8b5257 100644 --- a/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php +++ b/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php @@ -4,6 +4,7 @@ use League\CommonMark\ElementRendererInterface; use League\CommonMark\HtmlElement; use League\CommonMark\Inline\Element\AbstractInline; use League\CommonMark\Inline\Element\Link; +use Todaymade\Daux\DauxHelper; class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer { @@ -38,7 +39,7 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer } //Internal links - $file = $this->resolveInternalFile($url); + $file = DauxHelper::resolveInternalFile($this->daux, $url); $link_props = [ 'ri:content-title' => trim(trim($this->daux['confluence']['prefix']) . ' ' . $file->getTitle()), diff --git a/libs/Format/HTML/ContentTypes/Markdown/CommonMarkConverter.php b/libs/Format/HTML/ContentTypes/Markdown/CommonMarkConverter.php index 8d2a123..7ab3e13 100644 --- a/libs/Format/HTML/ContentTypes/Markdown/CommonMarkConverter.php +++ b/libs/Format/HTML/ContentTypes/Markdown/CommonMarkConverter.php @@ -2,6 +2,7 @@ use League\CommonMark\Environment; use League\CommonMark\Block\Element as BlockElement; +use League\CommonMark\Inline\Element as InlineElement; use League\CommonMark\Event\DocumentParsedEvent; use Todaymade\Daux\Config; use Todaymade\Daux\ContentTypes\Markdown\TableOfContents; @@ -17,5 +18,7 @@ class CommonMarkConverter extends \Todaymade\Daux\ContentTypes\Markdown\CommonMa $processor = new TOC\Processor($config); $environment->addEventListener(DocumentParsedEvent::class, [$processor, 'onDocumentParsed']); $environment->addBlockRenderer(TableOfContents::class, new TOC\Renderer($config)); + + $environment->addInlineRenderer(InlineElement\Image::class, new ImageRenderer($config)); } } diff --git a/libs/Format/HTML/ContentTypes/Markdown/ImageRenderer.php b/libs/Format/HTML/ContentTypes/Markdown/ImageRenderer.php new file mode 100644 index 0000000..26ef57e --- /dev/null +++ b/libs/Format/HTML/ContentTypes/Markdown/ImageRenderer.php @@ -0,0 +1,87 @@ +daux = $daux; + $this->parent = new \League\CommonMark\Inline\Renderer\ImageRenderer(); + } + + /** + * Relative URLs can be done using either the folder with + * number prefix or the final name (with prefix stripped). + * This ensures that we always use the final name when generating. + */ + protected function getCleanUrl(Image $element) + { + $url = $element->getUrl(); + + // empty urls and anchors should + // not go through the url resolver + if (!DauxHelper::isValidUrl($url)) { + return $url; + } + + // Absolute urls, shouldn't either + if (DauxHelper::isExternalUrl($url)) { + return $url; + } + + try { + $file = DauxHelper::resolveInternalFile($this->daux, $url); + return DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl()); + } catch (LinkNotFoundException $e) { + if ($this->daux->isStatic()) { + throw $e; + } + + $element->setAttribute('class', 'Link--broken'); + } + + return $url; + } + + /** + * @param Image $inline + * @param ElementRendererInterface $htmlRenderer + * + * @return HtmlElement + */ + public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) + { + $original = $inline->getUrl(); + $inline->setUrl($this->getCleanUrl($inline)); + + return $this->parent->render($inline, $htmlRenderer); + } + + /** + * @param ConfigurationInterface $configuration + */ + public function setConfiguration(ConfigurationInterface $configuration) + { + $this->config = $configuration; + $this->parent->setConfiguration($configuration); + } +} diff --git a/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php b/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php index f4ccc67..22e7d73 100644 --- a/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php +++ b/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php @@ -56,7 +56,7 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer } try { - $file = $this->resolveInternalFile($url); + $file = DauxHelper::resolveInternalFile($this->daux, $url); $url = $file->getUrl(); } catch (LinkNotFoundException $e) { if ($this->daux->isStatic()) {