lexers.js 743 B

12345678910111213141516171819202122232425262728293031
  1. const EventEmitter = require('events');
  2. const jsonc = require('comment-json');
  3. /**
  4. * Custom JSON parser for localizing keys matching format: /%.+%/
  5. */
  6. module.exports.CustomJSONLexer = class extends EventEmitter {
  7. extract(content, filename) {
  8. let keys = [];
  9. console.log(1)
  10. try {
  11. jsonc.parse(
  12. content,
  13. (key, value) => {
  14. if (typeof value === 'string') {
  15. const match = value.match(/^%(.+)%$/);
  16. if (match && match[1]) {
  17. keys.push({ key: match[1] });
  18. }
  19. }
  20. return value;
  21. },
  22. true,
  23. );
  24. } catch (e) {
  25. console.error('Failed to parse as JSON.', filename, e);
  26. keys = [];
  27. }
  28. return keys;
  29. }
  30. };