Skip to content

Commit 4532b25

Browse files
authored
Merge pull request #20 from reactplay/Issue-19-use-sanitized-and-slug-data-for-path
Issue-19 Use sanitized and slug data for path
2 parents d05d856 + 675a1b6 commit 4532b25

File tree

8 files changed

+34
-14
lines changed

8 files changed

+34
-14
lines changed

_templates/component_js.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PlayHeader from 'common/playlists/PlayHeader';
22
import './styles.%PLAY_STYLE%';
33

44
// WARNING: Do not change the entry componenet name
5-
function %PLAY_TITLE_NAME%(props) {
5+
function %PLAY_TITLE_NAME_SANITIZED%(props) {
66

77
// Your Code Start below.
88

@@ -28,4 +28,4 @@ function %PLAY_TITLE_NAME%(props) {
2828
);
2929
}
3030

31-
export default %PLAY_TITLE_NAME%;
31+
export default %PLAY_TITLE_NAME_SANITIZED%;

_templates/component_ts.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import PlayHeader from 'common/playlists/PlayHeader';
33
import './styles.{{style}}';
44

5-
function %PLAY_TITLE_NAME%(props) {
5+
function %PLAY_TITLE_NAME_SANITIZED%(props) {
66

77
// Your Code Start below.
88

@@ -28,4 +28,4 @@ function %PLAY_TITLE_NAME%(props) {
2828
);
2929
}
3030

31-
export default %PLAY_TITLE_NAME%;
31+
export default %PLAY_TITLE_NAME_SANITIZED%;

lib/services/datasource/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { FETH_ALL_PLAYS } from "./query.js";
44
import {
55
toKebabCase,
66
toPascalcase,
7+
toSanitized,
8+
toSlug,
79
toTitleCase,
810
toTitleCaseTrimmed,
911
} from "../../../util/string.js";
@@ -73,10 +75,11 @@ export const loadAllPlaysAsync = () => {
7375
};
7476

7577
const processPlayInformation = (play) => {
76-
play.kebab_name = toKebabCase(play.name);
77-
play.pascal_name = toPascalcase(play.name);
78+
play.kebab_name = play.slug;
79+
play.pascal_name = toPascalcase(toSanitized(play.name));
7880
play.title_string = toTitleCase(play.name);
7981
play.title_name = toTitleCaseTrimmed(play.name);
82+
play.title_name_sanitized = toSanitized(play.title_name);
8083
play.level = play.level.name;
8184
return play;
8285
};

lib/services/datasource/query.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const FETH_ALL_PLAYS = {
1515
"id",
1616
"dev_mode",
1717
"language",
18+
"slug",
1819
{ level: ["name"] },
1920
"name",
2021
"path",

lib/services/template/play_content.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs";
22
import * as path from "path";
3-
import { replaceAll } from "../../../util/string.js";
3+
import { replaceAll, toSanitized, toSlug } from "../../../util/string.js";
44
import { fileURLToPath } from "url";
55
import { Log } from "../../../log/index.js";
66

@@ -23,7 +23,7 @@ export const processPlayContent = (play) => {
2323
};
2424

2525
const createPlayDirectory = (play) => {
26-
const play_directory = `${PLAY_LIST_DIRECTORY}/${play.kebab_name}`;
26+
const play_directory = `${PLAY_LIST_DIRECTORY}/${toSlug(play.kebab_name)}`;
2727
if (!fs.existsSync(play_directory)) {
2828
fs.mkdirSync(play_directory, { recursive: true });
2929
}
@@ -50,7 +50,7 @@ const replaceTemplateVar = (play_data, filePath) => {
5050
);
5151
});
5252
fs.writeFile(filePath, updated_result, "utf8", function (err) {
53-
if (err) return Log.log(err);
53+
if (err) return Log.log(`Error: During file write: ${err}, ${play_data}`);
5454
});
5555
});
5656
};
@@ -80,7 +80,7 @@ const processTemplate = (play, template_type, play_path) => {
8080
if (template_type === "js" || template_type === "ts") {
8181
target_template_name = target_template_name.replace(
8282
"component",
83-
play.title_name
83+
toSanitized(play.title_name)
8484
);
8585
}
8686
const file_path = `${__filename}/${TEMPLATE_DIRECTORY}/${file_name}`;

lib/services/template/play_index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs";
22
import { Log } from "../../../log/index.js";
33
import { DIRECTORIES } from "../../../util/const.js";
4-
import { isValidString } from "../../../util/string.js";
4+
import { isValidString, toSanitized, toSlug } from "../../../util/string.js";
55
import { loadAllPlaysAsync } from "../datasource/index.js";
66

77
const INDEX_FILE_PATH = `${DIRECTORIES.PLAY}/index.js`;
@@ -41,9 +41,11 @@ const writeFile = (plays, dirs) => {
4141
fs.appendFileSync(
4242
INDEX_FILE_PATH,
4343
`\n export { default as ${
44-
isValidString(play.component) || play.title_name
44+
isValidString(play.component) || toSanitized(play.title_name)
4545
} } from 'plays/${playPath}/${
46-
isValidString(play.component) || play.title_name || play.kebab_name
46+
isValidString(play.component) ||
47+
toSanitized(play.title_name) ||
48+
toSanitized(play.kebab_name)
4749
}';`
4850
);
4951
} else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-react-play",
3-
"version": "0.0.9",
3+
"version": "0.0.11",
44
"description": "Boiler plate for creating a play within reactplay system",
55
"main": "index.js",
66
"bin": {

util/string.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,17 @@ export const isValidString = (str) => {
4646
}
4747
return str;
4848
};
49+
50+
export const toSlug = (str) => {
51+
return toSanitized(str).toLowerCase();
52+
};
53+
54+
export const toSanitized = (str) => {
55+
//replace all special characters | symbols with a space
56+
str = str.replace(/[`~!@#$%^&*()_\-+=\[\]{};:'"\\|\/,.<>?\s]/g, " ");
57+
// trim spaces at start and end of string
58+
str = str.replace(/^\s+|\s+$/gm, "");
59+
// replace space with dash/hyphen
60+
str = str.replace(/\s+/g, "-");
61+
return str;
62+
};

0 commit comments

Comments
 (0)