Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ module: {
| defaultCharset | string | utf-8 | force the file reader to convert the file content into a specific charset |
| quietError | boolean | false | if the file cannot be found on local or online replace it with an error message or not |
| onFileMatch | function | null | callback on each SSI line match with 3 parameters : filePath&#60;string&#62;, fileContent&#60;string&#62;, isLocal&#60;boolean&#62;.<br> If you return a string it will override and replace the content |
| saveToDisk | boolean | false | saves files fetched online to disk (if not present) |
13 changes: 12 additions & 1 deletion lib/ssi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const request = require('then-request');
const iconv = require('iconv-lite');
const chardet = require('chardet');
const fs = require('fs');
const path = require('path');

/**
* take HTLM string scan it to find ssi include string
Expand All @@ -24,7 +25,8 @@ const SSI = function (param) {
disableLocalScan: false,
includesMatcher: /<!--\s?#\s?include\s+(?:virtual|file)="([^"]+)"(?:\s+stub="(\w+)")?\s?-->/,
onFileMatch: () => null,
}
saveToDisk: false,
};
const options = {
...defaultOptions,
...param,
Expand Down Expand Up @@ -73,6 +75,15 @@ const SSI = function (param) {
content = iconv.decode(res.body, charset);
}

const target = `${options.localPath}${location}`;

if (options.saveToDisk && !fs.existsSync(target)) {
const targetDir = path.dirname(target);

fs.mkdirSync(targetDir, { recursive: true });
fs.writeFileSync(target, content);
}

const modifiedContent = options.onFileMatch(url, content, false);
return modifiedContent || content;
} catch(e) {
Expand Down