diff --git a/docs/2.x/extensions/footnotes.md b/docs/2.x/extensions/footnotes.md index c5f72a7509..36795dee11 100644 --- a/docs/2.x/extensions/footnotes.md +++ b/docs/2.x/extensions/footnotes.md @@ -59,6 +59,34 @@ Result: ``` +### Inline footnotes + +Inline footnotes (or anonymous footnotes) are easier to write, since you don't have to pick an identifier and move down to type the note. They have a slightly different syntax, with the caret placed before the opening square bracket: `^[Note]`. Inline footnotes do not support multiple paragraphs. + +Sample Markdown input: + +```markdown +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi^[Elit Malesuada Ridiculus] leo risus, porta ac consectetur ac. +``` + +Result: + +```html +

+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi1 leo risus, porta ac consectetur ac. +

+
+
+
    +
  1. +

    + Elit Malesuada Ridiculus  +

    +
  2. +
+
+``` + ## Usage Configure your `Environment` as usual and simply add the `FootnoteExtension`: @@ -74,14 +102,15 @@ use League\CommonMark\MarkdownConverter; // If you're happy with the defaults, feel free to remove them from this array $config = [ 'footnote' => [ - 'backref_class' => 'footnote-backref', - 'backref_symbol' => '↩', - 'container_add_hr' => true, - 'container_class' => 'footnotes', - 'ref_class' => 'footnote-ref', - 'ref_id_prefix' => 'fnref:', - 'footnote_class' => 'footnote', - 'footnote_id_prefix' => 'fn:', + 'backref_class' => 'footnote-backref', + 'backref_symbol' => '↩', + 'container_add_hr' => true, + 'container_class' => 'footnotes', + 'enable_inline_footnotes' => true, + 'ref_class' => 'footnote-ref', + 'ref_id_prefix' => 'fnref:', + 'footnote_class' => 'footnote', + 'footnote_id_prefix' => 'fn:', ], ]; diff --git a/src/Extension/Footnote/FootnoteExtension.php b/src/Extension/Footnote/FootnoteExtension.php index 0fa8038e22..d3daf36b24 100644 --- a/src/Extension/Footnote/FootnoteExtension.php +++ b/src/Extension/Footnote/FootnoteExtension.php @@ -44,6 +44,7 @@ public function configureSchema(ConfigurationBuilderInterface $builder): void 'backref_symbol' => Expect::string('↩'), 'container_add_hr' => Expect::bool(true), 'container_class' => Expect::string('footnotes'), + 'enable_inline_footnotes' => Expect::bool(true), 'ref_class' => Expect::string('footnote-ref'), 'ref_id_prefix' => Expect::string('fnref:'), 'footnote_class' => Expect::string('footnote'), @@ -53,8 +54,12 @@ public function configureSchema(ConfigurationBuilderInterface $builder): void public function register(EnvironmentBuilderInterface $environment): void { + if ($environment->getConfiguration()->get('footnote/enable_inline_footnotes')) { + $environment->addInlineParser(new AnonymousFootnoteRefParser(), 35); + $environment->addEventListener(DocumentParsedEvent::class, [new AnonymousFootnotesListener(), 'onDocumentParsed'], 40); + } + $environment->addBlockStartParser(new FootnoteStartParser(), 51); - $environment->addInlineParser(new AnonymousFootnoteRefParser(), 35); $environment->addInlineParser(new FootnoteRefParser(), 51); $environment->addRenderer(FootnoteContainer::class, new FootnoteContainerRenderer()); @@ -62,7 +67,6 @@ public function register(EnvironmentBuilderInterface $environment): void $environment->addRenderer(FootnoteRef::class, new FootnoteRefRenderer()); $environment->addRenderer(FootnoteBackref::class, new FootnoteBackrefRenderer()); - $environment->addEventListener(DocumentParsedEvent::class, [new AnonymousFootnotesListener(), 'onDocumentParsed'], 40); $environment->addEventListener(DocumentParsedEvent::class, [new FixOrphanedFootnotesAndRefsListener(), 'onDocumentParsed'], 30); $environment->addEventListener(DocumentParsedEvent::class, [new NumberFootnotesListener(), 'onDocumentParsed'], 20); $environment->addEventListener(DocumentParsedEvent::class, [new GatherFootnotesListener(), 'onDocumentParsed'], 10); diff --git a/tests/functional/Extension/Footnote/FootnoteExtensionTest.php b/tests/functional/Extension/Footnote/FootnoteExtensionTest.php index 38e0c0bf63..779172e445 100644 --- a/tests/functional/Extension/Footnote/FootnoteExtensionTest.php +++ b/tests/functional/Extension/Footnote/FootnoteExtensionTest.php @@ -66,15 +66,16 @@ public function testFootnotesWithCustomOptions(string $input, string $expected): { $environment = new Environment([ 'footnote' => [ - 'backref_class' => 'custom-backref', + 'backref_class' => 'custom-backref', // Ensure multiple characters are allowed (including multibyte) and special HTML characters are escaped. - 'backref_symbol' => '↩ 🦄️ <3 You', - 'container_add_hr' => false, - 'container_class' => 'custom-notes', - 'ref_class' => 'custom-ref', - 'ref_id_prefix' => 'fnref:', - 'footnote_class' => 'custom-footnote', - 'footnote_id_prefix' => 'fn:', + 'backref_symbol' => '↩ 🦄️ <3 You', + 'container_add_hr' => false, + 'container_class' => 'custom-notes', + 'enable_inline_footnotes' => false, + 'ref_class' => 'custom-ref', + 'ref_id_prefix' => 'fnref:', + 'footnote_class' => 'custom-footnote', + 'footnote_id_prefix' => 'fn:', ], ]); $environment->addExtension(new CommonMarkCoreExtension()); @@ -89,6 +90,7 @@ public static function dataProviderForTestFootnotesWithCustomOptions(): \Generat { yield ["Here[^note1]\n\n[^note1]: There", '

Here1

' . "\n" . '
  1. There ↩ 🦄️ <3 You

']; yield ["_Here_[^note1]\n\n[^note1]: **There**", '

Here1

' . "\n" . '
  1. There ↩ 🦄️ <3 You

']; + yield ['Disabled inline notes^[note1]', '

Disabled inline notes^[note1]

']; } public function testFootnotesWithEmptySymbol(): void