-
With Jekyll it's possible to process non markdown files if they contains a YAML front matter; IIUC this seems not possible with MKDocs, so I wonder which is the best approach for this. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 17 replies
-
That is properly supported, you could use an Other than that, yes, it's true that MkDocs only converts Markdown files to HTML and that's it. And I think you've also been implicitly mentioning the 'macros' plugin which apparently adds Jinja templating to Markdown files but not other files. But that's not something that can be solved here. |
Beta Was this translation helpful? Give feedback.
-
As an additional note, mkdocs-macros is designed to work in the markdown pages. Mkdocs-macros renders the macros during To include non markdown files, you may use the Jinja2 include directive in your markdown page. Include does render the file included (see the paragraph in mkdocs-macros.) You can used fenced blocks, if necessary:
|
Beta Was this translation helpful? Give feedback.
-
FTR I see that I guess an <?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{ site.name }} News</title>
<link href="{{ '/atom.xml' | absolute_url }}" rel="self"/>
<icon>{{ '/assets/ico/favicon.ico' | absolute_url }}</icon>
<link href="{{ '/' | absolute_url }}"/>
<updated>{{ site.time | date_to_xmlschema }}</updated>
<id>{{ '/' | absolute_url }}</id>
{% for post in site.posts limit:20 %}
<entry>
<title>{{ post.title }}</title>
<author><name>{{ post.author }}</name></author>
<link href="{{ post.url | absolute_url }}"/>
<published>{{ post.date | date_to_xmlschema }}</published>
<id>{{ post.id | absolute_url }}</id>
<content type="html">{{ post.content | xml_escape }}</content>
</entry>
{% endfor %}
</feed> This is the original Jekyll/Liquid file: given a number of post files (markdown, managed in MKDocs via blog plugin), this produces a feed entry for each. |
Beta Was this translation helpful? Give feedback.
-
@redtide For the example you are submitting, is my understanding correct that it's the output you want? You want the final page to be straight XML? I don't think that should be a problem, except that your output XML will be bracketed between other HTML for the header and footer, navigation, etc. (I assume you intend to put your XML code into a fence block?). MkDocs-Macros could do it fairly simply, if you copy your XML code/Liquid code directly into a markdown page of the MkDocs project, with the small tweaks so that it's Jinja2-compliant. What you will need, of course, is the same variables, functions and filters:
See for the gist of how to do that, within the |
Beta Was this translation helpful? Give feedback.
-
So I've come to this, though I would like to reuse as much mkdocs api as possible but haven't got how to use MkDocsConfig. Anyway... thank you all for the help, got it finally working 😃
Click to viewimport frontmatter, jinja2, mkdocs_gen_files, os, yaml
from datetime import datetime, timezone
from markdown import markdown
from mkdocs import utils
#from mkdocs.config.defaults import MkDocsConfig
#config = MkDocsConfig # TypeError: unsupported operand type(s) for +: 'Optional' and 'str'
def post_date(s):
dt = datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
dt = dt.replace(tzinfo=timezone.utc)
# return dt.strftime("%Y-%m-%dT%T%z")# without comma in tz
return dt.isoformat()
with open("mkdocs.yml") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
with mkdocs_gen_files.open("atom.xml", "r") as f:
content = f.read()
posts_dir = os.path.abspath("docs/news/posts/")
posts = []
files = [
os.path.abspath(os.path.join(posts_dir, f))
for f in os.listdir(posts_dir)
if os.path.isfile(os.path.join(posts_dir, f))
]
for f in sorted(files, key=lambda x: x, reverse=True):
with open(f) as file:
post = frontmatter.load(file)
post["date"] = post_date(post["date"])
post["id"] = "/news/posts/" + os.path.splitext(os.path.basename(f))[0]
posts.append(post)
content = jinja2.Template(content).render(
# now=utils.get_build_datetime().strftime("%Y-%m-%dT%T%z"),# without comma in tz
now=datetime.now(timezone.utc).replace(microsecond=0).isoformat(),
config=config,
markdown=markdown,
site_posts=posts
)
with mkdocs_gen_files.open("atom.xml", "w") as f:
f.write(content)
Click to view<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{ config.site_name }} news</title>
<link href="{{ config.site_url + '/atom.xml' }}" rel="self"/>
<icon>{{ config.site_url + '/assets/ico/favicon.ico' }}</icon>
<link href="{{ config.site_url }}"/>
<updated>{{ now }}</updated>
<id>{{ config.site_url }}</id>
{%- for post in site_posts[0:3] %}
<entry>
<title>{{ post.title }}</title>
<author><name>{{ post.author }}</name></author>
<link href="{{ config.site_url + post.id + '/' }}"/>
<published>{{ post.date }}</published>
<id>{{ post.id }}</id>
<content type="html">{{ markdown(post.content) }}</content>
</entry>
{%- endfor %}
</feed> |
Beta Was this translation helpful? Give feedback.
That is properly supported, you could use an
on_files
event inhooks
, but https://github.com/oprypin/mkdocs-gen-files is an even easier-to-use integration, where the script really does look like just a script but is properly integrated into a top-levelmkdocs build
command.Other than that, yes, it's true that MkDocs only converts Markdown files to HTML and that's it. And I think you've also been implicitly mentioning the 'macros' plugin which apparently adds Jinja templating to Markdown files but not other files. But that's not something that can be solved here.