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

Debug cannot import footnote from hash #2441

Merged
merged 23 commits into from
Mar 18, 2024

Conversation

yiwen101
Copy link
Contributor

@yiwen101 yiwen101 commented Mar 3, 2024

What is the purpose of this pull request?
Resolve #2203 (but #2430 still persists)

  • Documentation update
  • Bug fix
  • Feature addition or enhancement
  • Code maintenance
  • DevOps
  • Improve developer experience
  • Others, please explain:

Overview of changes:

Anything you'd like to highlight/discuss:

Testing instructions:

Proposed commit message: (wrap lines at 72 characters)
Fix issue of cannot import footnote from hash


Checklist: ☑️

  • Updated the documentation for feature additions and enhancements
  • Added tests for bug fixes or features
  • Linked all related issues
  • No unrelated changes

currently, when importing from hash, footnote is missing. For example, with

<div id="import">

text^[footnote]
</div>

and

<include src="contents/topic1.md#import"/>

this is rendered
Screenshot 2024-03-04 at 03 30 29
but we expect
Screenshot 2024-03-04 at 03 28 23

This is fixed by using regular pattern to greedily extract and append the missing information.

Please note that #2430 still persists (the index of the imported footnotes may remain be conflicting). This PR only fixes only failure in importing footnote from hash, but did not touch the issue of wrong index.


Reviewer checklist:

Indicate the SEMVER impact of the PR:

  • Major (when you make incompatible API changes)
  • Minor (when you add functionality in a backward compatible manner)
  • Patch (when you make backward compatible bug fixes)

At the end of the review, please label the PR with the appropriate label: r.Major, r.Minor, r.Patch.

Breaking change release note preparation (if applicable):

  • To be included in the release note for any feature that is made obsolete/breaking

Give a brief explanation note about:

  • what was the old feature that was made obsolete
  • any replacement feature (if any), and
  • how the author should modify his website to migrate from the old feature to the replacement feature (if possible).

Copy link

codecov bot commented Mar 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 51.22%. Comparing base (371228d) to head (d555215).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2441      +/-   ##
==========================================
+ Coverage   49.04%   51.22%   +2.17%     
==========================================
  Files         124      124              
  Lines        5262     5281      +19     
  Branches     1118     1121       +3     
==========================================
+ Hits         2581     2705     +124     
+ Misses       2376     2290      -86     
+ Partials      305      286      -19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@kaixin-hc kaixin-hc left a comment

Choose a reason for hiding this comment

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

Yay, thanks for the tests!

The issue #2203 also raises the example of the footnote in reader facing features. However, this is not fixed by your PR (is it because the new line isn't there?)

Can you also fix this documentation bug to demonstrate that this feature works?

packages/core/test/unit/html/includePanelProcessor.test.ts Outdated Show resolved Hide resolved
packages/core/src/html/includePanelProcessor.ts Outdated Show resolved Hide resolved
@yiwen101
Copy link
Contributor Author

yiwen101 commented Mar 4, 2024

Yay, thanks for the tests!

The issue #2203 also raises the example of the footnote in reader facing features. However, this is not fixed by your PR (is it because the new line isn't there?)

Can you also fix this documentation bug to demonstrate that this feature works?

Thanks for the comment; should be fixed in this commit

@kaixin-hc
Copy link
Contributor

Behavior for footnotes here is definitely unexpected...

Screenshot 2024-03-04 at 3 23 04 PM Screenshot 2024-03-04 at 3 23 16 PM

Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

Thanks for the work @yiwen101!
This is a rather tricky bug to fix so appreciate the work.

I have some concerns about the ordering of the footnote. I understand you aren't trying to address the wrong ordering here but I think this might result in overlaps - not just an issue with wrong ordering. Could you explain why you change the footnote number and why the appended footnote item will has the number 1-1?

Thanks!

packages/core/src/html/includePanelProcessor.ts Outdated Show resolved Hide resolved
packages/core/src/html/includePanelProcessor.ts Outdated Show resolved Hide resolved
let toAppend = '<mb-temp-footnotes>';
matches.forEach((match) => {
const href = match.match(hrefPattern)![1];
const replaceTo = `<a aria-describedby="footnote-label" href="#${href}">[${footnoteNumber}]`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it necessary to do this replace here? Why are you changing the number for the footnote?

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 have improved this with the following changes:
Screenshot 2024-03-14 at 16 54 05

where the default value is:
Screenshot 2024-03-14 at 16 54 00

last modification:
Screenshot 2024-03-14 at 16 54 13

Copy link
Contributor Author

@yiwen101 yiwen101 Mar 14, 2024

Choose a reason for hiding this comment

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

Motivation being, as this initially is to fix the error in display footnote in the user doc, I decided to be defensive and do the best (without going beyond the scope and making changes outside the hash part) to strive for making importing footnote from a single hash look good.

@@ -230,6 +230,24 @@ export function processInclude(node: MbNode, context: Context, pageSources: Page
const $ = cheerio.load(actualContent);
const actualContentOrNull = $(hash).html();
actualContent = actualContentOrNull || '';
if (actualContent !== '') {
const footnotePattern = /<a aria-describedby="footnote-label" href="#fn-\d+-\d+">\[\d+\]/g;
Copy link
Contributor

Choose a reason for hiding this comment

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

oh also small thing but i was wondering if using cheerio here makes more sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Emm, but is not to use cheerio, we need the exact id (eg: #${href}.footnote-item)? So we still need to pattern matching (and also to replace the index in the original content).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I get that cheerio will do the same thing but I think its cleaner to use cheerio to select out and append. In that case, we can use cheerio node to do the modifications below as well. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yucheng11122017
Thank you for the clarification; I have explored cheerio apis and made the requested changes. Following is a quick snapshot:
Screenshot 2024-03-14 at 23 47 04

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(console.log on last line was removed in later commits)

@yiwen101 yiwen101 marked this pull request as draft March 5, 2024 03:41
@yiwen101
Copy link
Contributor Author

yiwen101 commented Mar 5, 2024

Behavior for footnotes here is definitely unexpected...

Screenshot 2024-03-04 at 3 23 04 PM Screenshot 2024-03-04 at 3 23 16 PM

emm, thanks for pointing out this (and sorry for not examining it closely);will be working on it.

@yiwen101
Copy link
Contributor Author

yiwen101 commented Mar 5, 2024

Thanks for the work @yiwen101! This is a rather tricky bug to fix so appreciate the work.

I have some concerns about the ordering of the footnote. I understand you aren't trying to address the wrong ordering here but I think this might result in overlaps - not just an issue with wrong ordering. Could you explain why you change the footnote number and why the appended footnote item will has the number 1-1?

Thanks!

Thanks for the careful review.
The the number 1-1 is an unintended bug, thank you for pointing it out!
The footnoteNumber is just a temporary placeHolder; the thinking is that when I/somebody attempt 2430 in the future and add footnoteNumber as an argument to this method, the rest of the code does not need to be changed (wait, just come to me, how to do passing by reference in javascript? nevermind, there must be a way to do it).

@yiwen101
Copy link
Contributor Author

yiwen101 commented Mar 14, 2024

@kaixin-hc @yucheng11122017
I have made the requested changes, and is ready for review;
Summary of the changes:
1 improve the readability of the test
2 improve the code quality issue of hardcoded footnoteNumer = 1
3 fix bug.
Some additional note on to the third point:
Current behavior of serving user doc:
Screenshot 2024-03-14 at 16 18 22
Screenshot 2024-03-14 at 16 18 29

The bahaviour of the master branch

(after adding empty to enable the markdown:)
Screenshot 2024-03-14 at 16 15 47
(no pop up, when mouse is over; no response when clicked)
Screenshot 2024-03-14 at 16 14 54
Screenshot 2024-03-14 at 16 14 42

@yiwen101 yiwen101 marked this pull request as ready for review March 14, 2024 09:12
Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

Hi @yiwen101 thanks for the work! Some more corrections

packages/core/src/html/includePanelProcessor.ts Outdated Show resolved Hide resolved
packages/core/src/html/includePanelProcessor.ts Outdated Show resolved Hide resolved
@@ -230,6 +230,24 @@ export function processInclude(node: MbNode, context: Context, pageSources: Page
const $ = cheerio.load(actualContent);
const actualContentOrNull = $(hash).html();
actualContent = actualContentOrNull || '';
if (actualContent !== '') {
const footnotePattern = /<a aria-describedby="footnote-label" href="#fn-\d+-\d+">\[\d+\]/g;
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I get that cheerio will do the same thing but I think its cleaner to use cheerio to select out and append. In that case, we can use cheerio node to do the modifications below as well. What do you think?

Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

Hi @yiwen101 thanks so much for cleaning up the code! I think using cheerio makes it more consistent with the rest of the code base. Just addition of one comment and I think this PR is ready to merge.

Thanks for the good work!

if (footnodeHrefs.length > 0) {
const tempFootnotes = $('<mb-temp-footnotes></mb-temp-footnotes>');
footnodeHrefs.forEach((href) => {
const listItem = $('<li></li>').attr('id', href.substring(1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment to explain why we need to do a substring here? It is just to get rid of the hash in front right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the comment; has added the the latest commit

@yucheng11122017 yucheng11122017 merged commit dd4bfd3 into MarkBind:master Mar 18, 2024
10 checks passed
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.

Footnote not being imported correctly in includes when hash is used
3 participants