diplodoc-platform / diplodoc-platform/transform
Memory leak due to uncontrolled initializations of markdown-it
- Dominant language
- TypeScript
- Stars
- 69
- Forks
- 59
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 6
Description
### Package version
4.41.0
### Node version
20.10.0
### Platform
_No response_
### What steps will reproduce the bug?
Hi! We are using @diplodoc/transform, and our users have noticed that when rapidly switching between screens containing markdown content, the memory usage of the tab keeps increasing. We found the cause in [these lines of code](https://github.com/diplodoc-platform/transform/blob/ac701ccb8a88db3d098dd0efce9ef41d02c2dacb/src/transform/index.ts#L43-L52). In short, due to the initializations of markdown-it and its plugins for each markdown transformation, a memory leak occurs.
We made some modifications in the code within node_modules: now, the [initMarkdownIt](https://github.com/diplodoc-platform/transform/blob/ac701ccb8a88db3d098dd0efce9ef41d02c2dacb/src/transform/md.ts#L15) function is called only once:
```js
function getTransformer(options = {}) {
const { parse, compile, env } = (0, md_1.default)(options);
return (originInput) => {
const input = applyLiquid(originInput, options);
try {
return emitResult(compile(parse(input)), env);
}
catch (error) {
handleError(error, options.path);
}
}
}
```
And the issue was resolved. The memory consumption stopped increasing.
### What is the expected behavior?
It is expected that with each use of the transform function, markdown-it along with its plugins will not be initialized uncontrollably, thereby preventing memory clutter.
### What do you see instead?
### Additional information
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.