Skip to content

Commit

Permalink
Remove require("os") from helpers to reduce dependencies for browser …
Browse files Browse the repository at this point in the history
…scenarios.
  • Loading branch information
DavidAnson committed Dec 27, 2021
1 parent 9ec14f1 commit fd24b95
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
19 changes: 5 additions & 14 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ module.exports = webpackEmptyContext;
/*!*****************************!*\
!*** ../helpers/helpers.js ***!
\*****************************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
/***/ ((module) => {

"use strict";
// @ts-check

var os = __webpack_require__(/*! os */ "?591e");
// Regular expression for matching common newline characters
// See NEWLINES_RE in markdown-it/lib/rules_core/normalize.js
var newLineRe = /\r\n?|\n/g;
Expand Down Expand Up @@ -655,9 +654,10 @@ module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
* Gets the most common line ending, falling back to the platform default.
*
* @param {string} input Markdown content to analyze.
* @param {string} [platform] Platform identifier (process.platform).
* @returns {string} Preferred line ending.
*/
function getPreferredLineEnding(input) {
function getPreferredLineEnding(input, platform) {
var cr = 0;
var lf = 0;
var crlf = 0;
Expand All @@ -678,7 +678,8 @@ function getPreferredLineEnding(input) {
});
var preferredLineEnding = null;
if (!cr && !lf && !crlf) {
preferredLineEnding = os.EOL;
preferredLineEnding =
((platform || process.platform) === "win32") ? "\r\n" : "\n";
}
else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n";
Expand Down Expand Up @@ -4343,16 +4344,6 @@ module.exports = markdownit;

/***/ }),

/***/ "?591e":
/*!********************!*\
!*** os (ignored) ***!
\********************/
/***/ (() => {

/* (ignored) */

/***/ }),

/***/ "?ec0a":
/*!********************!*\
!*** fs (ignored) ***!
Expand Down
1 change: 0 additions & 1 deletion demo/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function config(options) {
"resolve": {
"fallback": {
"fs": false,
"os": false,
"path": false,
"util": false
}
Expand Down
8 changes: 4 additions & 4 deletions helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

"use strict";

const os = require("os");

// Regular expression for matching common newline characters
// See NEWLINES_RE in markdown-it/lib/rules_core/normalize.js
const newLineRe = /\r\n?|\n/g;
Expand Down Expand Up @@ -680,9 +678,10 @@ module.exports.emphasisMarkersInContent = emphasisMarkersInContent;
* Gets the most common line ending, falling back to the platform default.
*
* @param {string} input Markdown content to analyze.
* @param {string} [platform] Platform identifier (process.platform).
* @returns {string} Preferred line ending.
*/
function getPreferredLineEnding(input) {
function getPreferredLineEnding(input, platform) {
let cr = 0;
let lf = 0;
let crlf = 0;
Expand All @@ -703,7 +702,8 @@ function getPreferredLineEnding(input) {
});
let preferredLineEnding = null;
if (!cr && !lf && !crlf) {
preferredLineEnding = os.EOL;
preferredLineEnding =
((platform || process.platform) === "win32") ? "\r\n" : "\n";
} else if ((lf >= crlf) && (lf >= cr)) {
preferredLineEnding = "\n";
} else if (crlf >= cr) {
Expand Down
12 changes: 11 additions & 1 deletion test/markdownlint-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ test("forEachInlineCodeSpan", (t) => {
});

test("getPreferredLineEnding", (t) => {
t.plan(17);
t.plan(19);
const testCases = [
[ "", os.EOL ],
[ "\r", "\r" ],
Expand All @@ -412,6 +412,16 @@ test("getPreferredLineEnding", (t) => {
const actual = helpers.getPreferredLineEnding(input);
t.is(actual, expected, "Incorrect line ending returned.");
});
t.is(
helpers.getPreferredLineEnding("", "linux"),
"\n",
"Incorrect line ending for linux"
);
t.is(
helpers.getPreferredLineEnding("", "win32"),
"\r\n",
"Incorrect line ending for win32"
);
});

test("applyFix", (t) => {
Expand Down

0 comments on commit fd24b95

Please sign in to comment.