Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

freshlinks

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

freshlinks - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

lib/git.d.ts

19

lib/cli.js

@@ -39,2 +39,3 @@ #!/usr/bin/env node

const suggest_path_1 = require("./suggest-path");
const git_1 = require("./git");
const yargs = __importStar(require("yargs"));

@@ -45,5 +46,11 @@ const process = __importStar(require("process"));

return __awaiter(this, void 0, void 0, function* () {
const cmdLineArguments = yargs.options({
suggestions: { type: 'boolean', default: true }
}).argv;
const cmdLineArguments = yargs
.options({
suggestions: {
type: 'boolean',
default: true,
describe: 'When a broken link is detected, suggest a potential fix by considering the files stored in the Git repository. Note, this must be run in a Git repository for this functionality to work.'
}
})
.strict().argv;
const files = cmdLineArguments._;

@@ -53,3 +60,3 @@ let exitCode = 0;

if (cmdLineArguments.suggestions) {
possibleLinkDestinations = yield suggest_path_1.gitLsFiles('.');
possibleLinkDestinations = yield git_1.gitLsFiles();
}

@@ -71,4 +78,6 @@ else {

const [suggestion, distance] = suggest_path_1.suggestPath(link.sourceFile, link.link, possibleLinkDestinations);
// Don't suggest matches that are too far away from the original
// link
if (distance <= suggest_path_1.SUGGEST_MIN_DISTANCE) {
console.log(`Perhaps you meant: ${chalk_1.default.blue(suggestion)} ${distance}`);
console.log(`Perhaps you meant: ${chalk_1.default.blue(suggestion)}`);
}

@@ -75,0 +84,0 @@ }

import { formatMarkdownLink, formatInvalidMarkdownLink } from './format';
import { MarkdownLink, parse_markdown_links_from_file, parse_markdown_links_from_files } from './parse-markdown-links';
import { LinkValidity, valid_link } from './validate-link';
export { formatMarkdownLink, formatInvalidMarkdownLink, MarkdownLink, parse_markdown_links_from_file, parse_markdown_links_from_files, LinkValidity, valid_link };
import { suggestPath, SUGGEST_MIN_DISTANCE } from './suggest-path';
import { gitLsFiles } from './git';
export { formatMarkdownLink, formatInvalidMarkdownLink, MarkdownLink, parse_markdown_links_from_file, parse_markdown_links_from_files, LinkValidity, valid_link, gitLsFiles, suggestPath, SUGGEST_MIN_DISTANCE };
export declare function validate_markdown_links_from_files(filenames: AsyncGenerator<string>): AsyncGenerator<[MarkdownLink, LinkValidity]>;

@@ -22,3 +22,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.validate_markdown_links_from_files = exports.valid_link = exports.LinkValidity = exports.parse_markdown_links_from_files = exports.parse_markdown_links_from_file = exports.formatInvalidMarkdownLink = exports.formatMarkdownLink = void 0;
exports.validate_markdown_links_from_files = exports.SUGGEST_MIN_DISTANCE = exports.suggestPath = exports.gitLsFiles = exports.valid_link = exports.LinkValidity = exports.parse_markdown_links_from_files = exports.parse_markdown_links_from_file = exports.formatInvalidMarkdownLink = exports.formatMarkdownLink = void 0;
const format_1 = require("./format");

@@ -33,2 +33,7 @@ Object.defineProperty(exports, "formatMarkdownLink", { enumerable: true, get: function () { return format_1.formatMarkdownLink; } });

Object.defineProperty(exports, "valid_link", { enumerable: true, get: function () { return validate_link_1.valid_link; } });
const suggest_path_1 = require("./suggest-path");
Object.defineProperty(exports, "suggestPath", { enumerable: true, get: function () { return suggest_path_1.suggestPath; } });
Object.defineProperty(exports, "SUGGEST_MIN_DISTANCE", { enumerable: true, get: function () { return suggest_path_1.SUGGEST_MIN_DISTANCE; } });
const git_1 = require("./git");
Object.defineProperty(exports, "gitLsFiles", { enumerable: true, get: function () { return git_1.gitLsFiles; } });
function validate_markdown_links_from_files(filenames) {

@@ -35,0 +40,0 @@ return __asyncGenerator(this, arguments, function* validate_markdown_links_from_files_1() {

@@ -1,3 +0,2 @@

export declare function gitLsFiles(path: string): Promise<string[]>;
export declare function suggestPath(sourceFile: string, badLink: string, candidatePaths: string[]): [string, number];
export declare const SUGGEST_MIN_DISTANCE = 200;
export declare const SUGGEST_MIN_DISTANCE = 8;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SUGGEST_MIN_DISTANCE = exports.suggestPath = exports.gitLsFiles = void 0;
const nodegit_1 = require("nodegit");
exports.SUGGEST_MIN_DISTANCE = exports.suggestPath = void 0;
const path_1 = require("path");
const fastest_levenshtein_1 = require("fastest-levenshtein");
function gitLsFiles(path) {
return __awaiter(this, void 0, void 0, function* () {
const repoPathBuf = yield nodegit_1.Repository.discover(path, 0, '');
const repo = yield nodegit_1.Repository.open(repoPathBuf.toString());
const index = yield repo.index();
return index.entries().map(o => {
return o.path;
});
});
}
exports.gitLsFiles = gitLsFiles;
function suggestPath(sourceFile, badLink, candidatePaths) {
const relativePathCandidates = candidatePaths.map(path => {
const relativeLink = path_1.relative(sourceFile, path);
// the relative links calculated will have an extra `../`,
// if the directory is 'above', or the file is in the same
// directory as the current file. So if this relative path
// starts with a `../`, we remove one layer of it
if (relativeLink.startsWith('../')) {

@@ -37,2 +20,3 @@ return relativeLink.slice(3);

});
// Don't include a link to the sourceFile itself
const withoutSelfLink = relativePathCandidates.filter(link => {

@@ -45,2 +29,4 @@ return link !== '';

exports.suggestPath = suggestPath;
exports.SUGGEST_MIN_DISTANCE = 200;
// A constant that can be used to only suggest
// paths that are 'close enough' to the incorrect path
exports.SUGGEST_MIN_DISTANCE = 8;
{
"name": "freshlinks",
"version": "0.0.2",
"version": "0.0.3",
"private": false,

@@ -32,4 +32,6 @@ "description": "Freshlinks",

"dependencies": {
"@actions/exec": "^1.0.4",
"chalk": "^4.1.0",
"commonmark": "^0.29.1",
"fastest-levenshtein": "^1.0.12",
"yargs": "^15.4.1"

@@ -36,0 +38,0 @@ },

@@ -6,9 +6,30 @@ #!/usr/bin/env node

import {valid_link, LinkValidity} from './validate-link'
import {suggestPath, SUGGEST_MIN_DISTANCE} from './suggest-path'
import {gitLsFiles} from './git'
import * as yargs from 'yargs'
import * as process from 'process'
import chalk from 'chalk'
async function run(): Promise<void> {
const args = yargs.argv._
const cmdLineArguments = yargs
.options({
suggestions: {
type: 'boolean',
default: true,
describe:
'When a broken link is detected, suggest a potential fix by considering the files stored in the Git repository. Note, this must be run in a Git repository for this functionality to work.'
}
})
.strict().argv
const files = cmdLineArguments._
let exitCode = 0
for (const file of args) {
let possibleLinkDestinations: string[]
if (cmdLineArguments.suggestions) {
possibleLinkDestinations = await gitLsFiles()
} else {
possibleLinkDestinations = []
}
for (const file of files) {
try {

@@ -23,2 +44,15 @@ const links = await parse_markdown_links_from_file(file)

console.log(formatInvalidMarkdownLink(link))
if (cmdLineArguments.suggestions) {
const [suggestion, distance] = suggestPath(
link.sourceFile,
link.link,
possibleLinkDestinations
)
// Don't suggest matches that are too far away from the original
// link
if (distance <= SUGGEST_MIN_DISTANCE) {
console.log(`Perhaps you meant: ${chalk.blue(suggestion)}`)
}
}
}

@@ -25,0 +59,0 @@ }

@@ -14,5 +14,5 @@ import {MarkdownLink} from './parse-markdown-links'

const errorString = `
${chalk.underline(`${link.sourceFile}:${link.startLine}`)}
${chalk.bold.red('Error')} Could not resolve link: ${chalk.yellow(link.link)}`
${chalk.underline(`${link.sourceFile}:${link.startLine}`)}
${chalk.bold.red('Error')} Could not resolve link: ${chalk.yellow(link.link)}`
return errorString
}

@@ -8,2 +8,4 @@ import {formatMarkdownLink, formatInvalidMarkdownLink} from './format'

import {LinkValidity, valid_link} from './validate-link'
import {suggestPath, SUGGEST_MIN_DISTANCE} from './suggest-path'
import {gitLsFiles} from './git'

@@ -17,3 +19,6 @@ export {

LinkValidity,
valid_link
valid_link,
gitLsFiles,
suggestPath,
SUGGEST_MIN_DISTANCE
}

@@ -20,0 +25,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc