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

markdown checker #226

Draft
wants to merge 36 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e3d07a9
added source code reader
Jul 1, 2019
527c524
moved package into the tools folder
Jul 2, 2019
5cf1533
downgraded version from 1 to 0
Jul 2, 2019
1f6afca
Update tablerow object dot notation
oyilmaztekin Jul 2, 2019
de265d5
update private property
oyilmaztekin Jul 2, 2019
cde54af
Update Object dot notation
oyilmaztekin Jul 2, 2019
b01b4a5
Update test description
oyilmaztekin Jul 2, 2019
ade9c4c
Update test description
oyilmaztekin Jul 2, 2019
5204588
Update dot notation in test
oyilmaztekin Jul 2, 2019
346b29a
Update dot notation
oyilmaztekin Jul 2, 2019
2545fe6
Update destructuring assignment
oyilmaztekin Jul 2, 2019
853044f
reduced analyzeTable function
Jul 2, 2019
421e31d
Merge branch 'ozer-check-markdown' of https://github.com/oyilmaztekin…
Jul 2, 2019
a78ac0b
eslint integration
Jul 8, 2019
0601099
added new line
Jul 8, 2019
4c96e44
Merge pull request #1 from oguzzkilic/patch-1
oyilmaztekin Jul 8, 2019
a9710f7
added tested for resolver, parser and recursive test for tokenization
oyilmaztekin Jul 28, 2019
0df11c5
Merge branch 'master' of https://github.com/tc39/proposals into ozer-…
oyilmaztekin Nov 7, 2019
5325939
added test for collectLinkDefinitions.js
oyilmaztekin Nov 7, 2019
2f5a10b
added detectTables test
oyilmaztekin Nov 10, 2019
d068b15
added tables Represantation mock
oyilmaztekin Nov 11, 2019
1d15acb
fixed grammar
oyilmaztekin Nov 12, 2019
8ba807c
Merge branch 'master' into ozer-check-markdown
Sep 3, 2021
f19f819
adds handler for table head
Sep 7, 2021
829789a
removes unused function
oyilmaztekin Sep 7, 2021
cf70ee9
adds cell handler
oyilmaztekin Sep 10, 2021
b7cce4d
fixes handlers to group array by proposals
oyilmaztekin Sep 10, 2021
84d8c3d
makes refactoring on handleTables function
oyilmaztekin Sep 10, 2021
8a433ba
Merge branch 'master' into ozer-check-markdown
oyilmaztekin Sep 10, 2021
142a78b
adds doc into index.js
oyilmaztekin Sep 10, 2021
215ea12
ignores editor files
oyilmaztekin Sep 10, 2021
a14e799
removes extra spaces
oyilmaztekin Sep 10, 2021
43683bc
removes extra spaces
oyilmaztekin Sep 10, 2021
812d0ca
removes .gitignore
oyilmaztekin Sep 10, 2021
e591bdd
creates final JSON for stage1
oyilmaztekin Sep 11, 2021
6a347ee
adds some refactoring
oyilmaztekin Sep 13, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
#chunks
.DS_Store
.cache

#editor files
.idea/**
oyilmaztekin marked this conversation as resolved.
Show resolved Hide resolved
.vscode/**
1 change: 1 addition & 0 deletions tools/markdown-checker/.eslintignore
@@ -0,0 +1 @@
*_tests_*
21 changes: 21 additions & 0 deletions tools/markdown-checker/.eslintrc.json
@@ -0,0 +1,21 @@
{
"env": {
"node": true,
"es6": true
},
"extends": [
"airbnb-base"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"no-underscore-dangle": 0,
"import/no-extraneous-dependencies": 0,
"no-unused-vars": 0
}
}
21 changes: 21 additions & 0 deletions tools/markdown-checker/.gitignore
@@ -0,0 +1,21 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# environment variables
.env
.env.test
*/node_modules/*
yarn.lock

#chunks
.DS_Store
.cache
**/.idea/**

# packages
node_modules/
*/node_modules/*
3 changes: 3 additions & 0 deletions tools/markdown-checker/.prettierrc
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
29 changes: 29 additions & 0 deletions tools/markdown-checker/README.md
@@ -0,0 +1,29 @@
### MarkdownChecker
a script that generates a JSON file from the markdown which contains status of proposals.

#### modules
this script consist of three main stage like other compilers have
- parser
- analyzer
- transformer
- generator

##### Parser
- **_readMarkdown_** : `string`
- source file will be parsed as an AST
- **_parseToAST_** : `object`
- an AST representation of the markdown

##### Analyzer
- **_collectLinkDefinitions_** : `object`
- detects all link definitions declared from bottom of the markdown file and returns these definitions
- **_detectTables_** : `object`
- extracts all of the tables from the markdown and returns it as a tree
- **_detectHeaders_** : `object`
- extracts all of the row from the table nodes and returns it as a tree

##### Transformer
- **_traverser_**
- takes current node as an input if it has an children nodes then applies the logic with given callback functions and returns something declared in the callback function

##### Generator
12 changes: 12 additions & 0 deletions tools/markdown-checker/enums.js
@@ -0,0 +1,12 @@
module.exports = {
stage3: './../../../../README.md',
stage1: './../../../../stage-1-proposals.md',
DEFINITION: 'definition',
TABLE: 'table',
TEXT: 'text',
ROW: 'tableRow',
CELL: 'tableCell',
HTML: 'html',
LINK: 'linkReference',
INLINE_CODE: 'inlineCode',
};
21 changes: 21 additions & 0 deletions tools/markdown-checker/index.js
@@ -0,0 +1,21 @@
const readMarkdown = require('./lib/parser/readMarkdown');
const parseToAST = require('./lib/parser/parseToAst');
const globalData = require('./lib/data');
const {
collectLinkDefinitions,
} = require('./lib/analyzer/collectLinkDefinitions');
const { generateTable } = require('./lib/analyzer/analyzeTable');
const enums = require('./enums.js');

const processStage3 = (stage) => {
const activeStage = enums[stage];
// parse stage
const markdownStage = readMarkdown(activeStage);
const parsedFile = parseToAST(markdownStage);

globalData.linkDefinitions = collectLinkDefinitions(parsedFile);
const tableStage1 = generateTable(parsedFile);
// TODO: write data into json file
};

processStage3('stage1');
@@ -0,0 +1,43 @@
const readMarkdown = require('./../../parser/readMarkdown');
const parseToAST = require('./../../parser/parseToAst');
const {
collectLinkDefinitions,
addLinkIntoDefintions
} = require('../collectLinkDefinitions');
const definitionNode = require('../../../mocks/definitionNode');

describe('testing collectLinkDefiniton function', () => {
let parsedMarkdown;
let allLinkDefinitions;

beforeAll(() => {
const markdownFile = readMarkdown('../../mocks/stage3Mock.md');
parsedMarkdown = parseToAST(markdownFile);
allLinkDefinitions = collectLinkDefinitions(parsedMarkdown);
});

test('function is defined', () => {
expect(collectLinkDefinitions).toBeDefined();
expect(typeof collectLinkDefinitions).toBe('function');
});

test('definition added into hash', () => {
const definitions = addLinkIntoDefintions({}, definitionNode);
const { label, url } = definitions;
expect(typeof definitions).toBe('object');
expect(typeof label && typeof url).toBe('string');
expect(label).toBe('regexp-legacy');
expect(url).toBe('https://github.com/tc39/proposal-regexp-legacy-features');
});

test('link definitions are collected', () => {
expect(allLinkDefinitions).toBeDefined();
expect(typeof allLinkDefinitions).toBe('object');
expect(allLinkDefinitions['buffer-transfer']).toBe(
'https://github.com/domenic/proposal-arraybuffer-transfer/'
);
expect(allLinkDefinitions['private-methods']).toBe(
'https://github.com/tc39/proposal-private-methods'
);
});
});
@@ -0,0 +1,41 @@
const detectTables = require('../detectTables');
const detectHeaders = require('../handleTables');
const parsedMarkdownTree = require('../../../mocks/parsedMarkdownTree');

const markdownTables = detectTables(parsedMarkdownTree);
const [child] = markdownTables;

describe('testing detectTables function', () => {
test('is defined', () => {
expect(detectTables).toBeDefined();
});
test('length is matched', () => {
expect(markdownTables.length).toEqual(2);
});
test('has correct size of thead', () => {
expect(child.align.length).toBe(5);
})
test('is returned table', () => {
expect(child.type).toBe('table');
});
test('has row and cell children', () => {
const [rowChild] = child.children;
expect(rowChild.type).toBe('tableRow');

const [cellChild] = rowChild.children;
expect(cellChild.type).toBe('tableCell');
});
});

describe('testing detectHeaders function', () => {
let rowChild;
beforeAll(() => {
[rowChild] = child.children;
});
test('is defined', ()=>{
expect(detectHeaders).toBeDefined();
})
test('is defined', ()=>{
rowChild;
})
})
40 changes: 40 additions & 0 deletions tools/markdown-checker/lib/analyzer/analyzeTable.js
@@ -0,0 +1,40 @@

const detectTables = require('./detectTables');
const handleTables = require('./handleTables');
const { checkNodeHasChildren } = require('../utils');
/**
* @todo inspect below and decide
* @template [https://jsoneditoronline.org/?id=f1ce5803d66149d5bc86d0d53ffb40c0]
*
*/

/**
*
* @param {Object} node
* @returns {Array}
*/
const extractAllTablesFromTree = (node) => {
if (checkNodeHasChildren(node)) {
return detectTables(node);
}
return [];
};
/**
*
* @param {Object} node - current node of the parsed AST
* @param {Object} linkDefinitions - represents all of the link shortcuts
*/

const generateTable = (node) => {
const tables = extractAllTablesFromTree(node);
if (tables && tables.length > 0) {
tables.forEach((table) => {
if (Object.values(table).length > 0) {
const JSONTables = handleTables(table);
}
});
}
return [];
};

module.exports = { generateTable, extractAllTablesFromTree };
32 changes: 32 additions & 0 deletions tools/markdown-checker/lib/analyzer/collectLinkDefinitions.js
@@ -0,0 +1,32 @@
const { DEFINITION } = require('./../../enums.js');

/**
*
* @param {Object} definitions
* @param {Object} node
* @returns {Object}
*/
function addLinkIntoDefinitions(definitions, node) {
const { label, url } = node;
// eslint-disable-next-line no-param-reassign
definitions[label] = url;
return {
label,
url,
};
}

/**
* @param {Object} AST
* @returns {Object} - collected link definitions
*/
function collectLinkDefinitions(AST) {
const definitions = {};
AST.children.forEach((node) => {
// eslint-disable-next-line no-unused-expressions
node.type === DEFINITION && addLinkIntoDefinitions(definitions, node);
});
return definitions;
}

module.exports = { collectLinkDefinitions };
10 changes: 10 additions & 0 deletions tools/markdown-checker/lib/analyzer/detectTables.js
@@ -0,0 +1,10 @@
const { TABLE } = require('./../../enums.js');
/**
* @param {Object} node - Parsed markdown file an AST Object
* @returns {Array} - contains detected table nodes of the AST
*/
function detectTables(node) {
return node.children.filter(({ type }) => type === TABLE);
}

module.exports = detectTables;
87 changes: 87 additions & 0 deletions tools/markdown-checker/lib/analyzer/handleTables.js
@@ -0,0 +1,87 @@
const {
CELL, TABLE, ROW, LINK, TEXT,
} = require('../../enums');
const globalData = require('../data');

const { tableHead } = globalData;
const { iterateAndConcatValue, handleLinkReference } = require('../utils');

/**
*
* @param {Array} headNodes
* @return {handleTables}
*/
const createHead = (headNodes) => {
headNodes.forEach(({ type, children }) => {
if (type !== CELL) return null;
tableHead.push(iterateAndConcatValue(children));
return tableHead;
});
return tableHead;
};

const handleCellLinkReference = (cell, idx) => {
const relatedHead = tableHead[idx];
return {
[relatedHead]: handleLinkReference(cell, globalData.linkDefinitions),
};
};

const handleCellTextHTML = (cell, idx) => {
const relatedHead = tableHead[idx];
return {
[relatedHead]: cell.value,
};
};

const handleCell = (cells, idx) => {
// FIXME: handle <br/> separated multiple champions
let result;
if (cells.length) {
cells.forEach((cell) => {
switch (cell.type) {
case LINK:
result = handleCellLinkReference(cell, idx);
return result;
case TEXT:
result = handleCellTextHTML(cell, idx);
return result;
default:
return {};
}
});
}
return result;
};

const handleRows = (row) => {
let obj = {};
row.forEach(({ type, children }, idx) => {
if (type !== CELL) return null;
const rowLine = handleCell(children, idx);
obj = { ...obj, ...rowLine };
});
return obj;
};

/**
* @param {Object} table
* @returns {Array}
*/
const handleTables = ({ align: { length }, type, children }) => {
const arr = [];
if (type !== TABLE) return null;
children.forEach(({ children: tableRow, type: rowType }, idx) => {
if (rowType !== ROW) return [];
if (idx === 0) {
createHead(tableRow);
return [];
}
const row = handleRows(tableRow, length);
arr.push(row);
return arr;
});
return arr;
};

module.exports = handleTables;
3 changes: 3 additions & 0 deletions tools/markdown-checker/lib/data.js
@@ -0,0 +1,3 @@
module.exports = {
tableHead: [],
};