Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support internal links, issue #95 #104

Merged
merged 22 commits into from
Nov 30, 2023
Merged

Conversation

trustedtomato
Copy link
Contributor

Support internal links, resolves #95
I implemented it as per our discussions in #96

@Mr0grog
Copy link
Owner

Mr0grog commented Oct 14, 2023

Sorry I don’t have time to get to this today, but will try and take a deeper look sometime in the next few days.

Copy link
Owner

@Mr0grog Mr0grog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I dropped the ball and lost track of this! Added a few notes inline. I'll be away from a computer all weekend, but should be able to merge next week.

test/unit/convert.test.js Outdated Show resolved Hide resolved
lib/fix-google-html.js Outdated Show resolved Hide resolved
Comment on lines 153 to 166
// normalize the tag name
if (
(
(node.tagName === 'b' || node.tagName === 'strong')
&& (style['font-weight'] === 'normal' || style['font-weight'] === '400')
) ||
(
(node.tagName === 'i' || node.tagName === 'em') &&
style['font-style'] === 'normal'
)
) {
node.tagName = 'span';
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case you're hitting where Google Docs is outputting a <b>/<strong>/<i>/<em> tag and then setting the weight to not be bold/italic? I don't think I've seen this; curious about when it happens.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this since I could not find any clear examples of when/where this was needed in practice, and in the abstract, I think it's pretty debatable. Should the resulting markdown match the semantic info in the HTML or the styling in place? A <strong> could be restyled to not be bold, but instead be big, colored, underlined, or something else to show emphasis. It's not obvious that just because a <strong> is not bold it should not equate to **text** in markdown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that because the root element of the paste content is a bold element for some reason. However, that problem was resolved when I switched back to letting the browser handle the paste (instead of manually updating the innerHTML with the clipboard content on paste), so this is indeed obsolete. And I think you are right, the text might be bolder by default, and then this would break, so good call on removing it 👍

It's good to actually test the code! Especially since the new code does not pass its test now that we are actually running it. 😬
@Mr0grog
Copy link
Owner

Mr0grog commented Nov 29, 2023

I went ahead and rebased this. I also added support for the GDocs Slice Clip data to createFixtureTest() and uncommented the test here — it turns out it doesn’t actually pass (that is, the test failure on this PR now is legitimate).

I’ll have to do some more overall cleanup on this PR later in the week.

This simplifies the `internal-links` test fixture a bit in order to keep it focused. This also removes some complicated markup around boldness in the headings because things get complicated there, and made a separate issue about it: Mr0grog#113.

I've also moved the fixture to our official GDocs fixture folder.
The `<a>` element is meant for exactly this use case, not just clickable links (that's why it's called "a" -- short for "anchor"). This also moves it to the end of headings instead of the start, since that's where the existing test expectation has it. As a result, the `internal-links` test now passes.
Since headings now have anchors in them, other expectation fixtures that have headings need to include them, too. This makes unit tests pass.
@Mr0grog
Copy link
Owner

Mr0grog commented Nov 29, 2023

OK, tests pass! This needed both some code changes and fixture changes. There is probably some more cleanup and tweaking worth doing here before merging, though.

I also simplified the test fixture a bit, and removed the bold styling inside one of the headers, which it turns out behaves differently on different browsers. I’ve filed a separate issue to handle that: #113.

The helper made code usefully concise if detailed error messaging wasn't needed (if you provide custom error handling, it doesn't buy you much). In most places it was used, I think we could have benefitted from better messaging (which I added) or we aren't actually conerned with the errors at all and can use constructs like optional chaining/optional calling to have even more concise code instead. There is one callsite that becomes notably worse, but I think only one is OK.
I could not find any clear examples of when/where this was needed in practice, and in the abstract, I think it's pretty debatable. Should the resulting markdown match the semantic info in the HTML or the styling in place? I think it's debatable; a `<strong>` could be restyled to not be bold but big, or colored, or underlined, or something else instead to show emphasis. It's not obvious that just because a `<strong>` is not bold it should not equate to `**text**` in markdown.
For errors and warnings, use `console.error|warn` directly so they don't get buried in hidden. For more debug/info level logs, continue using `debug` (and use the multiple argument form correctly, with formatting directives). I'm not super in love with `debug` in the browser, but it's already included by Micromark (used by Remark), so this is probably fine.
@Mr0grog Mr0grog merged commit effb439 into Mr0grog:main Nov 30, 2023
4 checks passed
Mr0grog added a commit that referenced this pull request Nov 30, 2023
Oops, I should have done this as part of #104!
Mr0grog added a commit that referenced this pull request Nov 30, 2023
Oops, I should have done this as part of #104!
@trustedtomato
Copy link
Contributor Author

trustedtomato commented Dec 3, 2023

Thanks for the refactor! Lots of good calls! 🤩 Not sure about the change from <span> to <a>, seems like a semantical misuse of the link element. What's the rationale for that?

@Mr0grog
Copy link
Owner

Mr0grog commented Dec 3, 2023

Seems like a semantical misuse of the element

Oh, this is literally what <a> is designed for! It is semantically correct usage. Back in the day, <a href="#somewhere"> did not link to any element with id="somewhere" like it does today (this behavior was new in HTML 4.01, IIRC). Instead, it could only link to an <a> element with name="somewhere" or id="somewhere". “A” is short for anchor — of both ends of the link.

Obviously the ideal is to give the heading (or whatever element you are linking to) an id attribute instead of creating an empty element just to be the target of the link. But in cases where you can’t do that, the semantically correct element to use is <a>, just like you should use <strong> instead of <span style="font-weight: bold;">.

@Mr0grog Mr0grog mentioned this pull request Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Link within the document
2 participants