Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying markdown from a .md file #8

Open
bilogic opened this issue May 6, 2019 · 9 comments
Open

Displaying markdown from a .md file #8

bilogic opened this issue May 6, 2019 · 9 comments
Assignees
Labels

Comments

@bilogic
Copy link

bilogic commented May 6, 2019

Hi,

I have twig view and some markdown files which contain twig variables, i.e. {{ filename }}. What is the best (laravel) way to include the contents of the MD to be parsed as markdown in the twig view?

e.g.

{% markdown %}
How do I get the markdown contents from the file into this area dynamically?
{% endmarkdown %}

currently, my twig file is like:

<html><head></head>
<body>
<div>
{% markdown %}
# {{ title }}
- ...
- ...
{% endmarkdown %}
</div>
</body>

and my PHP code is like

return view("admin/taskview", ["title" => $p]);

I would like to separate the markdown and twig so that the twig is reusable. Thank you.

@jralph
Copy link
Owner

jralph commented May 7, 2019

Hey!

If its a file you have and want to render, I would recommend getting the file contents out and sending that to the template as a variable.

return view("admin/taskview", ["title" => $p, "file_contents" => file_get_contents($fileName)]);
<html><head></head>
<body>
<div>
{{ file_contents | markdown }}
</div>
</body>

I believe you could also use the new twig syntax of applying filters to blocks.

<html><head></head>
<body>
<div>
{% apply markdown %}
    # Some Markdown

    This is some simple markdown content.
    
    {{ moreMarkdown }}
{% endapply %}
</div>
</body>

If you have multiple variables that are not markdown, but want to create markdown and render it in the final output, the way you are doing things is probably easiest.

<html><head></head>
<body>
<div>
{% markdown %}
    # {{ title }}
    {{ some_paragraph }}

    ## Sub Heading {{ extra_text }}
{% endmarkdown %}
</div>
</body>

Was this working for you?

@bilogic
Copy link
Author

bilogic commented May 8, 2019

Hi!

I was thinking something along the lines of

<html><head></head>
<body>
<div>
{% include_markdown(markdown_filename) %}
</div>
</body>

Below was working for me, but it has a few downsides:

<html><head></head>
<body>
<div>
{% markdown %}
    # {{ title }}
    {{ some_paragraph }}

    ## Sub Heading {{ extra_text }}
{% endmarkdown %}
</div>
</body>
  1. It mixes MD with HTML, causes IDE coloring and syntax issues.
  2. It does not reuse the HTML styling etc as a result, I have to replicate them across all MD files.

Below involves reading the file contents midway through the code pipeline. It seems to me that Laravel avoids doing things this way, and instead, delaying it all the way til rendering. I'm not sure, but I think the aim is to keep memory usage low. Nonetheless, it seems to be my only option if there are no better ways.

return view("admin/taskview", ["title" => $p, "file_contents" => file_get_contents($fileName)]);

Thank you.

@jralph
Copy link
Owner

jralph commented May 8, 2019

I believe twig has a source function, maybe this could be used? I haven't tested this, but might be worth a shot!

<html><head></head>
<body>
<div>
{{ source(filename) | markdown }}
</div>
</body>

@jralph jralph self-assigned this May 8, 2019
@bilogic
Copy link
Author

bilogic commented May 9, 2019

The syntax works fine. Twig can't find the filename though, doesn't seem to state clearly in the docs https://twig.symfony.com/doc/2.x/functions/source.html, still trying. Thanks!

@jralph
Copy link
Owner

jralph commented May 9, 2019

I'm able to get the above syntax to work in tests but I haven't tried it in an actual project. Perhaps the markdown file must be declared as a twig template some how.

@bilogic
Copy link
Author

bilogic commented May 9, 2019

I'm about to trace deep into twig's code

@jralph
Copy link
Owner

jralph commented May 9, 2019

Let me know what you find out. I can create tests from that to make sure it's working in the future with future releases and so on.

If not, I could potentially add a new function like markdown_from_file or something that goes and loads the file for you.

@bilogic
Copy link
Author

bilogic commented May 9, 2019

Hey man, it works, however, I have to pass in the absolute path; relative paths did not work. Now I wonder if there are any security issues since twig files could be made editable for power users. LOL

@bilogic
Copy link
Author

bilogic commented Jun 2, 2019

Hi,

It was working until I have this markdown

[^1]: footnote 1.

It outputs this HTML

<li id="fn:1">
&lt;p&gt;Footnote 1.&amp;#160;&lt;a href="#fnref1:1" rev="footnote" class="footnote-backref"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
</li>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants