Skip to content

Instantly share code, notes, and snippets.

@TomBener
Last active June 27, 2021 05:25
Show Gist options
  • Save TomBener/a3c6aed53484c8f233df191fa2c0ec92 to your computer and use it in GitHub Desktop.
Save TomBener/a3c6aed53484c8f233df191fa2c0ec92 to your computer and use it in GitHub Desktop.
Change Markdown exported from Roam Research to general Markdown and convert to DOCX via Pandoc
# The order of finding & replacing cannot be changed arbitrarily!
# I define the contextual citation as `A conclusion was obtained by @stern2020`,
# And the bare citation as `The mining industry has negative impacts on the environment, such as air pollution [@wu2011] and water contamination [dogaru2009]`.
# Bear in mind that exported Flat Markdown:
# 1. Use the hashtag instead of the alias page references
# 2. The alt text of block references is the citekey
# 3. With the punctuation after the contextual citation, add additional {} in Roam
# 4. Without the punctuation after the bare citaion, add additional <> in Roam
# Turn italics syntax __ to *
perl -i -p -e 's/(__)(.*?)(__)/\*\2\*/g' *.md
# Remove something like {{word-count}}
perl -i -p -e 's/ \{\{.*\}\}//g' *.md
# Remove the hashtag
perl -i -p -e 's/\s#\[\[.*\]\]//g' *.md
perl -i -p -e 's/\s#\S+//g' *.md
# Remove block references alias with *
perl -i -p -e 's/\s\[\*\]\({3}\S*\){3}//g' *.md
# Clean page references without @
perl -i -p -e 's/(\[\[)([^@].*?)(\]\])/\2/g' *.md
# Clean page references with @
perl -i -p -e 's/(\[)(\[@[a-z0-9]*\])(\])/\2/g' *.md
# Clean block references
perl -i -p -e 's/\({3}\S*\){3}//g' *.md
# Change general [citekey] to citekey
perl -i -p -e 's/(\[)(@[a-z0-9]*)(\]\s)/\2 /g' *.md
# Tweaks for some special citekeys
# Remove <> for the bare citation
perl -i -p -e 's/(<)(\[@[a-z0-9]*\])(>)/\2/g' *.md
# Remove {[]} for the contextual citation
perl -i -p -e 's/(\{\[)(@[a-z0-9]*)(\]\})/\2/g' *.md
# Handle multiple citekeys
perl -i -p -e 's/(\])(;\s)(\[)(@)/\2\4/g' *.md
# Generate `main.docx` via Pandoc
pandoc -C -N --bibliography ref.bib \
-M reference-section-title="References" \
-M link-citations=true --reference-doc \
ref.docx *.md -o main.docx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment