Skip to content

Commit

Permalink
fix absolute path + algo to detect them
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinMachado committed Mar 9, 2023
1 parent 1f0e591 commit ac84c28
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 31 deletions.
3 changes: 1 addition & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ UD-Viz is a 3-package JavaScript framework for creating web applications for vis
[Developers](./docs/static/Devel/Developers.md) —
[License](./LICENSE.md) —
[Getting Started](#getting-started)
[test](/home/imuv/projects/UD-Viz/Readme.md)
[test_1](/bad/imuv/projects/UD-Viz/Readme.md)


**Online demos**:

Expand Down
121 changes: 121 additions & 0 deletions bin/remarkValidateLinks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const exec = require('child-process-promise').exec;
const fs = require('fs');
const { Data } = require('@ud-viz/shared');

const FAILURE_THRESHOLD = 0; // no warning should be found

Expand All @@ -15,4 +17,123 @@ exec('npx remark -u validate-links .').then((result) => {
throw new Error('Too much warnings report to log above to fix them');
}
}

// DETECT ABSOLUTE PATH in Link

/**
* directory to not parse
*/
const blackDirectoryList = ['node_modules'];

/**
*
* @param {string} path - path to check
* @returns {boolean} - true if path is an absolute path
*/
const isAbsolutePath = (path) => {
const noAbsolutePathPrefix = ['https', 'http', '.', '#'];
let result = true;
for (let index = 0; index < noAbsolutePathPrefix.length; index++) {
const prefix = noAbsolutePathPrefix[index];
if (path.startsWith(prefix)) {
result = false;
break;
}
}
return result;
};

/**
* Record log infos to display for absolute path check
*/
class LogInfosAbsolutePath {
constructor(filename) {
this.filename = filename;
this.absoluteLinks = [];
}

record(link) {
this.absoluteLinks.push(link);
}

log() {
console.log(this.filename);
console.log(this.absoluteLinks);
}
}

const logInfos = [];

const parseDirectory = (directoryPath) => {
const dirents = fs.readdirSync(directoryPath, { withFileTypes: true });
dirents.forEach((dirent) => {
if (dirent.isFile() && Data.computeFileFormat(dirent.name) == 'md') {
const filePath = directoryPath + '/' + dirent.name;
let logInfo = null;
const contentMd = fs.readFileSync(filePath, {
encoding: 'utf-8',
});

const linkRegexResult = contentMd.match(/\[(.*?)\]\(.*?\)/gm);

if (linkRegexResult) {
linkRegexResult.forEach((link) => {
// could be done with a regex but i didn't find one matching all cases
let lastCharacter = null;
let path = null;
for (let index = link.length - 1; index >= 0; index--) {
const character = link[index];
if (index == link.length - 1 && character != ')') {
console.log(link);
throw new Error('wrong link format');
}

// detect first ]( path is between there and the end of link
if (lastCharacter == '(' && character == ']') {
path = link.slice(index + 2, link.length - 1);
break;
}

lastCharacter = character;
}

if (!path) throw new Error('cant find path in link');

if (isAbsolutePath(path)) {
if (!logInfo) {
logInfo = new LogInfosAbsolutePath(filePath);
logInfos.push(logInfo);
}
logInfo.record(link);
}
});
}

if (!logInfo) console.log(filePath + ': no issues found');
} else if (
dirent.isDirectory() &&
!blackDirectoryList.includes(dirent.name)
) {
parseDirectory(directoryPath + '/' + dirent.name); // recursive
}
});
};

console.log('\nCheck Absolute path\n');
parseDirectory('.');

if (logInfos.length) {
console.log('\nFound Absolute path\n');

let countAbsolutePath = 0;
logInfos.forEach((logInfo) => {
logInfo.log();
countAbsolutePath += logInfo.absoluteLinks.length;
});
throw new Error(
'There is ' +
countAbsolutePath +
' absolute path use relative path or URL instead'
);
}
});
4 changes: 2 additions & 2 deletions docs/static/Devel/ArchitectureMVCTargetDesign.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ We propose a nomenclature to use when working with classes in a module, so that

The documents module follows an MVVM architecture :

![](Pictures/DocumentsArchitecture.png)
![](./Pictures/DocumentsArchitecture.png)

The model holds the documents and make requests to the server, the view model filter those documents and the view displays the filtered documents.

Expand All @@ -104,7 +104,7 @@ The code is separated in two main parts : the view and the service. It ressemble
* The service makes the appropriate request to the server, and processes the received data
* It then notifies the listeners. The service follows a simple implementation of the [Observer pattern](https://en.wikipedia.org/wiki/Observer_pattern) where observers are just callbacks without arguments (called listeners). The view has subscribed to the service when it was created, so it received the notification of the service and can update itself.

![](Pictures/ViewServiceArchitecture.png)
![](./Pictures/ViewServiceArchitecture.png)

### Services

Expand Down
4 changes: 2 additions & 2 deletions docs/static/Devel/Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Coding style (Linter)

The JavaScript files coding style is defined with [eslint](https://eslint.org/) through the [.eslintrc.js configuration file](/.eslintrc.js).
The JavaScript files coding style is defined with [eslint](https://eslint.org/) through the [.eslintrc.js configuration file](../../../.eslintrc.js).
It can be checked (e.g. prior to a commit) with the `npm run eslint` command.
Notice that UD-Viz coding style uses a unix `linebreak-style` (aka `LF` as newline character).

Expand Down Expand Up @@ -37,7 +37,7 @@ Notice that UD-Viz coding style uses a unix `linebreak-style` (aka `LF` as newli
Before submitting a pull request, and because [UD-Viz still misses some tests](https://github.com/VCityTeam/UD-SV/issues/34),
**non-regression testing must be done manually**.
A developer must thus at least check that all the
[demo examples](/packages/browser/examples/)
[demo examples](../../../examples/)
(they should function similar to [their online deployment](https://ud-viz.vcityliris.data.alpha.grandlyon.com/)) are still effective.

> Note that you should interact with ui (user interface) for complete tests.
Expand Down
2 changes: 1 addition & 1 deletion docs/static/Devel/Developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Developing UD-Viz applications requires knowledge about :

- [JavaScript](https://developer.mozilla.org/en-US/docs/Web/javascript)
- [node.js](https://en.wikipedia.org/wiki/Node.js)
- [npm](<https://en.wikipedia.org/wiki/Npm_(software)>)
- [npm](https://en.wikipedia.org/wiki/Npm_(software))
- [three.js](https://threejs.org/)
- [iTowns](http://www.itowns-project.org)

Expand Down
Loading

0 comments on commit ac84c28

Please sign in to comment.