question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Math equation is broken

See original GitHub issue
### Linear Models and Least Squares

Given a vector of inputs $X^T=(X_1, X_2, \ldots, X_p)$, we predict output $Y$ via the model
$$
\hat{Y} = \hat{\beta}_0 + \sum_{j=1}^p X_j \hat{\beta}_j
$$
The term $\hat{\beta}_0$ is the intercept, also known as the *bias* in machine learning. Often it is convenient to include the constant variable 1 in $X$, include $\hat{\beta_0}$ in the vector of coefficients $\hat{\beta}$, and then write the linear model in vector form as an inner product
$$
\hat{Y} = X^T \hat{\beta}
$$
where $X^T$ denotes vector or matrix transpose ($X$ being a column vector). Here we are modeling a single output, so $\hat{Y}$ is a scalar; in general $\hat{Y}$ can be a $K$-vector, in which case $\beta$ would be a $p \times K$ matrix of coefficients. In the $(p+1)$-dimensional input-output space, $(X, \hat{Y})$ represents a hyperplane. If the constant is included in $X$, then the hyperplane includes the origin and is a subspace; if not; it is an affine set cutting the $Y$-axis at the point $(0, \hat{\beta}_0)$. From now on we assume that the intercept is included in $\hat{\beta}$.

In typora: 图片

with open('temp.md', "r", encoding="utf-8") as mdFile:
    newPage = page.children.add_new(PageBlock, title=mdFile.name)
    
    txt = mdFile.read()
    txt_list = re.split(pattern, txt)
    for i, string in enumerate(txt_list):
        if string == '':
            txt_list[i] = '\n'
    new_txt = ''.join(txt_list)

    rendered = convert(new_txt,addLatexExtension(NotionPyRenderer))
    for blockDescriptor in rendered:
        uploadBlock(blockDescriptor, newPage, mdFile.name)

The equation is broken 图片

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
shizidushucommented, Mar 14, 2021

This line should be setting title_plaintext like CodeBlock does, instead of title. That should fix it

@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)
0reactions
Cobertoscommented, Mar 30, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

The simple reason a viral math equation stumped the internet
The real reason, then, that 8÷2(2+2) broke the internet stems from the practice of omitting the multiplication symbol, which was inappropriately ...
Read more >
The Math Equation That Tried to Stump the Internet
The Math Equation That Tried to Stump the Internet. Sometimes BODMAS is just PEMDAS by another name. And no, the answer is not...
Read more >
This Basic Math Problem Is Breaking the Internet | Inc.com
If you were taught math using BODMAS, you probably solved the equation as follows: 8 ÷ 2(2+2) = 8 ÷ 2(4) = 8...
Read more >
The simple reason a viral math equation stumped the internet
The real reason, then, that 8÷2(2+2) broke the internet stems from the practice of omitting the multiplication symbol, which was inappropriately ...
Read more >
The Simplest Math Problem No One Can Solve - YouTube
The Collatz Conjecture is the simplest math problem no one can solve — it is easy enough for almost anyone to understand but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found