Skip to content

Commit

Permalink
Added link checker for markdown content. closes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
SReichelt committed Aug 2, 2020
1 parent dce8ac6 commit b136715
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 56 deletions.
2 changes: 1 addition & 1 deletion data/libraries/hlm
Submodule hlm updated 54 files
+1 −1 Algebra/Fields/Fields.slate
+1 −1 Algebra/Groups/Groups.slate
+1 −1 Algebra/Groups/abelian group.slate
+1 −1 Algebra/Groups/abelian.slate
+1 −1 Algebra/Groups/group.slate
+1 −1 Algebra/Monoids/Monoids.slate
+1 −1 Algebra/Monoids/Submonoids/submonoid set.slate
+1 −1 Algebra/Monoids/Submonoids/subset monoid.slate
+1 −1 Algebra/Monoids/commutative.slate
+1 −1 Algebra/Monoids/invertible.slate
+1 −1 Algebra/Monoids/monoid.slate
+1 −1 Algebra/Rings/Homomorphisms/Homomorphisms.slate
+1 −1 Algebra/Rings/Homomorphisms/identity.slate
+1 −1 Algebra/Rings/Ideals/ideal.slate
+1 −1 Algebra/Rings/Modules/Left modules/Examples/ideal module.slate
+1 −1 Algebra/Rings/Modules/Left modules/Examples/polynomial module.slate
+1 −1 Algebra/Rings/Modules/Left modules/Homomorphisms/Homomorphisms.slate
+1 −1 Algebra/Rings/Modules/Left modules/Homomorphisms/identity.slate
+1 −1 Algebra/Rings/Modules/Left modules/Left modules.slate
+1 −1 Algebra/Rings/Modules/Left modules/Submodules/submodule set.slate
+1 −1 Algebra/Rings/Modules/Left modules/Submodules/subset module.slate
+1 −1 Algebra/Rings/Modules/Left modules/left module.slate
+0 −1 Algebra/Rings/Modules/Right modules/Homomorphisms/Homomorphisms.slate
+1 −1 Algebra/Rings/Polynomials/Polynomials.slate
+1 −1 Algebra/Rings/Polynomials/degree.slate
+1 −1 Algebra/Rings/Polynomials/polynomial ring.slate
+1 −1 Algebra/Rings/Polynomials/value.slate
+1 −1 Algebra/Rings/Rings.slate
+1 −1 Algebra/Rings/additive group.slate
+1 −1 Algebra/Rings/multiplicative monoid.slate
+1 −1 Algebra/Rings/ring.slate
+1 −1 Algebra/Rings/unit.slate
+1 −1 Algebra/Semigroups/Semigroups.slate
+1 −1 Algebra/Semigroups/semigroup.slate
+1 −1 Algebra/Semirings/Homomorphisms/Homomorphisms.slate
+1 −1 Algebra/Semirings/Homomorphisms/identity.slate
+1 −1 Algebra/Semirings/Semirings.slate
+1 −1 Algebra/Semirings/additive monoid.slate
+1 −1 Algebra/Semirings/multiplicative monoid.slate
+1 −1 Algebra/Semirings/semiring.slate
+1 −1 Essentials/Functions/bijective.slate
+1 −1 Essentials/Functions/inverse.slate
+1 −1 Essentials/Functions/restriction.slate
+1 −1 Essentials/Numbers/Natural/Induction principle of property.slate
+1 −1 Essentials/Numbers/Natural/Induction principle.slate
+1 −1 Essentials/Numbers/Real/Real numbers.slate
+1 −1 Essentials/Numbers/Real/difference.slate
+1 −1 Essentials/Numbers/Real/inverse.slate
+1 −1 Essentials/Numbers/Real/less or equal.slate
+1 −1 Essentials/Numbers/Real/less.slate
+1 −1 Essentials/Numbers/Real/product.slate
+1 −1 Essentials/Numbers/Real/sum.slate
+1 −1 Essentials/Sets/Multisets/Multisets.slate
+1 −1 Linear algebra/Vector spaces/Vector spaces.slate
38 changes: 38 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"file-loader": "^6.0.0",
"html-webpack-plugin": "^4.3.0",
"jest": "^26.2.0",
"link-check": "^4.5.0",
"open-browser-webpack-plugin": "github:rodrigopandini/open-browser-webpack-plugin",
"rimraf": "^3.0.2",
"style-loader": "^1.2.1",
Expand Down
3 changes: 3 additions & 0 deletions src/scripts/checkLibraryLinks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
find data/libraries/ -name '*.slate' -exec src/scripts/checkLinks.sh '{}' +
3 changes: 3 additions & 0 deletions src/scripts/checkLinks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
node_modules/.bin/ts-node -P src/scripts/tsconfig.json src/scripts/checkLinks.ts "$@"
92 changes: 92 additions & 0 deletions src/scripts/checkLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as fs from 'fs';
import * as Fmt from '../shared/format/format';
import * as FmtReader from '../shared/format/read';
import { getMetaModelWithFallback } from '../fs/format/dynamic';
import * as Logics from '../shared/logics/logics';

const Remarkable = require('remarkable').Remarkable;
const linkify = require('remarkable/linkify').linkify;
const linkCheck = require('link-check');

let linksFound = 0;
let linksChecking = 0;
let linksChecked = 0;

class LinkExtractor {
constructor(private fileName: string) {}

render(tokens: any[] = []): void {
for (let token of tokens) {
if (token.type === 'link_open') {
let uri: string = token.href;
if (uri.startsWith('http://') || uri.startsWith('https://')) {
linksFound++;
this.triggerLinkCheck(uri);
}
} else if (token.children) {
this.render(token.children);
}
}
}

private triggerLinkCheck(uri: string) {
if (linksChecking < 10) {
linksChecking++;
linkCheck(uri, (err: any, result: any) => {
if (err) {
console.error(`${this.fileName}: ${err}`);
} else if (result.status !== 'alive') {
console.error(`${this.fileName}: ${result.status} link: ${result.link}`);
}
linksChecking--;
linksChecked++;
if (linksChecked === linksFound) {
console.log(`${linksChecked} links checked.`);
}
});
} else {
setTimeout(() => this.triggerLinkCheck(uri), 1000);
}
}
}

function checkDefinitionLinks(fileName: string, definition: Fmt.Definition) {
if (definition.documentation) {
for (let documentationItem of definition.documentation.items) {
let md = new Remarkable;
md.use(linkify);
md.renderer = new LinkExtractor(fileName);
md.render(documentationItem.text);
}
}

for (let innerDefinition of definition.innerDefinitions) {
checkDefinitionLinks(fileName, innerDefinition);
}
}

function checkLinks(fileName: string): void {
let fileStr = fs.readFileSync(fileName, 'utf8');
let getMetaModel = (path: Fmt.Path) => {
let logic = Logics.findLogic(path.name);
if (logic) {
return logic.getMetaModel(path);
}
return getMetaModelWithFallback(fileName, path);
};
let file = FmtReader.readString(fileStr, fileName, getMetaModel);
for (let definition of file.definitions) {
checkDefinitionLinks(fileName, definition);
}
}

if (process.argv.length < 3) {
console.error('usage: src/scripts/checkLinks.sh <file1> [<file2>...]');
process.exit(2);
}

for (let fileName of process.argv.slice(2)) {
checkLinks(fileName);
}

console.log(`${linksFound} links found.`);
Loading

0 comments on commit b136715

Please sign in to comment.