Skip to content

Commit

Permalink
Merge pull request #3 from nhn/feat/injectScriptForErrorCatch
Browse files Browse the repository at this point in the history
feat: injectScript for example page error catch
  • Loading branch information
jinwoo-kim-nhn authored Apr 2, 2020
2 parents c871945 + 0aaadd1 commit d0e125c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ A list of product related links including company information can be displayed.
| `examples` | `object \| boolean` | Configures options to use the Examples page. If set to `false`, the Examples tab is hidden from the local navigation bar. |
| `examples.filePath` | `string` | Configures the file path to be displayed on the Examples page. Declare the folder with example files in `string` format. |
| `examples.titles` | `object` | Maps each example file to the menu name to be displayed on the local navigation bar. The configuration should be made in `{ [Example File Name]: [LNB Menu Name]}` format. |
| `examples.globalErrorLogVariable` | `?string \| ?boolean` | Automatically inserts the code snippet that puts the error in the example page into a global variable. Variable names can be set directly as strings. If set to `true`, the default `errorLogs` is used. |

#### Others

Expand Down Expand Up @@ -246,4 +247,4 @@ For more detailed explanation on making a PR, refer to the **Contributing** appe

## 📜 License

This software is provided under [MIT License](https://github.com/nhn/toast-ui.doc/blob/master/LICENSE). © [NHN](https://github.com/nhn).
This software is provided under [MIT License](https://github.com/nhn/toast-ui.doc/blob/master/LICENSE). © [NHN](https://github.com/nhn).
29 changes: 24 additions & 5 deletions bin/exampleDataFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const examples = config.examples || false;

const {
filePath,
titles
titles,
globalErrorLogVariable = false
} = examples;

const EXAMPLE_FILES_PATH = path.resolve(pwd, filePath || '');
Expand Down Expand Up @@ -113,10 +114,10 @@ function makeExamplePageDataFile(data) {
}

/**
* Read example files
* Post processing for example files
*/
function readExampleFiles() {
nodedir.readFiles(EXAMPLE_FILES_PATH, {
function postProcessingOfExampleFiles() {
nodedir.readFiles(COPY_FILES_PATH, {
match: /.html$/,
recursive: true
}, (err, content, filename, next) => {
Expand All @@ -128,6 +129,10 @@ function readExampleFiles() {
const data = makeExamplePageData(parsedContent, filename);

makeExamplePageDataFile(data);

if (globalErrorLogVariable) {
injectScriptForErrorCatch(content, filename);
}
next(); // read next file
});
}
Expand All @@ -141,10 +146,24 @@ function copyExampleFiles() {
throw err;
}

readExampleFiles();
postProcessingOfExampleFiles();
});
}

/**
* Inject script for error catch
* @param {string} content - example page html string
* @param {string} filename - exmaple page's filename
*/
function injectScriptForErrorCatch(content, filename) {
const injectVariable = typeof globalErrorLogVariable === 'string' ? globalErrorLogVariable : 'errorLogs';
const injectScriptString =
`var ${injectVariable}=[];window.onerror=function(o,r,e,n){errorLogs.push({message:o,source:r,lineno:e,colno:n})};`;
const newContent = content.replace(/(\n?)(\s*)(<\/head>)/i, `$1$2$2<script>${injectScriptString}</script>$1$2$3`);

fs.writeFileSync(filename, newContent, {encoding: 'utf-8'});
}

/**
* Copy external files to folder
*/
Expand Down

0 comments on commit d0e125c

Please sign in to comment.