rosscdh / mkdocs-markdownextradata-plugin

A MkDocs plugin that injects the mkdocs.yml extra variables into the markdown template
MIT License
84 stars 17 forks source link

Can't get _data/*.yml files to load #14

Closed daxm closed 4 years ago

daxm commented 4 years ago

I can get the extra: {} format in the mkdocs.yml file to work just fine. However, when I move that same structure into a _data/datafile.yml file it doesn't. When I 'mkdocs build' I just get a traceback:

Step 10/10 : RUN mkdocs build && cp -R /mkdocs/site/* /usr/share/nginx/html
 ---> Running in 03b433bab44f
INFO    -  Cleaning site directory 
INFO    -  Building documentation to directory: /mkdocs/site 
ERROR   -  Error reading page 'sp1.md': 'user' is undefined 
Traceback (most recent call last):
  File "/usr/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/usr/lib/python3.7/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.7/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/lib/python3.7/site-packages/mkdocs/__main__.py", line 159, in build_command
    build.build(config.load_config(**kwargs), dirty=not clean)
  File "/usr/lib/python3.7/site-packages/mkdocs/commands/build.py", line 274, in build
    _populate_page(file.page, config, files, dirty)
  File "/usr/lib/python3.7/site-packages/mkdocs/commands/build.py", line 171, in _populate_page
    'page_markdown', page.markdown, page=page, config=config, files=files
  File "/usr/lib/python3.7/site-packages/mkdocs/plugins.py", line 94, in run_event
    result = method(item, **kwargs)
  File "/usr/lib/python3.7/site-packages/markdownextradata/plugin.py", line 65, in on_page_markdown
    return md_template.render(**extra)
  File "/usr/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 4, in top-level template code
  File "/usr/lib/python3.7/site-packages/jinja2/environment.py", line 471, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'user' is undefined
ERROR: Service 'guide' failed to build: The command '/bin/sh -c mkdocs build && cp -R /mkdocs/site/* /usr/share/nginx/html' returned a non-zero code: 1
rosscdh commented 4 years ago

Please provide your mkdocs.yml for review and provide me at least with a tree _data so i can see the structure you have created

something like

site_name: MkLorum
plugins:
    - search
    - markdownextradata
extra:
  some_variable: value

will check for a folder located at

docs/_data/user.yml

and or (with 0.3.1 release)

./_data/user.yml

and write

{{user.name}} in the template context (notice user is the name of the file in the _data dir)

assuming of course you define a name: bob in the user.yml

Please check the latest 0.1.3 release for upgrade notes for multiple path handling

daxm commented 4 years ago

You bet! structure:

mkdocs
\_ mkdocs.yml
\_docs
  \_sp1.md
\__data
  \_datafile.yml

mkdocs.yml

site_name: Cisco Sales Plays

theme: windmill

plugins:
  - search:
  - markdownextradata:

extra:
  user:
    name: dax
    email: dmickels@cisco.com

markdown_extensions:
  - toc:
      permalink: True
      baselevel: 1

extra_javascript: [extra.js]

extra_css: [extra.css]

datafile.yml:

---
user:
  name: dax
  email: dmickels@cisco.com

sp1.md:

This sales play covers the Data Center features related to geographic redundancy and extending data to more locations.

## Enable Geographic Redundancy
{{ user.name }} - {{ user.email }}

Obviously I change the mkdocs.yml file to this when testing the datafile.yml format:

site_name: Cisco Sales Plays

theme: windmill

plugins:
  - search:
  - markdownextradata:
      data: _data

markdown_extensions:
  - toc:
      permalink: True
      baselevel: 1

extra_javascript: [extra.js]

extra_css: [extra.css]
rosscdh commented 4 years ago
plugins:
  - search:
  - markdownextradata:

should be

plugins:
  - search:
  - markdownextradata

notice the missing : this is yaml

you either need

plugins:
  - search:
  - markdownextradata:{}
or
  - markdownextradata

dont need this

plugins:
  - search:
  - markdownextradata:
      data: _data

as that is the default behaviour

please install 0.1.3

rosscdh commented 4 years ago
\_data
  \datafile.yml

only 1 _ in _data and you dont need it in the yaml file name

rosscdh commented 4 years ago
{{ datafile.user.name }} - {{ datafile.user.email }}
daxm commented 4 years ago

That fixed it! Thank you! Sorry for my misunderstanding. In hindsight it is obvious that I should have to include the filename's name as part of the variable. Doh!

rosscdh commented 4 years ago

no worries.. best of luck with your project

granted the docs were a bit bad on that point have improved them in 0.1.3