Skip to main content

parse logseq markdown text with easy access to properties, hierarchy, TODO etc

Project description

LogseqMarkdownParser

a simple python script to load a markdown file and easily access the properties of each block etc. You can also parse it as json, handy when using jq. toml output is also supported. You can use it as a cli tol or as a python library.

Notes to reader

  • Why make this? I wanted a script that reads a Logseq page, extracts every "DONE" tasks and append it to another file. So I made this little parser. The resulting script can be found in examples/done_mover.py. If you need anything just create an issue.
  • How stable is it? Probably okay, I use it for specific things so things might go south in edge cases. Please open an issue if you found a bug.
  • Note that the github version might be more up to date than the PyPI version
  • Does it take into account the logbook (i.e. what's added to the block when clicking on 'DOING')? I didn't think about that initially. I think it should be parsed as normal block content and not as a property.
  • What's the deal with properties? page.page_properties is a python dict, you can edit it freely as it's only appended to the top of the page when exporting. But page.blocks[0].properties is an ImmutableDict because the properties are stored inside the text content using Logseq format. To edit a block property, use the del_property and set_property method.

Features

  • Implements classes LogseqPage and LogseqBlock
  • read pages, page properties, block and block properties as a regular python dictionary
  • easily save to a path as a Logseq-ready markdown file with page.export_to
  • Static typing with beartype if you have it installed (otherwise no typechecking).
  • parse for the cli as json: LogseqMarkdownParser some_file.md --out_format='json' |jq
  • parse for the cli as toml: LogseqMarkdownParser some_file.md --out_format='toml' > output.toml
  • supports stdin: cat some_file.md | LogseqMarkdownParser --out_format='json' | jq
  • shell completion: eval "$(LogseqMarkdownParser -- --completion)" or eval "$(cat completion.zsh)"

How to

  • Install with python -m pip install LogseqMarkdownParser

Usage

import LogseqMarkdownParser

# loading:
# load file
page = LogseqMarkdownParser.parse_file(file_content, verbose=True)
# load a string
page = LogseqMarkdownParser.parse_text(content=my_string, verbose=True)
# load a string as page manually
page = LogseqMarkdownParser.LogseqPage(content=my_string, verbose=True)

# get page properties
page.page_properties

# access the blocks as a list
page.blocks

# get a block's properties
page.blocks[0].properties
# You can't edit them directly though, only page_properties can be directly edited at this time, see note below

# edit block properties
page.blocks[0].set_property(key, value)
page.blocks[0].del_property(key)

# inspect a page or block as a dict
page.dict()  # this include the page properties, each block and their properties
page.blocks[0].dict()

# Save as Logseq ready md file
page.export_to("some/path.md")

# format as another format
print(page.format('json'))  # also toml

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logseqmarkdownparser-3.3.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

LogseqMarkdownParser-3.3-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

Details for the file logseqmarkdownparser-3.3.tar.gz.

File metadata

  • Download URL: logseqmarkdownparser-3.3.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.20

File hashes

Hashes for logseqmarkdownparser-3.3.tar.gz
Algorithm Hash digest
SHA256 31546115334ea79d4a2f84a41c27250c66b09f826e0bfc792630824fb0e3e07b
MD5 c921f04880cc48b28650f2121093f414
BLAKE2b-256 bff34277780d2f77409021b7d7a58c6463888fc6b46f57cf0229170d4cecb708

See more details on using hashes here.

File details

Details for the file LogseqMarkdownParser-3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for LogseqMarkdownParser-3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 eb9b9b6fcd9365b15280f7723b7ea5b48fff642a33dfcf3883c8f5dcc2fc46eb
MD5 7b44d73ca7560fa3fe8ef37cefaee3b7
BLAKE2b-256 536e3f40d7c86ef8da5967d9d5c8428a4e8aff8148de30db5fcb842c67225e92

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page