Skip to content

Conversation

vezwork
Copy link
Collaborator

@vezwork vezwork commented Aug 5, 2025

Changes

  • Apply formatter to changed files (mostly normalize whitespace, semicolons)
  • Big refactor of mapTokens (renamed to mapTokensRecursive)
    • Move nested functions out
    • Clarify isToken logic by removing redundant logic
    • This function has somewhat brain-bending recursive functionality (I was confused and I also saw it being misused in resolvePandocBlockCapsuleText, resulting in redundant logic). It has two interrelated recursive parts. A "recursive array data mapping" part and a "recursive function output content mapping" part. Because of the recursive nature of this function, I feel justified in going full functional programmer on its refactor: I refactored into two separate functions that return functions: mapRecursiveArray factors out the "recursive array data mapping" part, and mapTokenAndContentTokensRecursively factors out the "recursive function output content mapping" part. It is still a difficult function to understand, but I've removed some redundancy and made how its parts depend on eachother more direct and explicit.
    • Add docs
  • After refactoring mapTokensRecursive I understood it better and was able to understand and significantly simplify resolvePandocBlockCapsuleText
  • Change writeToken to prefer Array.find over iterating through the array until the element is found.
  • Refactor createHandlers
    • Pulls out the non-looping part into createHandler which is nice because it can now early return instead of modifying a variable.

TODO

Could we add tests to see if the prosemirror doc was instantiated properly as well?

@vezwork vezwork force-pushed the refactor/pandoc-to-prosemirror branch 5 times, most recently from f02062a to 8b224d7 Compare August 5, 2025 21:11
@vezwork vezwork requested a review from cscheid August 5, 2025 22:02
@vezwork vezwork changed the title refactor code around conversion to prosemirror for my understanding refactor prosemirror conversion for understandability Aug 6, 2025
@vezwork vezwork force-pushed the refactor/pandoc-to-prosemirror branch 4 times, most recently from 488274d to 59e769c Compare August 12, 2025 14:46
@vezwork vezwork force-pushed the refactor/pandoc-to-prosemirror branch from 59e769c to 9302e30 Compare August 15, 2025 20:46
@vezwork
Copy link
Collaborator Author

vezwork commented Aug 15, 2025

The new tests in CI from #790 caught an error caused by this refactor! The visual editor is unable to open valid-nesting.qmd in this PR. It's exciting that the tests helped me!

On the other hand it's disappointing that my refactor is not flawless and perfect.

I know one thing that could have different behaviour than the original code (The fact that mapTokenAndContentTokensRecursively doesn't check if a token has children like the code previously did). I could take a look at that... Edit:I tried changing what I thought could be causing the error and it didn't fix it. I'd have to incrementally try the refactor's changes to see what's going wrong...

But I think I'm going to set aside this refactor for now. Writing this PR helped me understand the code, even if my refactor is not perfect. I will leave it up as a draft so I can reference it when I come back to this area.

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.

1 participant