-
Notifications
You must be signed in to change notification settings - Fork 66
Math equation is broken #31
Comments
Hmm, it looks like the That's the only difference I see between your equation and the below:
|
This line should be setting |
@Cobertos Thanks. I get it works. from mistletoe.block_token import BlockToken
from mistletoe.html_renderer import HTMLRenderer
from mistletoe import span_token
from mistletoe.block_token import tokenize
from md2notion.NotionPyRenderer import NotionPyRenderer
from notion.block import EquationBlock, field_map
class CustomEquationBlock(EquationBlock):
latex = field_map(
["properties", "title_plaintext"],
python_to_api=lambda x: [[x]],
api_to_python=lambda x: x[0][0],
)
_type = "equation"
class CustomNotionPyRenderer(NotionPyRenderer):
def render_block_equation(self, token):
def blockFunc(blockStr):
return {
'type': CustomEquationBlock,
'title_plaintext': blockStr #.replace('\\', '\\\\')
}
return self.renderMultipleToStringAndCombine(token.children, blockFunc)
import re
pattern = re.compile(r'( {0,3})((?:\$){2,}) *(\S*)')
class Document(BlockToken):
def __init__(self, lines):
if isinstance(lines, str):
lines = lines.splitlines(keepends=True)
else:
txt = lines.read()
txt_list = re.split(pattern, txt)
for i, string in enumerate(txt_list):
if string == '':
txt_list[i] = '\n'
lines = ''.join(txt_list)
lines = lines.splitlines(keepends=True)
lines = [line if line.endswith('\n') else '{}\n'.format(line) for line in lines]
self.footnotes = {}
global _root_node
_root_node = self
span_token._root_node = self
self.children = tokenize(lines)
span_token._root_node = None
_root_node = None
def markdown(iterable, renderer=HTMLRenderer):
"""
Output HTML with default settings.
Enables inline and block-level HTML tags.
"""
with renderer() as renderer:
return renderer.render(Document(iterable))
def convert(mdFile, notionPyRendererCls=NotionPyRenderer):
"""
Converts a mdFile into an array of NotionBlock descriptors
@param {file|string} mdFile The file handle to a markdown file, or a markdown string
@param {NotionPyRenderer} notionPyRendererCls Class inheritting from the renderer
incase you want to render the Markdown => Notion.so differently
"""
return markdown(mdFile, notionPyRendererCls) |
The InlineEquation has the same problem. @Cobertos Can you have a look? |
I comment this line https://github.com/miyuchina/mistletoe/blob/2cfe7446b975685f98837f9e40aaabcc0e270a79/mistletoe/core_tokens.py#L63 |
I'll leave it open until the fix gets in the library itself. Will need to do that soon. As for the inline equations, notion-py is the one that actually handles uploading inline equations to Notion, added in this PR. This is because it does some special conversions to convert to Notion's expected format. Looking at that PR, it looks like In your case though, in md2notion, emphasis is handled by re-echoing out the specific markdown as notion-py will handle that later. That's going to cause issues in your case, converting |
There is no problem related to the single There is another problem that worth metioning is that if there is no blank line before the block equation, the block equation will be treated as part of <
8000
code class="notranslate">TextBlock. import itertools
new_lines = []
for (i, line) in enumerate(lines):
new_line = [None, line, None]
if i > 0 and i < len(lines) - 2:
if line == '$$\n' and lines[i-1][0] != '\n':
new_line[0] = '\n'
if line == '$$\n' and lines[i+1][0] != '\n':
new_line[2] = '\n'
new_lines.append(new_line)
new_lines = list(itertools.chain(*new_lines))
new_lines = list(filter(lambda x: x is not None, new_lines))
new_lines = ''.join(new_lines)
lines = new_lines.splitlines(keepends=True)
lines = [line if line.endswith('\n') else '{}\n'.format(line) for line in lines] Hope it will be handled well and may be more intelligently by the package too. |
I'd have to play with it more, but I think requiring 2 line breaks between
blocks is part of Markdown itself.
…On Tue, Mar 16, 2021, 7:38 AM shizidushu ***@***.***> wrote:
There is no problem related to the single $, it has been handled well
somewhere.
There is another problem that worth metioning is that if there is no blank
line before the block equation, the block equation will be treated as part
of TextBlock.
I add \n before and after the double $$ and then trim the equation block
string to avoid.
import itertoolsnew_lines = []for (i, line) in enumerate(lines):
new_line = [None, line, None]
if i > 0 and i < len(lines) - 2:
if line == '$$\n' and lines[i-1][0] != '\n':
new_line[0] = '\n'
if line == '$$\n' and lines[i+1][0] != '\n':
new_line[2] = '\n'
new_lines.append(new_line)new_lines = list(itertools.chain(*new_lines))new_lines = list(filter(lambda x: x is not None, new_lines))new_lines = ''.join(new_lines)lines = new_lines.splitlines(keepends=True)lines = [line if line.endswith('\n') else '{}\n'.format(line) for line in lines]
Hope it will be handled well and may be more intelligently by the package
too.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#31 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTSGCBQ4MBJVJA4NBP2WULTD47MPANCNFSM4ZCTPFZA>
.
|
To answer all the fixes/questions related to equation blocks current state
I added a test that now tests for this. What gets passed to I don't see an easy fix for
Woops, yes, I was mistaken. This works correctly. Single
Hmm, I am seeing this issue. Ideally we would support this sort of case because it's similar to how CommonMark's specification describes code fences. "A fenced code block may interrupt a paragraph, and does not require a blank line either before or after." After some research, the issue lies with how |
Upstream tag for the inline equation issue. Open to ideas to fix the newline thing,,, can't think of an easy way to integrate that |
In typora:

The equation is broken

The text was updated successfully, but these errors were encountered: