-
Notifications
You must be signed in to change notification settings - Fork 2
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
Refactor: make helper functions more universal/modular #30
Comments
Replying to the comment from @absorpheus
They can be both inside the contextualText and at the end of it. My initial thought was to concentrate on the "end of a block" case, as it should be easier to catch and more relevant in terms of our initial goal. I want to play around with regular expressions, specifically, word boundaries and see whether it helps. |
* test: add tests to demonstrate that using a combination of preserveNewLineIndentation and removeAllLastNewlines helper functions can handle the edge cases specified in the issue
I pushed a commit to this branch containing tests which demonstrate handling the edge cases stated in the problem overview above using both Let me know if this meets the requirements in terms of functionality and if there's anything that I've missed. My thinking is to first prove we can handle these edge cases and only then begin to refactor to a more elegant solution. I look forward to seeing what you come up with in terms of regex and word boundaries. |
* add createTextBlockHandler * add createTextBlockHandlerChain * add handleTextForContext * refactor test
I pushed a commit to this branch I created a higher-order function (HOF) obsidian-apple-books-highlights-plugin/src/utils/createTextBlockHandlerChain.ts Lines 1 to 9 in fbdc9be
We can use
And then use
I also created a helper function
We can then use obsidian-apple-books-highlights-plugin/src/utils/preserveNewlineIndentation.ts Lines 1 to 7 in fbdc9be
I think this way we can test each handler function separately and test |
@absorpheus Thanks for the POC, it looks great and I do like this approach. I also spent some time with chatGPT elaborating more on this idea, and high-order functions is one reasonable way to go. But I also had a second thought about the solution in general. Higher-order functions add another layer of complexity (and even may be considered a kind of an advanced technique), however, considering the overall simplicity of the current codebase, I would rather avoid adding this complexity for as long as possible, because I want to keep the codebase human-friendly and easy to understand. Also, considering that currently we only have two cases with 1) preserving indentation and 2) removing trailing newlines and spaces, I tend to think that it would be over-engineering for these pretty simple use cases, at least for now. Plus, we don't know (yet) is there something else in text blocks that should be fixed, so potentially, we can end up with those two cases only. So my suggestion is to proceed with separate helper functions for as long as we can, chaining a few of them (up to 3) wouldn't be a problem for now. And let's wait for the users' feedback to understand whether we need to iterate further on this. And your PR along with this issue will be placed on hold until the next round of discussion. P.S: Please, don't think that I'm rejecting your solution, or discarding your efforts. Absolutely not. I was the initiator of this discussion to validate the idea and understand possible solutions. And I'm just trying not to jump into the boat of unjustified code complexity too early without proper confirmation of whether it makes sense now. |
@absorpheus No objections from my side on the chosen approach. const removeTrailingSpaces = (textBlock: string): string => {
const endLineSpaces = /\s+$/;
return endLineSpaces.test(textBlock) ? textBlock.replace(endLineSpaces, '') : textBlock;
} Following up your question and my initial response about newlines inside a string, I believe we don't need to worry about this case - I checked out several templates, including one with quote wrappers, everything was rendered as expected. To go further, I tried to combine it with the const preserveNewlineIndentation = (textBlock: string): string => {
const stringWithNewLinesAndSpaces = /\n+\s*|\s+$/g;
return stringWithNewLinesAndSpaces.test(textBlock) ?
// what value should <replace-symbol> have here?
textBlock.replace(stringWithNewLinesAndSpaces, <replace-symbol>) :
textBlock;
} Still, a bit clumsy and not pretty straightforward as long as regexes are involved. So at this point, my vote goes for two separate functions as a cleaner, and more human-friendly solution. |
…#30) * Add removeTrailingSpaces helper * Add tests for removeTrailingSpaces * Add tests for preserveNewlineIndentation
@bandantonio Thank you for your detailed replies above. I completely agree with you that using two separate helper functions is a lot simpler and keeps the codebase more readable. HOF's is probably overkill for what we're trying to achieve in this context. I've pushed a new commit which uses the Let me know what you think. |
This issue is a continuation of the discussion from #28
Problem overview
All the highlights are accompanied by a so-called representative text that acts as a full version of the sentence from which the highlight is taken. Depending on a book structure and highlighted fragments, the representative text may contain excessive symbols, like trailing spaces, newlines, tabs, or a combination of them. This results in newlines within imported highlight blocks, that add confusion as it may seem like two separate highlight blocks. For example:
The initial solution was proposed in #28 and it acts as a good starting point. However, the solution doesn't cover following cases (yet):
along the way.
reboot?\n\n
project.\n\n\n
simple answers.\n\t\t
success.\n\t\t\t
instead.\n\t\t\t\t\n\t\t\t\t\n\t\t\t\t\t
a book\n\t\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t
The proposed solution is similar to the function created for preserving indentation in text blocks: check
preserveNewlineIndentation
function in the src/methods/aggregateDetails.ts.Proposal
Below are options under discussion:
The text was updated successfully, but these errors were encountered: