Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Rendering Issue on Bold/Italics/Strikethrough Followed By Text #7

Open
kleinfelter opened this issue May 1, 2018 · 9 comments
Open

Comments

@kleinfelter
Copy link

Both of the following lose the space between 'first' and 'second'. I see that you support two slashes for italics. GFM also supports the asterisk form per https://guides.github.com/features/mastering-markdown/

*first* second
_first_ second
**first** second

These should render as:

first second
first second
first second

but they render as:

firstsecond
_first_second
firstsecond

I see that you used this markup successfully at your demo site, but only as single-line items. This is a problem at your demo site too, if you add a space and text after the italicized items.

Interestingly, it also consumes multiple spaces and even  

@kleinfelter
Copy link
Author

kleinfelter commented May 1, 2018

Same thing happens with strikethrough:

~~strikethrough~~  followed

renders as strikethroughfollowed

The only way I can find to get a space to render in between the words is

~~strikethrough~~[]()followed

which renders as

strikethrough followed

... And the same thing happens with:

<em>italics</em>  followed

which ALSO renders as italicsfollowed

I thought at first it might be a bug in Markdown-It, but their demo site at https://markdown-it.github.io/ handles these correctly.

@kleinfelter kleinfelter changed the title Rendering Issue on Rendering Issue on Bold/Italics/Strikethrough Followed By Text May 1, 2018
@kleinfelter
Copy link
Author

kleinfelter commented May 2, 2018

I think I see the problem, but fixing it is over my head (for the moment). In wrapper.js, you call

const source = markdown.render(text);

The output from that is correct. The space is still present. e.g. Output is something like:

<em>italics</strong>  and a space

When you call this:

const children = new $tw.Wiki.parsers[TYPE_WIKI](TYPE_WIKI, node.textContent, {}).tree[0].children;

with " and a space" in node.textContent, there is a leading space. But for a text node, after calling that routine, children[0] never has a leading space, and that drops necessary leading spaces.

I don't fully understand your code, but changing the try block in tiddlify to look like this has fixed the dropping of necessary spaces.

            try {
                const children = new $tw.Wiki.parsers[TYPE_WIKI](TYPE_WIKI, node.textContent, {}).tree[0].children;
                if (children.length === 0) {
                    subtree = null;
                }
                else if (children.length === 1) {
                    subtree = children[0];
                    subtree.text = node.textContent; // **** kpk hack ****
                }
                else {
                    subtree = {
                        type: 'element',
                        tag: 'span',
                        children: children,
                    };
                }
            }

@anstosa
Copy link
Owner

anstosa commented May 2, 2018

Thanks for looking into this in so much detail! It's a busy week for me but I'll play with this on Sunday

@sanderv
Copy link

sanderv commented Jul 12, 2019

It also fails on [links](https://github.com/anstosa/tw5-markdown/issues/7) followed by text.

It also fails on linksfollowed by text.

@sanderv
Copy link

sanderv commented Jul 13, 2019

@kleinfelter -hacked- wrote:

            else if (children.length === 1) {
                subtree = children[0];
                subtree.text = node.textContent; // **** kpk hack ****
            }

If you change the hack to an educated guess (I didn't go through thorough testing for all scenarios), you could just re-add the leading space:

            else if (children.length === 1) {
                subtree = children[0];
                if (node.textContent.startsWith(" ")) {
                    subtree.text = " " + subtree.text;
                }
            }

@sanderv
Copy link

sanderv commented Jul 13, 2019

Even fancier, because mixing Markdown and TiddlyWiki syntax doesn't work in the above solution:

Now _this_ works.

But it _doesn't_ work if you add wiki syntax like [[this link]]. It would just remove the space again...

I added a function that recursively looks for the first text node, adds a space and then returns:

const addStartSpace = (node) => {
    if (node.type == 'text') {
        node.text = ' ' + node.text
        return true;
    } else if (node.children.length > 0) {
        let addedSpace = false;
        node.children.forEach((child) => {
            if (!addedSpace) {
                addedSpace = addStartSpace(child);
            }
        });
        return addedSpace;
    }
}

It should be called after all the markdown and wiki parsing on the resulting subtree:

            if (children.length === 0) {
                subtree = null;
            }
            else if (children.length === 1) {
                subtree = children[0];
            }
            else {
                subtree = {
                    type: 'element',
                    tag: 'span',
                    children: children,
                };
            }
            if (subtree != null && node.textContent.startsWith(" ")) {
                addStartSpace(subtree);
            }

@sanderv
Copy link

sanderv commented Jul 14, 2019

Found another issue: macros don't work. Apparently those aren't picked up by the Wiki parser. Giving up on fixing this, sorry.

AndreiPashkin added a commit to AndreiPashkin/tw5-markdown that referenced this issue Aug 23, 2019
This patch makes MarkdownParser rely on WikiParser to parse WikiText
instead of hackish low-level recursive code previously encapsulated
in tiddlify() function.

Addresses anstosa#7.
@nullrend
Copy link

Manually applied cb9d404 patch to my work tiddlywiki instance and everything seems to be working okay.

@AndreiPashkin
Copy link

@nullrend, macros won't work.

haaase pushed a commit to haaase/tw5-markdown that referenced this issue May 7, 2020
This patch makes MarkdownParser rely on WikiParser to parse WikiText
instead of hackish low-level recursive code previously encapsulated
in tiddlify() function.

Addresses anstosa#7.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants