Multiline strings in metadata
See original GitHub issueI am attempting to add a multiline string to a document, but I’m getting syntax that breaks my pandoc processing. Is there a way to save the multi line strings using yaml block syntax?
Test case
import frontmatter
doc = frontmatter.loads(
'\n'.join([
'---',
'title: multiline test',
'---',
'# multiline test',
])
)
doc['multiline'] = '\n'.join([
'this is a',
'multiline',
'string',
])
print(frontmatter.dumps(doc))
Expected (desired) output
---
multiline: |
this is a
multiline
string
title: multiline test
---
# multiline test
Actual output
---
multiline: 'this is a
multiline
string'
title: multiline test
---
# multiline test
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to define multi-line @description metadata?
You don't. The general idea was that the description was supposed to be concise "Microcontent" and the size can't be too large without ......
Read more >Metadata Properties
If you need a string that spans several lines, use a multiline string literal - a sequence of characters surrounded by double (...
Read more >Metadata format: metadata is not a plain mapping of strings
Almost all build backends assume metadata to be a plain mapping of strings (distutils, setuptools, flit, poetry, etc.)
Read more >Ease creating multiline documentation or metadata values.
Creating multiline documentation is currently somewhat annoying as you need to include \n in the test data yourself: *** Settings *** Documentation First...
Read more >appendix B. TOML and JSON for metadata - Hugo in Action
key1= """ This is a multiline string where newline characters are valid. Multi line strings end by three quote(") symbols """ key12= '''...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, it is indeed the SafeDumper. Excellent suggestion. Makes sense as I’m now wrapping a string in an object. It seems that if I add the new representer to the SafeDumper itself, then I can continue to dump this document safely.
So this is the working version:
Which outputs:
Any
**kwargs
you pass when you runfrontmatter.dumps
will get passed through toyaml.dump
, so you might try that. Here’s the relevant code: https://github.com/eyeseast/python-frontmatter/blob/master/frontmatter/default_handlers.py#L240-L249It uses PyYaml’s
SafeDumper
class by default, which might be the issue. But if you can make it work withyaml.dump
, you should be able to get it working withfrontmatter.dumps
.