wikity 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -31,7 +31,7 @@ else if (arg(1).includes('c')) {
31
31
  const configArgs = args.slice(2);
32
32
  const argsList = configArgs.join(' ');
33
33
  // retrieve item from arguments list
34
- const getArgContent = (arg) => arg.test(argsList) && configArgs.filter((_, i) => arg.test(configArgs[i - 1])).join(' ') || '';
34
+ const getArgContent = (arg) => arg.test(argsList) && configArgs.filter((_, i) => arg.test(configArgs[i - 1])).join(' ') || undefined;
35
35
  // Fetch user-supplied arguments
36
36
  const folder = arg(2) || '.';
37
37
  const outputFolder = getArgContent(/^-+o/);
package/dist/compile.js CHANGED
@@ -4,37 +4,45 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.compile = exports.eleventyCompile = void 0;
7
- const fs = require('fs');
8
- const glob = require('glob');
9
- const dedent = require('dedent');
10
- const formatter = require('html-formatter');
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const glob_1 = __importDefault(require("glob"));
10
+ const dedent_1 = __importDefault(require("dedent"));
11
+ const formatter = require('html-formatter'); // does not have type package
11
12
  const parse_1 = require("./parse");
12
- const common_1 = require("./common");
13
13
  const wiki_css_1 = __importDefault(require("./wiki.css"));
14
- const r = String.raw;
15
14
  function eleventyCompile(dir = '.', config = {}) {
16
15
  compile(dir, { eleventy: true, ...config });
17
16
  }
18
17
  exports.eleventyCompile = eleventyCompile;
19
18
  function compile(dir = '.', config = {}) {
19
+ var _a, _b, _c;
20
+ // set defaults
21
+ (_a = config.templatesFolder) !== null && _a !== void 0 ? _a : (config.templatesFolder = 'templates');
22
+ (_b = config.imagesFolder) !== null && _b !== void 0 ? _b : (config.imagesFolder = 'images');
23
+ (_c = config.outputFolder) !== null && _c !== void 0 ? _c : (config.outputFolder = 'wikity-out');
24
+ // directory variables (absolute paths)
25
+ const baseDir = path_1.default.resolve(dir);
26
+ const templatesFolder = path_1.default.join(baseDir, config.templatesFolder);
27
+ const imagesFolder = path_1.default.join(baseDir, config.imagesFolder);
28
+ const outputFolder = path_1.default.join(baseDir, config.outputFolder);
29
+ const outputImagesFolder = path_1.default.join(baseDir, config.outputFolder, config.imagesFolder);
30
+ const newConfig = { ...config, templatesFolder, imagesFolder, outputFolder };
20
31
  let stylesCreated = false;
21
32
  // Write wikitext files
22
- const files = glob.sync(dir + "/**/*.wiki", {});
33
+ const files = glob_1.default.sync(dir + "/**/*.wiki", {});
23
34
  files.forEach((file) => {
24
35
  var _a;
25
- const fileData = fs.readFileSync(file, { encoding: 'utf8' });
26
- const { data: parsedContent, metadata } = (0, parse_1.parse)(fileData, config);
36
+ const fileData = fs_1.default.readFileSync(file, { encoding: 'utf8' });
37
+ const { data: parsedContent, metadata } = (0, parse_1.parse)(fileData, newConfig);
27
38
  let outText = parsedContent;
28
- const templatesFolder = config.templatesFolder || 'templates';
29
- const imagesFolder = config.imagesFolder || 'images';
30
- const outputFolder = config.outputFolder || 'wikity-out';
31
- const [, folder, filename] = file.match((0, common_1.RegExpBuilder)(r `^(.+?[\/\\]) ((?:(?:${templatesFolder}|${imagesFolder})[\/\\])?[^\/\\]+)$`, '')) || [];
32
- const outFolder = dir + '/' + outputFolder + '/';
39
+ const filename = file.replace(dir, '').replace(/^[\/\\]/, '');
33
40
  const outFilename = filename.replace(/ /g, '_').replace('.wiki', '.html');
34
- const urlPath = outFilename.replace(/(?<=^|\/)\w/g, m => m.toUpperCase());
41
+ const outFilePath = path_1.default.join(outputFolder, outFilename);
42
+ const urlPath = outFilename.replace(/(?<=^|\/)\w/g, m => m.toUpperCase()); // capitalise first letters
35
43
  const displayTitle = metadata.displayTitle || urlPath.replace('.html', '');
36
44
  // Eleventy configuration
37
- const frontMatter = config.eleventy ? dedent `
45
+ const frontMatter = config.eleventy ? (0, dedent_1.default) `
38
46
  ---
39
47
  permalink: /wiki/${urlPath}
40
48
  ---
@@ -49,7 +57,7 @@ function compile(dir = '.', config = {}) {
49
57
  const lvl = +(((_a = match.match(/\d/g)) === null || _a === void 0 ? void 0 : _a[0]) || -1);
50
58
  toc += `${`<ol>`.repeat(lvl - 1)} <li> <a href="#${encodeURI(text.replace(/ /g, '_'))}">${text}</a> </li> ${`</ol>`.repeat(lvl - 1)}`;
51
59
  });
52
- const tocElem = dedent `
60
+ const tocElem = (0, dedent_1.default) `
53
61
  <div id="toc">
54
62
  <span id="toc-heading">
55
63
  <strong>Contents</strong>
@@ -70,14 +78,15 @@ function compile(dir = '.', config = {}) {
70
78
  // Create plaintext of HTML for use as description/metadata property.
71
79
  const plaintextData = parsedContent.replace(/<.+?>/gs, ' ');
72
80
  // Create HTML
73
- const html = dedent `
81
+ const folderUpCount = file.split(/[\/\\]/).length - dir.split(/[\/\\]/).length; // number of folders to go up by to get to root
82
+ const html = (0, dedent_1.default) `
74
83
  <html>
75
84
  <head>
76
85
  <meta charset="utf-8">
77
86
  <meta name="viewport" content="initial-scale=1.0, width=device-width">
78
87
  <meta name="description" content="${plaintextData.substring(0, 256)}...">
79
88
  <title>${displayTitle}</title>
80
- <link id="default-styles" rel="stylesheet" href="/wiki.css">
89
+ <link id="default-styles" rel="stylesheet" href="${'../'.repeat(folderUpCount)}./wiki.css">
81
90
  </head>
82
91
  <body>
83
92
  <header>
@@ -94,25 +103,21 @@ function compile(dir = '.', config = {}) {
94
103
  </html>
95
104
  `;
96
105
  // Write to file
97
- for (const path of ['', templatesFolder, imagesFolder]) {
98
- if (!fs.existsSync(outFolder + path)) {
99
- fs.mkdirSync(outFolder + path);
100
- }
106
+ if (!fs_1.default.existsSync(path_1.default.dirname(outFilePath))) {
107
+ fs_1.default.mkdirSync(path_1.default.dirname(outFilePath));
101
108
  }
102
- ;
103
109
  const renderedHtml = formatter.render(html).replace(/(<\/\w+>)(\S)/g, '$1 $2');
104
- fs.writeFileSync(outFolder + outFilename, frontMatter + '\n' + renderedHtml, 'utf8');
110
+ fs_1.default.writeFileSync(outFilePath, frontMatter + '\n' + renderedHtml, 'utf8');
105
111
  // Move images
106
- glob(imagesFolder + '/*', {}, (err, files) => {
112
+ (0, glob_1.default)(imagesFolder + '/*', {}, (err, files) => {
107
113
  if (err) {
108
114
  console.warn(err);
109
115
  }
110
- const outImagesFolder = outFolder + imagesFolder + '/';
111
- if (!fs.existsSync(outImagesFolder)) {
112
- fs.mkdirSync(outImagesFolder);
116
+ if (!fs_1.default.existsSync(outputImagesFolder)) {
117
+ fs_1.default.mkdirSync(outputImagesFolder);
113
118
  }
114
119
  for (const file of files) {
115
- fs.copyFileSync(file, outImagesFolder + file.split(/[/\\]/).pop());
120
+ fs_1.default.copyFileSync(file, path_1.default.join(outputImagesFolder, path_1.default.basename(file)));
116
121
  }
117
122
  ;
118
123
  });
@@ -126,13 +131,14 @@ function compile(dir = '.', config = {}) {
126
131
  if (config.customStyles) {
127
132
  styles += config.customStyles;
128
133
  }
129
- const cssOutput = dedent `
134
+ const cssOutput = config.eleventy ? (0, dedent_1.default) `
130
135
  ---
131
136
  permalink: /wiki.css
132
137
  ---
133
138
  ${styles}
134
- `;
135
- fs.writeFileSync(outFolder + 'wiki.css.njk', cssOutput);
139
+ ` : styles;
140
+ const cssOutFilename = config.eleventy ? 'wiki.css.njk' : 'wiki.css';
141
+ fs_1.default.writeFileSync(path_1.default.join(outputFolder, cssOutFilename), cssOutput);
136
142
  }
137
143
  });
138
144
  }
package/dist/parse.js CHANGED
@@ -1,21 +1,30 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.parse = exports.rawParse = void 0;
4
- const fs = require('fs');
5
- const htmlEscape = require('escape-html');
6
- const dateFormat = require('dateformat');
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const escape_html_1 = __importDefault(require("escape-html"));
10
+ const dateformat_1 = __importDefault(require("dateformat"));
7
11
  const common_1 = require("./common");
8
12
  const r = String.raw;
9
13
  const MAX_RECURSION = 20;
10
- const MAX_ARG_COUNT = 10;
11
14
  const arg = r `\s*([^|}]+?)\s*`;
15
+ function fullyEscape(text) {
16
+ return (0, escape_html_1.default)(text)
17
+ .replace(/{/g, '&#123;'); // avoid keeping plain {{}} which is parsed as a template call
18
+ }
12
19
  function rawParse(data, config = {}) {
13
20
  return parse(data, config).data;
14
21
  }
15
22
  exports.rawParse = rawParse;
16
23
  function parse(data, config = {}) {
17
- const templatesFolder = config.templatesFolder || 'templates';
18
- const imagesFolder = config.imagesFolder || 'images';
24
+ var _a, _b, _c;
25
+ const templatesFolder = (_a = config.templatesFolder) !== null && _a !== void 0 ? _a : 'templates';
26
+ const imagesFolder = (_b = config.imagesFolder) !== null && _b !== void 0 ? _b : 'images';
27
+ const outputFolder = (_c = config.outputFolder) !== null && _c !== void 0 ? _c : 'wikity-out';
19
28
  const vars = {};
20
29
  const metadata = {};
21
30
  const nowikis = [];
@@ -24,7 +33,6 @@ function parse(data, config = {}) {
24
33
  let rawExtLinkCount = 0;
25
34
  let refCount = 0;
26
35
  let outText = data;
27
- let stylesCreated = false;
28
36
  for (let l = 0, last = ''; l < MAX_RECURSION; l++) {
29
37
  if (last === outText)
30
38
  break;
@@ -93,20 +101,21 @@ function parse(data, config = {}) {
93
101
  const errMsg = `Wikity does not use Wikipedia's #time function syntax. Use repetition-based formatting (e.g. yyyy-mm-dd) instead.`;
94
102
  console.warn(`<Wikity> [WARN] ${errMsg}`);
95
103
  }
96
- return dateFormat(args[1] ? new Date(args[1]) : new Date(), args[0]);
104
+ return (0, dateformat_1.default)(args[1] ? new Date(args[1]) : new Date(), args[0]);
97
105
  }
98
106
  })
99
107
  // Templates: {{template}}
100
108
  .replace((0, common_1.RegExpBuilder)(r `(?<!{) {{ \s* ([^#{}|]+?) (\|[^{}]+)? }} (?!})`), (_, title, params = '') => {
101
- const page = templatesFolder + '/' + title.trim().replace(/ /g, '_');
109
+ const page = path_1.default.join(templatesFolder, title.trim().replace(/ /g, '_'));
102
110
  let content = '';
103
111
  // Try retrieve template content
104
112
  try {
105
- content = fs.readFileSync('./' + page + '.wiki', { encoding: 'utf8' });
113
+ content = fs_1.default.readFileSync(page + '.wiki', { encoding: 'utf8' });
106
114
  }
107
115
  catch {
108
116
  // Return redlink if template doesn't exist
109
- return `<a class="internal-link redlink" title="${title}" href="${page}">${title}</a>`;
117
+ const relPage = path_1.default.basename(templatesFolder) + '/' + path_1.default.relative(templatesFolder, page);
118
+ return `<a class="internal-link redlink" title="${title}" href="${relPage}">${title}</a>`;
110
119
  }
111
120
  // Remove non-template sections
112
121
  content = content
@@ -133,7 +142,7 @@ function parse(data, config = {}) {
133
142
  .replace((0, common_1.RegExpBuilder)(r `\[\[ (?:File|Image): (.+?) (\|.+?)? \]\]`), (_, file, params = '') => {
134
143
  if (/{{/.test(params))
135
144
  return _;
136
- const path = imagesFolder + '/' + file.trim().replace(/ /g, '_');
145
+ const path = path_1.default.join(imagesFolder, file.trim().replace(/ /g, '_'));
137
146
  let caption = '';
138
147
  let imageData = {};
139
148
  let imageArgs = params.split('|').map((arg) => arg.replace(/"/g, '&quot;'));
@@ -197,7 +206,7 @@ function parse(data, config = {}) {
197
206
  "
198
207
  >
199
208
  <img
200
- src="${path}"
209
+ src="${path_1.default.basename(imagesFolder)}/${path_1.default.relative(imagesFolder, path)}"
201
210
  alt="${imageData.alt || file}"
202
211
  width="${imageData.width || 300}"
203
212
  height="${imageData.height || 300}"
@@ -235,7 +244,8 @@ function parse(data, config = {}) {
235
244
  .replace((0, common_1.RegExpBuilder)(r `^ \{\| (.*?) $`), (_, attrs) => `<table ${attrs}><tr>`)
236
245
  .replace((0, common_1.RegExpBuilder)(r `^ ! ([^]+?) (?= \n^[!|] )`), (_, content) => `<th>${content}</th>`)
237
246
  .replace((0, common_1.RegExpBuilder)(r `^ \|\+ (.*?) $`), (_, content) => `<caption>${content}</caption>`)
238
- .replace((0, common_1.RegExpBuilder)(r `^ \|[^-+}] ([^]*?) (?= \n^[!|] )`), (_, content) => `<td>${content}</td>`)
247
+ .replace((0, common_1.RegExpBuilder)(r `^ \|[^-+}] ([^]*?) (?= \n | \|\| )`), (_, content) => `<td>${content}</td>`)
248
+ .replace((0, common_1.RegExpBuilder)(r `\|\|[^-+}] ([^]*?) (?= \n | \|\| )`), (_, content) => `<td>${content}</td>`)
239
249
  .replace((0, common_1.RegExpBuilder)(r `^ \|- (.*?) $`), (_, attrs) => `</tr><tr ${attrs}>`)
240
250
  .replace((0, common_1.RegExpBuilder)(r `^ \|\}`), `</tr></table>`)
241
251
  // References: <ref></ref>, <references/>
@@ -249,10 +259,12 @@ function parse(data, config = {}) {
249
259
  .replace((0, common_1.RegExpBuilder)(r ` \`\`\` ([^\`]+?) \`\`\` `), '<pre>$1</pre>')
250
260
  .replace((0, common_1.RegExpBuilder)(r ` \`\` ([^\`]+?) \`\` `), '<code>$1</code>')
251
261
  // Spacing
252
- .replace(/(\r?\n){2}/g, '\n</p><p>\n')
253
- // Restore nowiki contents
254
- .replace(/%NOWIKI#(\d+)%/g, (_, n) => htmlEscape(nowikis[n]));
262
+ .replace(/(\r?\n){2}/g, '\n</p><p>\n');
255
263
  }
264
+ // Final changes (run only once)
265
+ outText = outText
266
+ // Restore nowiki contents
267
+ .replace(/%NOWIKI#(\d+)%/g, (_, n) => fullyEscape(nowikis[n]));
256
268
  const result = { data: outText, metadata: metadata };
257
269
  return result;
258
270
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikity",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "Compile wikitext to HTML: wikitext as a templating language.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -39,20 +39,16 @@
39
39
  "license": "ISC",
40
40
  "dependencies": {
41
41
  "dateformat": "^4.6.3",
42
- "dedent": "^0.7.0",
42
+ "dedent": "^1.5.3",
43
43
  "escape-html": "^1.0.3",
44
- "glob": "^7.2.3",
44
+ "glob": "^8.1.0",
45
45
  "html-formatter": "^0.1.9"
46
46
  },
47
- "peerDependencies": {
48
- "@11ty/eleventy": ">=0.10.0"
49
- },
50
- "peerDependenciesMeta": {
51
- "@11ty/eleventy": {
52
- "optional": true
53
- }
54
- },
55
47
  "devDependencies": {
48
+ "@types/dateformat": "^3.0.1",
49
+ "@types/dedent": "^0.7.2",
50
+ "@types/escape-html": "^1.0.4",
51
+ "@types/glob": "^8.1.0",
56
52
  "@types/node": "ts5.0",
57
53
  "typescript": "~5.0.4 <5.1"
58
54
  }
package/readme.md CHANGED
@@ -69,12 +69,13 @@ Use Wikity along with Eleventy to have all your wiki files compiled during the b
69
69
  // .eleventy.js (eleventy's configuration file)
70
70
  const wikity = require('wikity');
71
71
  module.exports = function (eleventyConfig) {
72
- const wikiFolder = 'src';
73
- const templatesFolder = 'templates', imagesFolder = 'images', outputFolder = 'wikity-out'; // defaults
74
- const wikityOptions = { templatesFolder, imagesFolder, outputFolder };
75
- const wikityPlugin = () => wikity.compile(wikiFolder, { eleventy: true, ...wikityOptions });
72
+ const rootFolder = 'src';
73
+ const templatesFolder = 'templates', imagesFolder = 'images'; // defaults; relative to root folder
74
+ const outputFolder = 'wikity-out'; // default
75
+ const wikityOptions = { eleventy: true, templatesFolder, imagesFolder, outputFolder };
76
+ const wikityPlugin = () => wikity.compile(rootFolder, wikityOptions);
76
77
  eleventyConfig.addPlugin(wikityPlugin);
77
- eleventyConfig.addPassthroughCopy({[imagesFolder]: 'wiki/' + imagesFolder}); // Eleventy does not pass through images by default
78
+ eleventyConfig.addPassthroughCopy({ 'src/images': 'wiki/' + imagesFolder }); // Eleventy does not pass through images by default
78
79
  }
79
80
  ```
80
81
 
@@ -83,10 +84,10 @@ The above will use the following file structure (with some example wiki files gi
83
84
  - `src/`
84
85
  - `templates/`: Directory for wiki templates (called like `{{this}}`)
85
86
  - `images/`: Directory to place images (called like `[[File:this]]`)
86
- - `wikity-out/`: File templates compiled from the `.wiki` files (add this to `.gitignore`)
87
87
  - `Index.wiki`: Example file
88
88
  - `Other_Page.wiki`: Example other file
89
- - `wiki/`: Output HTML files compiled from wikity-out (add this to `.gitignore`)
89
+ - `wikity-out/`: File templates compiled from the `.wiki` files (add this to `.gitignore`)
90
+ - `wiki/`: Output HTML files compiled from `wikity-out/` (add this to `.gitignore`)
90
91
 
91
92
  (View the above starting at the URL path `/wiki/` when ran in an HTTP server.)
92
93