Comments (4)
I tracked the issue down to the fact that latex blocks may contain inline code. Currently, inline code gets replaced before latex blocks:
def StripCodeSections(self):
"""(Temporarily) Remove codeblocks/-lines so that they are not altered in all the conversions. Placeholders are inserted."""
self.codeblocks = re.findall(r"^```([\s\S]*?)```[\s]*?$", self.page, re.MULTILINE)
for i, match in enumerate(self.codeblocks):
self.page = self.page.replace("```" + match + "```", f"%%%codeblock-placeholder-{i}%%%")
self.codelines = re.findall("`(.*?)`", self.page)
for i, match in enumerate(self.codelines):
self.page = self.page.replace("`" + match + "`", f"%%%codeline-placeholder-{i}%%%")
self.latexblocks = re.findall(r"^\$\$([\s\S]*?)\$\$[\s]*?$", self.page, re.MULTILINE)
for i, match in enumerate(self.latexblocks):
self.page = self.page.replace("$$" + match + "$$", f"%%%latexblock-placeholder-{i}%%%")
but the reverse is not true for the reinsertionRestoreCodeSections(self)
:
def RestoreCodeSections(self):
"""Undo the action of StripCodeSections."""
for i, value in enumerate(self.codeblocks):
self.page = self.page.replace(f"%%%codeblock-placeholder-{i}%%%", f"```{value}```\n")
for i, value in enumerate(self.codelines):
self.page = self.page.replace(f"%%%codeline-placeholder-{i}%%%", f"`{value}`")
for i, value in enumerate(self.latexblocks):
self.page = self.page.replace(f"%%%latexblock-placeholder-{i}%%%", f"$${value}$$")
The replacement parts are correctly stored in self.codelines, and everything is parsed correctly-ish. So, to resolve this issue you just rearrange the order of operations when restoring code sections:
def RestoreCodeSections(self):
"""Undo the action of StripCodeSections."""
for i, value in enumerate(self.codeblocks):
self.page = self.page.replace(f"%%%codeblock-placeholder-{i}%%%", f"```{value}```\n")
for i, value in enumerate(self.latexblocks):
self.page = self.page.replace(f"%%%latexblock-placeholder-{i}%%%", f"$${value}$$")
for i, value in enumerate(self.codelines):
self.page = self.page.replace(f"%%%codeline-placeholder-{i}%%%", f"`{value}`")
However, in principle you could nest inline code into a codeblock the same way you do with a latex block. In that case, the above solution would fail for the same reason the master branch fails for in this issue. So, you could reorder both RestoreCodeSections()
and StripCodeSections()
to
def StripCodeSections(self):
"""(Temporarily) Remove codeblocks/-lines so that they are not altered in all the conversions. Placeholders are inserted."""
self.codelines = re.findall("`(.*?)`", self.page)
for i, match in enumerate(self.codelines):
self.page = self.page.replace("`" + match + "`", f"%%%codeline-placeholder-{i}%%%")
self.codeblocks = re.findall(r"^```([\s\S]*?)```[\s]*?$", self.page, re.MULTILINE)
for i, match in enumerate(self.codeblocks):
self.page = self.page.replace("```" + match + "```", f"%%%codeblock-placeholder-{i}%%%")
self.latexblocks = re.findall(r"^\$\$([\s\S]*?)\$\$[\s]*?$", self.page, re.MULTILINE)
for i, match in enumerate(self.latexblocks):
self.page = self.page.replace("$$" + match + "$$", f"%%%latexblock-placeholder-{i}%%%")
def RestoreCodeSections(self):
"""Undo the action of StripCodeSections."""
for i, value in enumerate(self.latexblocks):
self.page = self.page.replace(f"%%%latexblock-placeholder-{i}%%%", f"$${value}$$")
for i, value in enumerate(self.codeblocks):
self.page = self.page.replace(f"%%%codeblock-placeholder-{i}%%%", f"```{value}```\n")
for i, value in enumerate(self.codelines):
self.page = self.page.replace(f"%%%codeline-placeholder-{i}%%%", f"`{value}`")
Issue here again is that in theory, if you flip it upside down you could have code blocks containing latex blocks. Not sure if there's an actual application where that would be viable, especially as this would be extremely dependant on the markdown postprocessor used.
I'd argue that especially codelines in latex blocks are a valid option to support, given that f.e. RMarkdown allows for it and is well-used by a shitton of people. Dynamic document generation is awesome, yes I'll preach about it till my death :P.
By design, the edge case of the first-replaced type containing the second- or third-replaced type leading to issues will always exist with this design - not that I would know a way of the top of my head how to circumvent that - so I would suggest to favour the most likely and most useful version.
And in my opinion, you're much more likely to have a latex block containing code in a markdown doc than a code block containing latex syntax . But I might be wrong there.
I guess this pit is deeper than I initially thought, and opens up to the wonderful world of edge-cases. I suppose one could crawl the each document top to bottom and capture/restore it properly that way, just not sure if that is worth the hassle.
Thank you again for this amazing tool.
Sincerely,
~Gw
from obsidian-html.
However, in principle you could nest inline code into a codeblock the same way you do with a latex block. In that case, the above solution would fail for the same reason the master branch fails for in this issue. So, you could reorder both
RestoreCodeSections()
andStripCodeSections()
to
Welp. Guess my brain was dead, cuz that solution is utter bogus. This would annihilate code blocks as the first two ` would be consumed to create inline code, and leaving a malformatted codeblock. So the overall order should be:
- Strip codeblocks
- Strip codelines
- Strip latexblocks
- Restore latexblocks
- Restore codelines
- Restore codeblocks
._.
from obsidian-html.
Did not test this as I am a bit short on time lately, but I agree with your analysis.
And if it works for you than I guess it's tested enough :)
Thanks for working this out and the PR!
from obsidian-html.
I can say at least that for me, running on that branch since opening this issue and PR I have not encountered an issue so far. Obviously I will be biased because I wrote the code pertaining to this; but so far I can not attest to any issues.
from obsidian-html.
Related Issues (20)
- Links of files ending with question marks not generated in Breadcrumbs HOT 2
- filter_on_metadata not working HOT 5
- Request for using non-tag metadata in filter_on_metadata module HOT 3
- Better way of maintaining config files HOT 5
- ESC not available on smartphones HOT 3
- Text height calculation HOT 3
- Non-ASCII characters are not recognized as tags and become h1 HOT 1
- Have all output follow verbosity settings
- Feature Request, inline css and js
- Links with Unicode chars (including hashpart) do not work -- slugified to empty string or single #
- Development stop ObsidianHtml HOT 3
- Excalidraw Drawings not rendered HOT 1
- question: Customizing navigation panel on left-hand side of a website? HOT 1
- Different style for transcluded blocks HOT 7
- Configfiles with encoding 'UTF-8' are parsed correctly even if the path does contain invalidly-encoded chars HOT 2
- Developer Docs - apparent incongruence between existing codebase formatting and results of documented lint operations? HOT 5
- Separator line in notes with frontmatter is ignored
- Rendering Mermaid diagrams with title
- issue with `slugify_html_links` config and directory paths
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from obsidian-html.