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

Add <tree> component for folder structure visualisations #1807

Merged
merged 20 commits into from
Mar 16, 2022

Conversation

tlylt
Copy link
Contributor

@tlylt tlylt commented Mar 7, 2022

What is the purpose of this pull request?

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

Overview of changes:
Fixes #1660

Provides a convenient way to create lightweight tree-like textual output, suitable for folder structure visualisations.
Syntax:

<tree>
Edit me to generate
  a
    nice
      tree
        diagram!
        :)
</tree>
<tree>
Edit me to generate
  - a
    - nice
    - tree
        - diagram!
        - :)
</tree>

To

Edit me to generate
└── a
    └── nice
        └── tree
            ├── diagram!
            └── :)

More details in UG's tree section

A summary of the work done:

  • feature development in core/src/plugins/default/markbind-plugin-tree.js
  • unit test in core/test/unit/plugins/default/tree.test.js
  • functional test in packages/cli/test/functional/test_site/testTree.md
  • UG inclusion under Using Component - Images & Diagrams - Tree
  • Migration of the existing folder structure code in UG over to use the new tree syntax, mainly in Reusing Contents
  • Update tests

Anything you'd like to highlight / discuss:

  • Initially thought of making a Vue component, but went with making a default plugin instead (since the feature itself is quite lightweight)
  • The currently supported syntax uses spaces + unordered list bullets for indication of indent/level, I am not sure whether I should also support (chop off) ordered list bullets (basically, numbers like 1., 2. etc).
  • The support for inline markdown/HTML elements works for most syntax, but not for tooltip/popover
<tooltip id="tt:trigger_id" content="This tooltip triggered by a trigger"></tooltip>

<tree>
C:/course/
  textbook/
    <tooltip content="hello">index.md</tooltip>
    <trigger for="tt:trigger_id">via trigger</trigger>
</tree>

Testing the above code will give:

<div class="tree">C:/course/
├── <span data-mb-component-type="tooltip" tabindex="0" content="hello" class="trigger"><!----> <!----> index.md</span>
└── <span tabindex="0" class="trigger"><!----> via trigger</span></div>

image

Notice the white space added in front of the words "index.md" and "via trigger". While the direct tooltip approach does not work on hover, the trigger approach works. Not too sure if this is a bug with the tooltip/trigger element or is it due to the newly added tree component.

Testing instructions:
Tests cases are added in

  • packages/cli/test/functional/test_site/testTree.md
  • packages/core/test/unit/plugins/default/tree.test.js

User guide section

  • tree
  • see some migration of the folder structure code in this page

Proposed commit message: (wrap lines at 72 characters)
Add component for folder structure visualisations

It is tedious to manually create tree-like structure diagrams.

Let's add a component to conveniently convert
an indented list of items into tree-like textual output.

This is especially useful for file system visualisations.


Checklist: ☑️

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

Copy link
Contributor

@jonahtanjz jonahtanjz left a comment

Choose a reason for hiding this comment

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

Very nicely done @tlylt 👍 Tried it out and so far it is working well :)

Just a quick review on the design and documentation first.

docs/userGuide/syntax/tree.md Outdated Show resolved Hide resolved
@jonahtanjz
Copy link
Contributor

Notice the white space added in front of the words "index.md" and "via trigger". While the direct tooltip approach does not work on hover, the trigger approach works. Not too sure if this is a bug with the tooltip/trigger element or is it due to the newly added tree component.

<div class="tree">C:/course/
├── <span data-mb-component-type="tooltip" tabindex="0" content="hello" class="trigger"><!----> <!----> > index.md</span>
└── <span tabindex="0" class="trigger"><!----> via trigger</span></div>

Seems like an issue with the way the tree processes the node? Notice that the content="hello" attribute is still there on the HTML output which means the tooltip processor did not get to change the content into a <slot>.

If we try putting the tree code manually with the tooltip, it doesn't seem to have an issue.

<div class="tree">
  C:/course/
  └── textbook/
      └── <tooltip content="hello">index.md</tooltip>
</div>

image

Not entirely sure what is causing this but somehow after the tree plugin processes the code, the tooltip does not get processed.

@tlylt
Copy link
Contributor Author

tlylt commented Mar 9, 2022

If we try putting the tree code manually with the tooltip, it doesn't seem to have an issue.

<div class="tree">
  C:/course/
  └── textbook/
      └── <tooltip content="hello">index.md</tooltip>
</div>

image

Not entirely sure what is causing this but somehow after the tree plugin processes the code, the tooltip does not get processed.

It seems like the problem of empty spaces added in front of the word 'index.md' is a separate issue. Is this a bug with the tooltip component? 🤔

@jonahtanjz
Copy link
Contributor

jonahtanjz commented Mar 9, 2022

It seems like the problem of empty spaces added in front of the word 'index.md' is a separate issue. Is this a bug with the tooltip component? 🤔

Seems like the the usual tooltip component will put in a white space before the text but this is usually ignored/removed with white-space: normal (default)

image

However, since the tree component has the css rule white-space: pre, white spaces will be preserved leading to the empty space.

I guess we can remove the white space generated from the tooltip/trigger components if possible.

@tlylt
Copy link
Contributor Author

tlylt commented Mar 10, 2022

Seems like the the usual tooltip component will put in a white space before the text but this is usually ignored/removed with white-space: normal (default)

Seems like this is indeed the issue. Curious how did you find that out 😅? I was looking over the code for the Tooltip/Trigger Vue components and couldn't find something similar to that effect.

@jonahtanjz
Copy link
Contributor

Seems like this is indeed the issue. Curious how did you find that out 😅? I was looking over the code for the Tooltip/Trigger Vue components and couldn't find something similar to that effect.

I noticed the white space by looking at the HTML code output on the browser. Since the Tooltip/Trigger component rely on the slot component for the content, which is transformed using the processAttributeWithoutOverride function, my guess is that the space is introduced during the processing of attributes here.

Copy link
Contributor

@jonahtanjz jonahtanjz left a comment

Choose a reason for hiding this comment

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

Other than the tooltip/trigger issue, I think this PR is good to merge.

Might also want to add a note in the documentation to show that tooltip/trigger is not supported in the tree component yet and open a new issue tracker for this.

EDIT: There seems to be some merge conflict as well.

Copy link
Contributor

@jonahtanjz jonahtanjz left a comment

Choose a reason for hiding this comment

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

Thanks @tlylt!

LGTM 👍

@jonahtanjz jonahtanjz added this to the 4.0 milestone Mar 15, 2022
@jonahtanjz jonahtanjz merged commit 3d8495a into MarkBind:master Mar 16, 2022
}
node.name = 'div';
node.attribs.class = node.attribs.class ? `${node.attribs.class} tree` : 'tree';
node.children[0].data = TreeNode.visualize(node.children[0].data);
Copy link
Contributor

Choose a reason for hiding this comment

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

DomParser nodes returned by htmlparser have various types, with the isSpecial option specified the (only) child is treated as text (much like script/style tags).

You could use cheerio apis here instead to attach the visualized string as html, which solves the main (non-whitespace) issue with #1831.

Copy link
Contributor

@ang-zeyu ang-zeyu Mar 16, 2022

Choose a reason for hiding this comment

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

Debugger (do set it up if you haven't! might be rather useful for resolving low-level issueslike this 🙂):
Untitled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You could use cheerio apis here instead to attach the visualized string as html

Do you mean changing it to something like:

  processNode: (pluginContext, node) => {
    if (node.name !== 'tree') {
      return;
    }
    cheerio(node).text(TreeNode.visualize(cheerio(node).text()));
    cheerio(node).addClass('tree');
    node.name = 'div';
  },

I tried this but doesn't seem to resolve the issue with tooltips (still has white space + does not show up on hover).

Copy link
Contributor

Choose a reason for hiding this comment

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

yup, but this is the wrong method .text( (still attaching as text, not html)

Copy link
Contributor

Choose a reason for hiding this comment

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

reping in case this was missed @tlylt.
though not urgent, we can put up an issue if no time to test it yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reping in case this was missed @tlylt. though not urgent, we can put up an issue if no time to test it yet.

Thanks for the reminder. I have tested and setting it via .html (iirc) does achieve the effect as you mentioned. This resolves the white-space issue but triggers are still quite buggy/ not functional. I have #1831 to keep track of this and was thinking to investigate along with the comment #1831 (comment) before pushing a fix.

@damithc
Copy link
Contributor

damithc commented Mar 24, 2022

Minor: perhaps we can also add something like this to the examples given in the documentation? (it uses :far-folder: and far-file). I prefer to give examples that shows how the feature is used in realistic 'best-effect' scenarios.

image

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.

Support a way to visualize folder structures
4 participants