Skip to content

Commit

Permalink
chore: add eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
uetchy committed Jul 3, 2020
1 parent 1d54d68 commit ad3bace
Show file tree
Hide file tree
Showing 17 changed files with 505 additions and 137 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
env: {
es2020: true,
node: true,
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 11,
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'no-console': 'error',
},
};
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
"@types/node": "^14.0.14",
"@types/refractor": "^2.8.0",
"@types/vfile": "^4.0.0",
"doctoc": "^1.4.0",
"@typescript-eslint/eslint-plugin": "^3.5.0",
"@typescript-eslint/parser": "^3.5.0",
"@uetchy/doctoc": "^1.5.0",
"eslint": "^7.3.1",
"husky": "^4.2.5",
"jest": "^26.1.0",
"lint-staged": "^10.2.11",
Expand All @@ -75,10 +78,20 @@
],
"husky": {
"hooks": {
"pre-commit": "doctoc README.md docs/vfm.md && git add README.md docs/vfm.md && pretty-quick --staged",
"pre-commit": "lint-staged && pretty-quick --staged",
"pre-push": "npm test"
}
},
"lint-staged": {
"*.ts": [
"eslint",
"git add"
],
"*.md": [
"doctoc -p",
"git add"
]
},
"publishConfig": {
"access": "public"
},
Expand Down
21 changes: 12 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fs from 'fs';
import meow from 'meow';
import readline from 'readline';
import {stringify} from '.';
import { stringify } from '.';

const cli = meow(
`
Expand Down Expand Up @@ -44,10 +44,10 @@ const cli = meow(
function convert(
input: string,
flags: meow.TypedFlags<{
style: {type: 'string'; alias: string};
partial: {type: 'boolean'; alias: string};
title: {type: 'string'};
language: {type: 'string'};
style: { type: 'string'; alias: string };
partial: { type: 'boolean'; alias: string };
title: { type: 'string' };
language: { type: 'string' };
}>,
) {
return stringify(input, {
Expand All @@ -60,16 +60,17 @@ function convert(

function main(
cli: meow.Result<{
style: {type: 'string'; alias: string};
partial: {type: 'boolean'; alias: string};
title: {type: 'string'};
language: {type: 'string'};
style: { type: 'string'; alias: string };
partial: { type: 'boolean'; alias: string };
title: { type: 'string' };
language: { type: 'string' };
}>,
) {
try {
const filepath = cli.input[0];

if (filepath) {
// eslint-disable-next-line no-console
return console.log(
convert(fs.readFileSync(filepath).toString(), cli.flags),
);
Expand All @@ -81,9 +82,11 @@ function main(
terminal: false,
});
rl.on('line', function (line) {
// eslint-disable-next-line no-console
console.log(convert(line, cli.flags));
});
} catch (err) {
// eslint-disable-next-line no-console
console.log(err.message);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/plugins/attr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import attr from 'remark-attr';

export const mdast = [
attr,
{
enableAtxHeaderInline: true,
scope: 'permissive',
elements: [
'link',
'atxHeading',
'strong',
'emphasis',
'code',
'deletion',
'reference',
'footnoteCall',
'autoLink',
],
},
];
4 changes: 2 additions & 2 deletions src/plugins/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Node } from 'unist';
import u from 'unist-builder';
import visit from 'unist-util-visit';

export function attacher() {
export function mdast() {
return (tree: Node) => {
visit<Code>(tree, 'code', (node) => {
const match = /^(.+?):(.+)$/.exec(node.lang ?? '');
Expand All @@ -16,7 +16,7 @@ export function attacher() {
node.data = { ...(node.data ?? {}), hProperties: { title } };
node.lang = lang;
if (node.position?.end.offset) {
node.position!.end.offset -= title.length + 1;
node.position.end.offset -= title.length + 1;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/fenced-block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Plugin} from 'unified';
import {roleMappingTable} from '../utils/wai-aria';
import { Plugin } from 'unified';
import { roleMappingTable } from '../utils/wai-aria';

const FENCE = ':';
const ROLE_SYMBOL = '@';
Expand All @@ -17,7 +17,7 @@ const tokenizer: Tokenizer = function (eat, value, silent) {

const fenceSymbol = FENCE.repeat(DEPTH + 3);
const match = new RegExp(
`^${fenceSymbol}\s*(.*?)\s*\\n([\\w\\W]+?)\\n${fenceSymbol}$`,
`^${fenceSymbol}\\s*([^\\s]*?)\\s*\\n([\\w\\W]+?)\\n${fenceSymbol}$`,
'm',
).exec(value);
if (!match) return;
Expand Down Expand Up @@ -56,10 +56,10 @@ const tokenizer: Tokenizer = function (eat, value, silent) {
tokenizer.notInLink = true;
tokenizer.locator = locator;

export const attacher: Plugin = function () {
export const mdast: Plugin = function () {
if (!this.Parser) return;

const {blockTokenizers, blockMethods} = this.Parser.prototype;
const { blockTokenizers, blockMethods } = this.Parser.prototype;
blockTokenizers.fencedBlock = tokenizer;
blockMethods.splice(blockMethods.indexOf('text'), 0, 'fencedBlock');
};
50 changes: 24 additions & 26 deletions src/plugins/figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@ interface HastNode extends HastParent {
properties: { [key: string]: any };
}

export function plugin(options = {}) {
return (tree: Node) => {
visit<HastNode>(tree, 'element', (node, index, parent) => {
// handle captioned code block
const maybeCode = node.children?.[0] as HastNode | undefined;
export const hast = () => (tree: Node) => {
visit<HastNode>(tree, 'element', (node, index, parent) => {
// handle captioned code block
const maybeCode = node.children?.[0] as HastNode | undefined;
if (is(node, 'pre') && maybeCode?.properties.title) {
const maybeTitle = maybeCode?.properties?.title;
if (is(node, 'pre') && maybeTitle) {
delete maybeCode!.properties.title;
(parent as Parent).children[index] = h(
'figure',
{ class: maybeCode!.properties.className[0] },
h('figcaption', maybeTitle),
node,
);
return;
}
delete maybeCode.properties.title;
(parent as Parent).children[index] = h(
'figure',
{ class: maybeCode.properties.className[0] },
h('figcaption', maybeTitle),
node,
);
return;
}

// handle captioned img
if (is(node, 'img') && node.properties.alt) {
(parent as Parent).children[index] = h(
'figure',
node,
h('figcaption', node.properties.alt),
);
}
});
};
}
// handle captioned img
if (is(node, 'img') && node.properties.alt) {
(parent as Parent).children[index] = h(
'figure',
node,
h('figcaption', node.properties.alt),
);
}
});
};
2 changes: 1 addition & 1 deletion src/plugins/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const removeMathML = () => (tree: Node) => {
});
};

export const plugin = { plugins: [katex, removeMathML] };
export const hast = { plugins: [katex, removeMathML] };
37 changes: 17 additions & 20 deletions src/plugins/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,22 @@ interface File extends VFile {

// https://github.com/Symbitic/remark-plugins/blob/master/packages/remark-meta/src/index.js

export function plugin() {
return (tree: Node, file: File) => {
const heading = select('heading', tree);
if (heading) {
file.data.title = toString(heading);
}
export const mdast = () => (tree: Node, file: File) => {
const heading = select('heading', tree);
if (heading) {
file.data.title = toString(heading);
}

visit<FrontmatterContent>(tree, ['yaml'], (node) => {
file.data = {
...file.data,
...yaml(node.value),
};
});
visit<FrontmatterContent>(tree, ['yaml'], (node) => {
file.data = {
...file.data,
...yaml(node.value),
};
});

file.data.toc = false;
visit<Literal>(tree, ['shortcode'], (node) => {
if (node.identifier !== 'toc') return;
console.log(node);
file.data.toc = true;
});
};
}
file.data.toc = false;
visit<Literal>(tree, ['shortcode'], (node) => {
if (node.identifier !== 'toc') return;
file.data.toc = true;
});
};
7 changes: 4 additions & 3 deletions src/plugins/ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const tokenizer: Tokenizer = function (eat, value, silent) {
if (silent) return true;

now.column += 1;
now.offset! += 1;
now.offset += 1;

return eat(eaten)({
type: 'ruby',
Expand All @@ -30,7 +30,7 @@ const tokenizer: Tokenizer = function (eat, value, silent) {
tokenizer.notInLink = true;
tokenizer.locator = locateRuby;

export const attacher: Plugin = function () {
export const mdast: Plugin = function () {
if (!this.Parser) return;

const { inlineTokenizers, inlineMethods } = this.Parser.prototype;
Expand All @@ -40,12 +40,13 @@ export const attacher: Plugin = function () {

// rehype
export const handler: Handler = (h, node) => {
if (!node.data) node.data = {};
const rtNode = h(
{
type: 'element',
},
'rt',
[u('text', node.data!.rubyText as string)],
[u('text', node.data.rubyText as string)],
);

return h(node, 'ruby', [...all(h, node), rtNode]);
Expand Down
26 changes: 13 additions & 13 deletions src/plugins/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import findAfter from 'unist-util-find-after';
import visit from 'unist-util-visit-parents';
import { roleMappingTable, roles } from '../utils/wai-aria';

// TODO: handle @subtitle properly

const MAX_HEADING_DEPTH = 6;

export function plugin() {
return (tree: any) => {
for (let depth = MAX_HEADING_DEPTH; depth > 0; depth--) {
visit(
tree,
(node: any) => {
return node.type === 'heading' && node.depth === depth;
},
sectionize as any,
);
}
};
}
export const mdast = () => (tree: any) => {
for (let depth = MAX_HEADING_DEPTH; depth > 0; depth--) {
visit(
tree,
(node: any) => {
return node.type === 'heading' && node.depth === depth;
},
sectionize as any,
);
}
};

function sectionize(node: any, ancestors: Parent[]) {
const start = node;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ const keepToC = () => (tree: Node) => {
});
};

export const plugin = {
export const mdast = {
plugins: [[shortcodes, { startBlock: '[[', endBlock: ']]' }], keepToC],
};
Loading

0 comments on commit ad3bace

Please sign in to comment.