BookStackApp / BookStack

A platform to create documentation/wiki content built with PHP & Laravel
https://www.bookstackapp.com/
MIT License
15.36k stars 1.92k forks source link

Server-side Markdown Footnote Inline rendering #2664

Closed paulcrown closed 3 years ago

paulcrown commented 3 years ago

I see as of v0.31, final Markdown for the page is rendered server-side, and the library used on the backend does have a footnotes extension, but I don't have the expertise to understand how to utilize this in BookStack.

@ssddanbrown in https://github.com/BookStackApp/BookStack/issues/1671#issuecomment-779447882_ indicated that a back-end markdown customization could be requested.

Because I had been using 'Custom HTML Head Content' to include markdown-it-footnote.min.js, all of my page Previews still render footnotes perfectly. However, all pages while not editing/Preview, simply display markdown notations in the text.

I am currently using BookStack v0.31.8.

I am requesting back-end markdown customization to allow inline footnotes, or alternatively, enlightenment as to how I should go about such myself.

Thank you.

ssddanbrown commented 3 years ago

Hi @paulcrown, This has actually now been implemented as part of #2639, But just awaiting release (Finishing up the docs/post now but may take a while).

We'll keep this open and I'll try to remember to post back here after release with an example and to get feedback on the implementation.

FYI @AlphaJack

paulcrown commented 3 years ago

Hi @paulcrown, This has actually now been implemented as part of #2639, But just awaiting release (Finishing up the docs/post now but may take a while).

@ssddanbrown Thanks, I look forward to its release.

ssddanbrown commented 3 years ago

@paulcrown @AlphaJack 21.04 is now out: https://www.bookstackapp.com/blog/bookstack-release-v21-04/

Tweaking the markdown rendering server-side is done via the logical theme system. Follow the getting started up the instructions to get setup with a functions.php file: https://github.com/BookStackApp/BookStack/blob/release/dev/docs/logical-theme-system.md. Note, this system is still in early stages to subject to change.

Heres the functions.php file code that gets footnote rendering working on my instance:

<?php

use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents;
use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Extension\Footnote\FootnoteExtension;

Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, function(ConfigurableEnvironmentInterface $environment) {
    $environment->addExtension(new FootnoteExtension());
});

I've not tested with the other front-end library footnotes so there may be differences in rendering or parsing compared to the preview but let me know how that goes.

paulcrown commented 3 years ago

21.04 is now out: https://www.bookstackapp.com/blog/bookstack-release-v21-04/

Tweaking the markdown rendering server-side is done via the logical theme system. Follow the getting started up the instructions to get setup with a functions.php file: https://github.com/BookStackApp/BookStack/blob/release/dev/docs/logical-theme-system.md. Note, this system is still in early stages to subject to change.

@ssddanbrown Thanks for all your work. I did see the release come out this week. I have updated my instance, and added a themes/footnote/functions.php with the code as you suggest. However, I was unable to get Inline rendering of footnotes to work, except in preview.

If I leave the Custom HTML Header blank, the preview screen shows no footnotes: Screenshot_2021-04-10 Editing Page Custom Header Blank The page view is without a footnote: Screenshot_2021-04-10 Custom HTML Header Blank Here is the page source to show no header: Screenshot_2021-04-10 Source Custom HTML Header Blank

If I instead, include a Custom HTML Header, then the preview is correct: Screenshot_2021-04-11 Editing Page Custom HTML Header MD-i-f But the page view, loses the footnote again. This is the same as @AlphaJack was experiencing. Screenshot_2021-04-11 Custom HTML Header MD-i-f Here I show the page source with the Custom HTML Header included: Screenshot_2021-04-11 Source HTML Header MD-i-f

I copied your latest version of function.php into theme/footnote/functions.php, but I don't see that anything has changed. Any suggestions for me, as to where to look or test next?

paulcrown commented 3 years ago

I confirmed the functions.php file was in the theme/footnotes/ directory, and that APP_THEME="footnotes" to match the name of the theme directory.

While inline footnotes were still only working in preview mode, I have discovered that if I edit, make any change, and save each page that has inline footnotes, they work now.

@ssddanbrown My problem is solved with the new theme hook, provided I make at least one edit to each page.

DanielGordonIT commented 9 months ago

Heres the functions.php file code that gets footnote rendering working on my instance:

<?php

use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents;
use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Extension\Footnote\FootnoteExtension;

Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, function(ConfigurableEnvironmentInterface $environment) {
    $environment->addExtension(new FootnoteExtension());
});

I've not tested with the other front-end library footnotes so there may be differences in rendering or parsing compared to the preview but let me know how that goes.

Testing this, I found that it no longer takes a ConfigurableEnvironmentInterface but instead just an Environment. The code to include in the functions.php should now read:

use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\Footnote\FootnoteExtension;

Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, function(Environment $environment) {
    $environment->addExtension(new FootnoteExtension());
});

Putting this here for anyone like me who found the old version of the code and realized that it didn't work.

I don't have a good way of enabling interoperability between the WYSIWYG and markdown editors for this, but I don't think it'll be too important for my use case.