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

Specify start attribute on ordered lists #100

Closed
hkdobrev opened this issue Feb 13, 2014 · 15 comments
Closed

Specify start attribute on ordered lists #100

hkdobrev opened this issue Feb 13, 2014 · 15 comments

Comments

@hkdobrev
Copy link
Contributor

According to the Markdown spec any number with a following dot could start an ordered list.

Like so:

5. fifth
6. sixth
7. seventh

In the HTML5 spec there is a start attribute on ol elements which could indicate the start of the list.

So this should produce something like:

<ol start="5">
<li>fifth</li>
<li>sixth</li>
<li>seventh</li>
</ol>

Other implementations which do that:


Thanks should go to @wkpark for revealing it in #99.

@kminchev
Copy link

I don't like this and #101, and generally any feature specifically addressing HTML5. Not everyone is designing in HTML5.

@hkdobrev
Copy link
Contributor Author

@kminchev This part of the HTML5 spec is completely backwards-compatible. What exactly is your concern?

@hkdobrev
Copy link
Contributor Author

It is worth noting the ol[start] and li[value] attributes are part of HTML4 as well, but their semantics are just a little bit different and are deprecated from the final version of HTML4.

So they are not HTML5-only. They are just resurrected in HTML5.

@wkpark
Copy link
Contributor

wkpark commented Feb 14, 2014

I didn't know that the start attribute is the part of the Markdown spec :)

BTW the start attribute also supported by ReStructuredText

@wkpark
Copy link
Contributor

wkpark commented Feb 14, 2014

this is not the part of Markdown spec.

3. item #1
1. item #2
8. item #3
  1. item Add composer / packagist support #1
  2. item Fenced block support #2
  3. item Email links support #3

Markdown does not support "start" or "value" attributes

all numberings are ignored.

anyway I like "start" attribute and It is useful to support it optionally.

@scottchiefbaker
Copy link

This is specifically against the spec.

@wkpark
Copy link
Contributor

wkpark commented Feb 14, 2014

there are Text::Markdown perl module exists (version 1.0.31)

it has trust_list_start_value option to support "start" attrubute.

@kminchev
Copy link

anyway I like "start" attribute and It is useful to support it optionally.

Agreed. If any HTML5 addition is going to be implemented, I suggest it being optional.

@hkdobrev
Copy link
Contributor Author

@wkpark This is not part of the Markdown spec. It is just undefined by the spec. It used to be part of the HTML4 spec and it is part of the HTML5 spec.

@scottchiefbaker Why is this against the spec?

@wkpark
Copy link
Contributor

wkpark commented Feb 14, 2014

ReStructuredText have similar feature by default. (demo page: http://rst.ninjs.org/)

and as I already said, the Text::Markdown perl module have trust_list_start_value option to support start attribute.
(Text::Markdown is a fork of the original Markdown.pl)

the source code of Text::Markdown found at Github https://github.com/bobtfish/text-markdown

trust_list_start_value option is described as following

If true, ordered lists will use the first number as the starting point for
numbering. This will let you pick up where you left off by writing:

  1. foo
  2. bar

some paragraph

  3. baz
  6. quux
  1. foo
  2. bar

some paragraph

  1. baz
  2. quux

(Note that in the above, quux will be numbered 4.)

@erusev
Copy link
Owner

erusev commented Apr 24, 2014

@hkdobrev

@scottchiefbaker Why is this against the spec?

Probably, because of the following excerpt:

If you instead wrote the list in Markdown like this:

1.  Bird
1.  McHale
1.  Parish

or even:

3. Bird
1. McHale
8. Parish

you’d get the exact same HTML output. The point is, if you want to, you can use ordinal numbers in your ordered Markdown lists, so that the numbers in your source match the numbers in your published HTML. But if you want to be lazy, you don’t have to.

@rmjarvis
Copy link

+1 on this feature request. I am constantly bitten by this "feature" of markdown. e.g. if someone makes a list of 5 things in an issue, and I want to respond to the 4th one, I'll try to copy it out for a response:

> 4. The fourth item that someone talked about

This is already implemented.  See this issue...

Shows up as:

  1. The fourth item that someone talked about

This is already implemented. See this issue...

It would be nice to have some way to force the numbering to start where I want it, not at 1.

@erusev
Copy link
Owner

erusev commented Oct 17, 2015

@rmjarvis that's a good point, i'll se what i can do

@aidantwoods
Copy link
Collaborator

aidantwoods commented Sep 26, 2016

@erusev just taking a quick glance at the source for this issue

For the following section of code (which creates tags)
if (isset($Element['text'])) { $markup .= '>'; if (isset($Element['handler'])) { $markup .= $this->{$Element['handler']}($Element['text']); } else { $markup .= $Element['text']; } $markup .= '</'.$Element['name'].'>'; }
Would the following change cause any issues? $markup .= '</'.$Element['name'].'>'; to $markup .= '</'.preg_replace('/[ ].*/', '', $Element['name']).'>';
This should just strip everything after a space (if any, and including the space) out of the tag name (which a tag shouldn't have in it anyway at the close).

That change would allow you to change the blockList function from

protected function blockList($Line)
    {
        list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]');
        if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches))
        {
            $Block = array(
                'indent' => $Line['indent'],
                'pattern' => $pattern,
                'element' => array(
                    'name' => $name,
                    'handler' => 'elements',
                ),
            );
            $Block['li'] = array(
                'name' => 'li',
                'handler' => 'li',
                'text' => array(
                    $matches[2],
                ),
            );
            $Block['element']['text'] []= & $Block['li'];
            return $Block;
        }
    }

To something like

protected function blockList($Line)
    {
        list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '([*+-])') : array('ol', '([0-9]+)[.]');
        if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches))
        {
            $Block = array(
                'indent' => $Line['indent'],
                'pattern' => preg_replace('/\(|\)/', '', $pattern),
                'element' => array(
                    'name' => $name,
                    'handler' => 'elements',
                ),
            );
            if($name === 'ol' && $matches[2] !== '1') $Block['element']['attributes'] = array('start' => $matches[2]);
            $Block['li'] = array(
                'name' => 'li',
                'handler' => 'li',
                'text' => array(
                    $matches[3],
                ),
            );
            $Block['element']['text'] []= & $Block['li'];
            return $Block;
        }
    }

All I've done is put an additional capture group round the number on the ol pattern, and the entire expression on the ul pattern (so we keep the same amount of capture groups in each expression). I've then corrected the $matches[2] to $matches[3] for the list item text (since I've added an additional capture group before it). And I've then added an if statement to append a start value (the list number captured) if the list type is ol and the number is something other than 1.

I've then relied on the fact that my adjustment to the tag creation function will strip that start value out of the name when it closes the tag (since a space precedes the start attribute).

Edit: I've added a pull request #431 that you can merge if those changes look okay.

Edit: Have amended the code in this comment to return the original pattern into the $Block array (so the rest of the code works as expected).

aidantwoods added a commit to aidantwoods/parsedown that referenced this issue Sep 26, 2016
(I think this should work)
Allow parsedown to specify list start attribute (see: erusev#100 (comment))
@aidantwoods
Copy link
Collaborator

Fixed in #431

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants