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

using #each in Markdown #7

Closed
mvolkmann opened this issue Feb 28, 2020 · 18 comments
Closed

using #each in Markdown #7

mvolkmann opened this issue Feb 28, 2020 · 18 comments

Comments

@mvolkmann
Copy link

Should this work? The first hardcoded table does work. The second one that uses #each does not. It puts the whole row, including the vertical bars, in the first column of each row.

<script>
  const dogs = [
   {name: 'Dasher', gender: 'male', breed: 'Whippet'},
   {name: 'Maisey', gender: 'female', breed: 'Treeing Walker Coonhound'},
   {name: 'Ramsey', gender: 'male', breed: 'Native American Indian Dog'},
   {name: 'Oscar ', gender: 'male', breed: 'German Shorthaired Pointer'}
  ];
</script>

# dogs

| Name   | Gender | Breed                      |
| ------ | :----: | -------------------------- |
| Dasher |  male  | Whippet                    |
| Maisey | female | Treeing Walker Coonhound   |
| Ramsey |  male  | Native American Indian Dog |
| Oscar  |  male  | German Shorthaired Pointer |

| Name | Gender | Breed |
| ---- | :----: | ----- |
{#each dogs as {name, gender, breed}}
| {name} | {gender} | {breed} |
{/each}

<style>
  h1 {
    color: blue;
    text-transform: uppercase;
    font-size: 4em;
  }

  table {
    border-collapse: collapse;
  }

  td, th {
    border: solid lightgray 3px;
    padding: 0.5rem;
  }

  th {
    background-color: pink;
  }
</style>
@AlexxNB
Copy link
Owner

AlexxNB commented Feb 28, 2020

No, it doens't work. I didn't implement this case. I will try to make it work.

@AlexxNB
Copy link
Owner

AlexxNB commented Feb 28, 2020

Check the v2.6.0 on the NPM. I added support for tables in each blocks.

@mvolkmann
Copy link
Author

Excellent! Now I'm wondering whether #if is supported and if it can be nested. For example, these do not work currently. Should they?

{#if dogs.length > 2}
  {#each dogs as {name, gender, breed}}
    | {name} | {gender} | {breed} |
  {/each}
{/if}

and

{#each dogs as {name, gender, breed}}
  {#if breed === 'male'}
    | {name} | {gender} | {breed} |
  {/if}
{/each}

@mvolkmann
Copy link
Author

mvolkmann commented Feb 28, 2020

It seems it is very important to not have a blank line before the #each. For example:

| Name | Gender | Breed |

{#each dogs as {name, gender, breed}}
| {name} | {gender} | {breed} |
{/each}

will render this:
Screen Shot 2020-02-28 at 7 48 23 AM

Removing the blank line will render this:
Screen Shot 2020-02-28 at 7 48 41 AM

One thing that makes this tricky that is out of your control is that some code formatters will add the blank line. I use VS Code with the Prettier extension. When I save changes to a .md file, it adds that blank line. To work around this I used Vim to edit the file.

@mvolkmann
Copy link
Author

Minor documentation typo: At https://alexxnb.github.io/svelte-preprocess-markdown/, "handeled" should be "handled". Great work by the way!

@AlexxNB
Copy link
Owner

AlexxNB commented Feb 28, 2020

For example, these do not work currently. Should they?

No, it doesn't work now. My suggesion is to filter dogs array before put it into each block.

It seems it is very important to not have a blank line before the #each.

Will check tomorrow

Minor documentation typo

Yeah, my english is poor. Thanks =)

@AlexxNB
Copy link
Owner

AlexxNB commented Feb 29, 2020

One thing that makes this tricky that is out of your control is that some code formatters will add the blank line.

Will the blank line be added by formatters, if you will write |---|---|---| also?

Your example does not looks like valid MD table.

By the way, MDSv syntax is not valid MD and is not valid Svelte. So Prettier never will work correctly with this format.

@mvolkmann
Copy link
Author

If I remove the spaces around the dashes and save the file in VS Code, something (maybe Prettier) is putting them back in. I don't expect there is anything you can do about this.

I think the only think left to consider for this issue is whether your library can support nested #each and #if blocks.

@AlexxNB
Copy link
Owner

AlexxNB commented Mar 2, 2020

I think the only think left to consider for this issue is whether your library can support nested #each and #if blocks.

Reworked table handling, now it supports all logic for tables. Check tested cases here https://raw.githubusercontent.com/AlexxNB/svelte-preprocess-markdown/master/tests/9-table-in-each.md

Try v2.7.0 on NPM.

@AlexxNB
Copy link
Owner

AlexxNB commented Mar 2, 2020

@mvolkmann will you mention my preprocessor in your book? =)

@mvolkmann
Copy link
Author

mvolkmann commented Mar 2, 2020 via email

@AlexxNB
Copy link
Owner

AlexxNB commented Mar 2, 2020

Awesome! =)

@mvolkmann
Copy link
Author

mvolkmann commented Mar 2, 2020 via email

@AlexxNB
Copy link
Owner

AlexxNB commented Mar 2, 2020

Sorry, can't figure out the "here" place =)
You can find me at Svelte's Discord for private messaging. (nickname - AlexxNB).

@mvolkmann
Copy link
Author

mvolkmann commented Mar 3, 2020 via email

@AlexxNB
Copy link
Owner

AlexxNB commented Mar 3, 2020

I edited the comment after found wrong version there. You are tracking issues by email, so you have only first version of the comment. v.2.7.0 is the last at the moment with implemented nested logic. You may be interesting with else syntax also. It produces table row with spanned cell, when there is only one paragraph in the else block:

{#each ... } 
.... 
{:else}
No entries
{/each]

will be processed to:

<table> 
...
<tr><td colspan="num_of_columns_in_the_head>">No entries</td></tr>
...
</table>

Maybe it is good idea to add align="center" also?

@AlexxNB AlexxNB closed this as completed Mar 29, 2020
@mvolkmann
Copy link
Author

Thanks Alexey!

@AlexxNB
Copy link
Owner

AlexxNB commented Mar 30, 2020

You are welcome. Will buy the papper book as soon as published. =)

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

No branches or pull requests

2 participants