Skip to content

Commit

Permalink
feat(ghMentions): add support for github's @mentions
Browse files Browse the repository at this point in the history
Closes #51
  • Loading branch information
tivie committed Jan 6, 2017
1 parent 561dc5f commit f2671c0
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 12 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
@@ -1,11 +1,6 @@
<a name="1.5.5"></a>
## [1.5.5](https://github.com/showdownjs/showdown/compare/1.5.4...1.5.5) (2016-12-30)


### Bug Fixes

* **ghCompatibleHeaderId:** add % as an escaped char ([3102615](https://github.com/showdownjs/showdown/commit/3102615))

### Features

* **ghCompatibleHeaderId:** generate header ids compatible with github ([db97a90](https://github.com/showdownjs/showdown/commit/db97a90)), closes [#320](https://github.com/showdownjs/showdown/issues/320) [#321](https://github.com/showdownjs/showdown/issues/321)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -292,6 +292,8 @@ var defaultOptions = showdown.getDefaultOptions();
* **requireSpaceBeforeHeadingText**: (boolean) [default false] Makes adding a space between `#` and the header text mandatory (since v1.5.3)
* **ghMentions**: (boolean) [default false] Enables github @mentions, which link to the username mentioned (since v1.5.6)
## Flavors
You can also use flavors or presets to set the correct options automatically, so that showdown behaves like popular markdown flavors.
Expand Down
16 changes: 14 additions & 2 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/options.js
Expand Up @@ -100,6 +100,11 @@ function getDefaultOpts(simple) {
defaultValue: false,
description: 'Makes adding a space between `#` and the header text mandatory (GFM Style)',
type: 'boolean'
},
ghMentions: {
defaultValue: false,
description: 'Enables github @mentions',
type: 'boolean'
}
};
if (simple === false) {
Expand Down
3 changes: 2 additions & 1 deletion src/showdown.js
Expand Up @@ -22,7 +22,8 @@ var showdown = {},
disableForced4SpacesIndentedSublists: true,
simpleLineBreaks: true,
requireSpaceBeforeHeadingText: true,
ghCompatibleHeaderId: true
ghCompatibleHeaderId: true,
ghMentions: true
},
vanilla: getDefaultOpts(true),
allOn: allOptionsOn()
Expand Down
7 changes: 6 additions & 1 deletion src/subParsers/anchors.js
Expand Up @@ -59,11 +59,16 @@ showdown.subParser('anchors', function (text, options, globals) {
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
writeAnchorTag);

// Last, handle reference-style shortcuts: [link text]
// handle reference-style shortcuts: [link text]
// These must come last in case you've also got [link test][1]
// or [link test](/foo)
text = text.replace(/(\[([^\[\]]+)])()()()()()/g, writeAnchorTag);

// Lastly handle GithubMentions if option is enabled
if (options.ghMentions) {
text = text.replace(/(^|\s)(@([a-z\d\-]+))(?=[.!?;,[\]()]|\s|$)/gmi, '$1<a href="https://www.github.com/$3">$2</a>');
}

text = globals.converter._dispatch('anchors.after', text, options, globals);
return text;
});
Empty file added src/subParsers/ghMentions.js
Empty file.
2 changes: 2 additions & 0 deletions test/features/ghMentions.html
@@ -0,0 +1,2 @@
<p>hello <a href="https://www.github.com/tivie">@tivie</a> how are you?</p>
<p>this email foo@gmail.com is not parsed</p>
3 changes: 3 additions & 0 deletions test/features/ghMentions.md
@@ -0,0 +1,3 @@
hello @tivie how are you?

this email foo@gmail.com is not parsed
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Expand Up @@ -51,6 +51,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({requireSpaceBeforeHeadingText: true});
} else if (testsuite[i].name === '#320.github-compatible-generated-header-id') {
converter = new showdown.Converter({ghCompatibleHeaderId: true});
} else if (testsuite[i].name === 'ghMentions') {
converter = new showdown.Converter({ghMentions: true});
} else {
converter = new showdown.Converter();
}
Expand Down

0 comments on commit f2671c0

Please sign in to comment.