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

add implicit link plugin #344

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/transform/plugins/implicit-link/implicit-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import StateCore from 'markdown-it/lib/rules_core/state_core';

export function replaceTokens(state: StateCore) {
const {tokens} = state;
for (let i = 0; i < tokens.length; i++) {
if (tokens[i].type !== 'inline' || !tokens[i].children) continue;

const splicePointsMap: Map<number, number> = new Map();

tokens[i].children?.forEach((children, childrenIndex) => {
if (children.type === 'text' && isUrl(children.content)) {
splicePointsMap.set(i, childrenIndex);
}
});

splicePointsMap.forEach((childrenIndex, tokenIndex) => {
const token = tokens[tokenIndex];

//TODO: refactor. children never nullable
if (!token.children) return;

const children = token.children[childrenIndex];

tokens.splice(tokenIndex - 1, 3, token);
token.children.splice(childrenIndex, 1, ...createLinkToken(state, children.content));
});
}
}

function isUrl(maybeUrl: string) {
try {
// eslint-disable-next-line no-new
new URL(maybeUrl);
} catch (err) {
return false;
}

return true;
}

function createLinkToken(state: StateCore, link: string) {
const linkOpenToken = new state.Token('link_open', 'a', 1);
linkOpenToken.attrSet('href', link);

const textToken = new state.Token('text', '', 0);
textToken.content = link;

const linkClose = new state.Token('link_close', 'a', -1);

return [linkOpenToken, textToken, linkClose];
}
10 changes: 10 additions & 0 deletions src/transform/plugins/implicit-link/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import MarkdownIt from 'markdown-it';
import {replaceTokens} from './implicit-link';

const implicitLink = (md: MarkdownIt) => {
md.core.ruler.after('links', 'implicit-link', replaceTokens);

return md;
};

export default implicitLink;
Binary file not shown.
3 changes: 3 additions & 0 deletions test/__snapshots__/implicit-link.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`implicit-link renders implicit-link 1`] = `"<a href=\\"https://google.com\\">https://google.com</a>"`;
14 changes: 14 additions & 0 deletions test/implicit-link.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import initMarkdown from '../src/transform/md';
import plugin from '../src/transform/plugins/block-anchor';
import implicitLink from '../src/transform/plugins/implicit-link';
import links from '../src/transform/plugins/links';

const {parse, compile} = initMarkdown({plugins: [plugin, links, implicitLink]});

describe('implicit-link', function () {
it('renders implicit-link', () => {
const input = 'https://google.com';
const actual = compile(parse(input));
expect(actual).toMatchSnapshot();
});
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"baseUrl": ".",
"declaration": true,
"target": "ES6",
"types": ["node", "jest"]
"types": ["node", "jest"],
"esModuleInterop": true
},
"include": ["src/**/*", "test/**/*"]
}
Loading