# Abbreviations: longest/shortest valid match?

**URL:** <https://talk.commonmark.org/t/abbreviations-longest-shortest-valid-match/3060>\
**Category:** Extensions\
**Created:** [January 26, 2019, 12:46pm UTC](https://talk.commonmark.org/t/abbreviations-longest-shortest-valid-match/3060 "2019-01-26T12:46:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MihaZupan](https://talk.commonmark.org/user_avatar/talk.commonmark.org/mihazupan/32/1506_2.png) [@MihaZupan](https://talk.commonmark.org/u/MihaZupan)\
**Post date:** [January 26, 2019, 12:46pm UTC](https://talk.commonmark.org/t/abbreviations-longest-shortest-valid-match/3060/1 "2019-01-26T12:46:16Z")

</div>

Consider the following example: ([babelmark](https://babelmark.github.io/?text=*%5BFoo%5D%3A+foo%0A*%5BBar%5D%3A+bar%0A*%5BFoo+Bar%5D%3A+foobar%0A%0AFoo+Bar))

```auto
*[Foo]: foo
*[Foo Bar]: foobar

Foo Bar

```

and the discrepancy between outputs:  
`<abbr title="foo">Foo</abbr> Bar` (e.g. php-markdown-extra) and  
` <abbr title="foobar">Foo Bar</abbr>` (e.g. Markdig)

I believe that the behaviour of Markdig is correct in this case, matching the longest possible abbreviation definition. Thoughts?

I’m new to this forum, so are there any plans for commonmark-unified specifications for commonly used extensions?

---

<div class="post-metadata">

**Author:** ![Crissov](https://talk.commonmark.org/user_avatar/talk.commonmark.org/crissov/32/1455_2.png) [@Crissov](https://talk.commonmark.org/u/Crissov)\
**Post date:** [January 26, 2019, 4:30pm UTC](https://talk.commonmark.org/t/abbreviations-longest-shortest-valid-match/3060/2 "2019-01-26T16:30:15Z")

</div>

I am surprised how many implementations support this abbreviation syntax, at least [Kramdown](http://kramdown.gettalong.org/), [Markdig](https://github.com/lunet-io/markdig/), [Maruku](https://github.com/bhollis/maruku), [Multimarkdown](http://fletcherpenney.net/multimarkdown/) and [PHP Markdown Extra](https://michelf.ca/projects/php-markdown/extra/).

> An _abbreviation definition_ is a line beginning with an asterisk `*`, optionally indented by up to three spaces, immediately followed by an opening square bracket `[`, followed by the _abbreviation_, followed a closing bracket `]`, immediately followed by a colon `:`, followed by optimal whitespace, followed by the _expansion_.

There is at least [one thing similar](https://babelmark.github.io/?text=-+%5Bfoo%5D%5Bnil%5D+-+foo+nil%0A-+%5Bnil%5D%5Bfoo%5D+-+nil+foo%0A-+%5Bfoo%5D%5Bbar%5D+-+foo+bar%0A-+%5Bbar%5D%5Bfoo%5D+-+bar+foo%0A-+%5Bfoo%5D%5Bbar%5D%5Bnil%5D+-+foo+bar+nil%0A-+%5Bbar%5D%5Bfoo%5D%5Bnil%5D+-+bar+foo+nil%0A-+%5Bnil%5D%5Bfoo%5D%5Bbar%5D+-+nil+foo+bar%0A-+%5Bnil%5D%5Bbar%5D%5Bfoo%5D+-+nil+bar+foo%0A-+%5Bfoo%5D%5Bnil%5D%5Bbar%5D+-+foo+nil+bar%0A-+%5Bbar%5D%5Bnil%5D%5Bfoo%5D+-+bar+nil+foo%0A%0A%5Bfoo%5D%3A+baz%0A%5Bbar%5D%3A+quuz+) in Commonmark to derive expected behavior of this extension from: pairs of square brackets immediately following each other that could either be interpreted as a reference link or a multiple shortcut reference links. Commonmark prefers the longer link syntax, even if the link text would resolve to a link definition but the link label does not.

```markdown
[foo][nil] 
[foo][bar] 

[foo]: baz
[bar]: quuz 
.
<p>[foo][nil] 
<a href="quuz">foo</a></p>

```

One should also note that all of the implementations that support this syntax match the longer term if it is specified first. Commonmark also [prefers the first definition](https://babelmark.github.io/?text=%5Bfoo%5D%0A%0A%5Bfoo%5D%3A+bar%0A%5Bfoo%5D%3A+baz%0A%0A%5Bfoo%5D+), although many legacy parsers prefer the last one.

```markdown
[foo]

[foo]: bar
[foo]: baz

[foo] 
.
<p><a href="bar">foo</a></p>
<p><a href="bar">foo</a></p>

```

So, the question is not actually about the longest or shortest match, but about the longest or first defined match. I tend to go for _first defined_.

---

<div class="post-metadata">

**Author:** ![MihaZupan](https://talk.commonmark.org/user_avatar/talk.commonmark.org/mihazupan/32/1506_2.png) [@MihaZupan](https://talk.commonmark.org/u/MihaZupan)\
**Post date:** [January 26, 2019, 4:59pm UTC](https://talk.commonmark.org/t/abbreviations-longest-shortest-valid-match/3060/3 "2019-01-26T16:59:28Z")

</div>

> [@Crissov](#):
>
> So, the question is not actually about the longest or shortest match, but about the longest or first defined match. I tend to go for _first defined_ .

I myself prefer there be no re-definitions (like with link references for example), hence in my [Markdown Validator](https://github.com/MihaZupan/MarkdownValidator) project, I will emit errors on markdown [with them](https://markdown-validator.ml/?md=%5BFoo%5D%0A%0A%5BFoo%5D%3A%20https%3A%2F%2Fcommonmark.org%2F%0A%5BFoo%5D%3A%20https%3A%2F%2Fspec.commonmark.org%2F).

Going by first defined, with markdown like the one in the original post, `Foo Bar` could never be matched.
