Skip to content

Commit

Permalink
add support for markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
vinmaster committed Oct 8, 2021
1 parent 32bc6b7 commit 48ecd60
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Javascript } from './languages/javascript';
import { Julia } from './languages/julia';
import { Kotlin } from './languages/kotlin';
import { Lua } from './languages/lua';
import { Markdown } from './languages/markdown';
import { Pascal } from './languages/pascal';
import { PHP } from './languages/php';
import { Python } from './languages/python';
Expand All @@ -36,6 +37,7 @@ const languages: Record<string, LanguagePattern[]> = {
Julia,
Kotlin,
Lua,
Markdown,
Pascal,
PHP,
Python,
Expand Down
22 changes: 22 additions & 0 deletions src/languages/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { LanguagePattern } from '../types';

export const Markdown: LanguagePattern[] = [
// headings
{ pattern: /(#){1,6} .+/, type: 'macro', nearTop: true },
// headings alternate syntax
{ pattern: /^(?!!)(=|-){2,}(?<!>)$/, type: 'meta.module', nearTop: true },
// bold
{ pattern: /(\w*(?!\/)(\*\*)\w+(\*\*)\w*)|(\w*(__)\w+(__)\w*)/, type: 'macro', nearTop: true },
// italic
{ pattern: /(^.*(\*).+(\*)(?<!\/).*)$|^(.*(_).+(_).*)$/, type: 'macro', nearTop: true },
// lists
{ pattern: /^(?!-)(- \w*)(?<!-)|^(?!\/)(\* .*)/, type: 'meta.module', nearTop: true },
// images
{ pattern: /!\[.+\]\(.+\)/, type: 'macro', nearTop: true },
// links
{ pattern: /\[.+\]\(.+\)/, type: 'macro', nearTop: true },
// blockquotes
{ pattern: /^(> .*)+/, type: 'macro', nearTop: true },
// inline code
{ pattern: /.*`.+`.*/, type: 'meta.module', nearTop: true },
];
1 change: 1 addition & 0 deletions tests/cpp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test('hello world', () => {
SQL: 0,
Unknown: 1,
YAML: 0,
Markdown: 0,
});
});

Expand Down
1 change: 1 addition & 0 deletions tests/cs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test('hello world', () => {
SQL: 0,
Unknown: 1,
YAML: 0,
Markdown: 0,
});
});

Expand Down
1 change: 1 addition & 0 deletions tests/large.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ test('large input', () => {
SQL: 24,
Unknown: 1,
YAML: 4,
Markdown: 9,
});
});

Expand Down
97 changes: 97 additions & 0 deletions tests/markdown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import detectLang from '../src/index';

test('heading 1', () => {
const code = detectLang('# Heading level 1');
assert.equal(code, 'Markdown');
});

test('heading 2', () => {
const code = detectLang('## Heading level 2');
assert.equal(code, 'Markdown');
});

test('heading 3', () => {
const code = detectLang('### Heading level 3');
assert.equal(code, 'Markdown');
});

test('heading 4', () => {
const code = detectLang('#### Heading level 4');
assert.equal(code, 'Markdown');
});

test('heading 5', () => {
const code = detectLang('##### Heading level 5');
assert.equal(code, 'Markdown');
});

test('heading 6', () => {
const code = detectLang('###### Heading level 6');
assert.equal(code, 'Markdown');
});

test('heading 1 alternate syntax', () => {
const code = detectLang('Heading level 1\n============');
assert.equal(code, 'Markdown');
});

test('heading 2 alternate syntax', () => {
const code = detectLang('Heading level 1\n------------');
assert.equal(code, 'Markdown');
});

test('bold syntax 1', () => {
const code = detectLang('**This text will be bold**');
assert.equal(code, 'Markdown');
});

test('bold syntax 2', () => {
const code = detectLang('__This will also be bold__');
assert.equal(code, 'Markdown');
});

test('italic syntax 1', () => {
const code = detectLang('*This text will be italic*');
assert.equal(code, 'Markdown');
});

test('italic syntax 2', () => {
const code = detectLang('_This will also be italic_');
assert.equal(code, 'Markdown');
});

test('list syntax 1', () => {
const code = detectLang(`* Item 1`);
assert.equal(code, 'Markdown');
});

// FIXME: Conflicts with YAML
test.skip('list syntax 2', () => {
const code = detectLang(`- Item 1`);
assert.equal(code, 'Markdown');
});

test('images', () => {
const code = detectLang(`![GitHub Logo](/images/logo.png)`);
assert.equal(code, 'Markdown');
});

test('links', () => {
const code = detectLang(`[GitHub](http://github.com)`);
assert.equal(code, 'Markdown');
});

test('blockquotes', () => {
const code = detectLang(`> We're living the future so
> the present is our past.`);
assert.equal(code, 'Markdown');
});

test('inline code', () => {
const code = detectLang('I think you should use an`<addr>` element here instead.');
assert.equal(code, 'Markdown');
});

test.run();

0 comments on commit 48ecd60

Please sign in to comment.