@lvce-editor/extension-detail-view 2.0.0 → 2.2.0

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.
@@ -1,3 +1,44 @@
1
+ class AssertionError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = 'AssertionError';
5
+ }
6
+ }
7
+ const getType = value => {
8
+ switch (typeof value) {
9
+ case 'number':
10
+ return 'number';
11
+ case 'function':
12
+ return 'function';
13
+ case 'string':
14
+ return 'string';
15
+ case 'object':
16
+ if (value === null) {
17
+ return 'null';
18
+ }
19
+ if (Array.isArray(value)) {
20
+ return 'array';
21
+ }
22
+ return 'object';
23
+ case 'boolean':
24
+ return 'boolean';
25
+ default:
26
+ return 'unknown';
27
+ }
28
+ };
29
+ const array = value => {
30
+ const type = getType(value);
31
+ if (type !== 'array') {
32
+ throw new AssertionError('expected value to be of type array');
33
+ }
34
+ };
35
+ const string = value => {
36
+ const type = getType(value);
37
+ if (type !== 'string') {
38
+ throw new AssertionError('expected value to be of type string');
39
+ }
40
+ };
41
+
1
42
  const Two = '2.0';
2
43
  const create$4 = (method, params) => {
3
44
  return {
@@ -6,17 +47,17 @@ const create$4 = (method, params) => {
6
47
  params
7
48
  };
8
49
  };
9
- const state = {
50
+ const state$1 = {
10
51
  callbacks: Object.create(null)
11
52
  };
12
53
  const set = (id, fn) => {
13
- state.callbacks[id] = fn;
54
+ state$1.callbacks[id] = fn;
14
55
  };
15
56
  const get = id => {
16
- return state.callbacks[id];
57
+ return state$1.callbacks[id];
17
58
  };
18
59
  const remove = id => {
19
- delete state.callbacks[id];
60
+ delete state$1.callbacks[id];
20
61
  };
21
62
  let id = 0;
22
63
  const create$3 = () => {
@@ -339,7 +380,7 @@ const send = (transport, method, ...params) => {
339
380
  const message = create$4(method, params);
340
381
  transport.send(message);
341
382
  };
342
- const invoke = (ipc, method, ...params) => {
383
+ const invoke$1 = (ipc, method, ...params) => {
343
384
  return invokeHelper(ipc, method, params, false);
344
385
  };
345
386
  const invokeAndTransfer = (ipc, method, ...params) => {
@@ -753,7 +794,7 @@ const createRpc = ipc => {
753
794
  send(ipc, method, ...params);
754
795
  },
755
796
  invoke(method, ...params) {
756
- return invoke(ipc, method, ...params);
797
+ return invoke$1(ipc, method, ...params);
757
798
  },
758
799
  invokeAndTransfer(method, ...params) {
759
800
  return invokeAndTransfer(ipc, method, ...params);
@@ -774,13 +815,18 @@ const handleMessage = event => {
774
815
  return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, requiresSocket);
775
816
  };
776
817
  const handleIpc = ipc => {
777
- ipc.addEventListener('message', handleMessage);
818
+ if ('addEventListener' in ipc) {
819
+ ipc.addEventListener('message', handleMessage);
820
+ } else if ('on' in ipc) {
821
+ // deprecated
822
+ ipc.on('message', handleMessage);
823
+ }
778
824
  };
779
-
780
- // @ts-ignore
781
- const listen$1 = async () => {
782
- const module = IpcChildWithModuleWorkerAndMessagePort$1;
783
- const rawIpc = await module.listen();
825
+ const listen$1 = async (module, options) => {
826
+ const rawIpc = await module.listen(options);
827
+ if (module.signal) {
828
+ module.signal(rawIpc);
829
+ }
784
830
  const ipc = module.wrap(rawIpc);
785
831
  return ipc;
786
832
  };
@@ -789,7 +835,7 @@ const create = async ({
789
835
  }) => {
790
836
  // TODO create a commandMap per rpc instance
791
837
  register(commandMap);
792
- const ipc = await listen$1();
838
+ const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
793
839
  handleIpc(ipc);
794
840
  const rpc = createRpc(ipc);
795
841
  return rpc;
@@ -799,6 +845,808 @@ const WebWorkerRpcClient = {
799
845
  create
800
846
  };
801
847
 
848
+ const Document = 'document';
849
+
850
+ const ExtensionDetail = 'ExtensionDetail';
851
+ const ExtensionDetailDescription = 'ExtensionDetailDescription';
852
+ const ExtensionDetailHeader = 'ExtensionDetailHeader';
853
+ const ExtensionDetailHeaderDetails = 'ExtensionDetailHeaderDetails';
854
+ const ExtensionDetailIcon = 'ExtensionDetailIcon';
855
+ const ExtensionDetailName = 'ExtensionDetailName';
856
+ const Markdown = 'Markdown';
857
+ const Viewlet = 'Viewlet';
858
+
859
+ const HandleReadmeContextMenu = 'handleReadmeContextMenu';
860
+
861
+ const Div$1 = 4;
862
+ const H1$1 = 5;
863
+ const Span$1 = 8;
864
+ const Text$1 = 12;
865
+ const Img$1 = 17;
866
+ const H2$1 = 22;
867
+ const H3$1 = 23;
868
+ const H4$1 = 24;
869
+ const H5$1 = 25;
870
+ const Article$1 = 27;
871
+ const Aside$1 = 28;
872
+ const Footer$1 = 29;
873
+ const Header$1 = 30;
874
+ const Nav$1 = 40;
875
+ const Section$1 = 41;
876
+ const Search$1 = 42;
877
+ const Dd$1 = 43;
878
+ const Dl$1 = 44;
879
+ const Figcaption$1 = 45;
880
+ const Figure$1 = 46;
881
+ const Hr$1 = 47;
882
+ const Li$1 = 48;
883
+ const Ol$1 = 49;
884
+ const P$1 = 50;
885
+ const Pre$1 = 51;
886
+ const A$1 = 53;
887
+ const Abbr$1 = 54;
888
+ const Br$1 = 55;
889
+ const Cite$1 = 56;
890
+ const Data$1 = 57;
891
+ const Time$1 = 58;
892
+ const Tfoot$1 = 59;
893
+
894
+ const text = data => {
895
+ return {
896
+ type: Text$1,
897
+ text: data,
898
+ childCount: 0
899
+ };
900
+ };
901
+
902
+ const getExtensionDetailHeaderVirtualDom = extensionDetail => {
903
+ const {
904
+ name,
905
+ iconSrc,
906
+ description
907
+ } = extensionDetail;
908
+ const dom = [{
909
+ type: Div$1,
910
+ className: ExtensionDetailHeader,
911
+ childCount: 2
912
+ }, {
913
+ type: Img$1,
914
+ className: ExtensionDetailIcon,
915
+ alt: '',
916
+ draggable: false,
917
+ childCount: 0,
918
+ src: iconSrc
919
+ }, {
920
+ type: Div$1,
921
+ className: ExtensionDetailHeaderDetails,
922
+ childCount: 2
923
+ }, {
924
+ type: Div$1,
925
+ className: ExtensionDetailName,
926
+ childCount: 1
927
+ }, text(name), {
928
+ type: Div$1,
929
+ className: ExtensionDetailDescription,
930
+ childCount: 1
931
+ }, text(description)];
932
+ return dom;
933
+ };
934
+
935
+ const allowedMarkdownAttributes = ['src', 'id', 'className', 'title', 'alt', 'href', 'target', 'rel'];
936
+
937
+ const Div = 'div';
938
+ const H1 = 'h1';
939
+ const H2 = 'h2';
940
+ const H3 = 'h3';
941
+ const H4 = 'h4';
942
+ const H5 = 'h5';
943
+ const Img = 'img';
944
+ const Span = 'span';
945
+ const Article = 'article';
946
+ const Aside = 'aside';
947
+ const Footer = 'footer';
948
+ const Header = 'header';
949
+ const Nav = 'nav';
950
+ const Section = 'section';
951
+ const Search = 'search';
952
+ const Dd = 'dd';
953
+ const Dl = 'dl';
954
+ const Figcaption = 'figcaption';
955
+ const Figure = 'figure';
956
+ const Hr = 'hr';
957
+ const Li = 'li';
958
+ const Ol = 'ol';
959
+ const P = 'p';
960
+ const Pre = 'pre';
961
+ const A = 'a';
962
+ const Abbr = 'abbr';
963
+ const Br = 'br';
964
+ const Cite = 'cite';
965
+ const Data = 'data';
966
+ const Time = 'time';
967
+ const Tfoot = 'tfoot';
968
+
969
+ const getVirtualDomTag = text => {
970
+ switch (text) {
971
+ case H1:
972
+ return H1$1;
973
+ case H2:
974
+ return H2$1;
975
+ case H3:
976
+ return H3$1;
977
+ case H4:
978
+ return H4$1;
979
+ case H5:
980
+ return H5$1;
981
+ case Div:
982
+ return Div$1;
983
+ case Article:
984
+ return Article$1;
985
+ case Aside:
986
+ return Aside$1;
987
+ case Footer:
988
+ return Footer$1;
989
+ case Header:
990
+ return Header$1;
991
+ case Nav:
992
+ return Nav$1;
993
+ case Section:
994
+ return Section$1;
995
+ case Search:
996
+ return Search$1;
997
+ case Dd:
998
+ return Dd$1;
999
+ case Dl:
1000
+ return Dl$1;
1001
+ case Figcaption:
1002
+ return Figcaption$1;
1003
+ case Figure:
1004
+ return Figure$1;
1005
+ case Hr:
1006
+ return Hr$1;
1007
+ case Li:
1008
+ return Li$1;
1009
+ case Ol:
1010
+ return Ol$1;
1011
+ case P:
1012
+ return P$1;
1013
+ case Pre:
1014
+ return Pre$1;
1015
+ case A:
1016
+ return A$1;
1017
+ case Abbr:
1018
+ return Abbr$1;
1019
+ case Br:
1020
+ return Br$1;
1021
+ case Cite:
1022
+ return Cite$1;
1023
+ case Data:
1024
+ return Data$1;
1025
+ case Time:
1026
+ return Time$1;
1027
+ case Tfoot:
1028
+ return Tfoot$1;
1029
+ case Img:
1030
+ return Img$1;
1031
+ case Span:
1032
+ return Span$1;
1033
+ default:
1034
+ return Div$1;
1035
+ }
1036
+ };
1037
+
1038
+ const None$1 = 0;
1039
+ const OpeningAngleBracket = 1;
1040
+ const ClosingAngleBracket = 2;
1041
+ const TagNameStart = 3;
1042
+ const TagNameEnd = 4;
1043
+ const Content = 5;
1044
+ const ClosingTagSlash = 6;
1045
+ const WhitespaceInsideOpeningTag = 7;
1046
+ const AttributeName = 8;
1047
+ const AttributeEqualSign = 9;
1048
+ const AttributeQuoteStart = 10;
1049
+ const AttributeValue = 11;
1050
+ const AttributeQuoteEnd = 12;
1051
+ const WhitespaceAfterClosingTagSlash = 13;
1052
+ const WhitespaceAfterOpeningTagOpenAngleBracket = 14;
1053
+ const ExclamationMark = 15;
1054
+ const Doctype = 16;
1055
+ const StartCommentDashes = 17;
1056
+ const Comment = 18;
1057
+ const EndCommentTag = 19;
1058
+ const Text = 20;
1059
+ const CommentStart = 21;
1060
+
1061
+ const isSelfClosingTag = tag => {
1062
+ switch (tag) {
1063
+ case Img:
1064
+ return true;
1065
+ default:
1066
+ return false;
1067
+ }
1068
+ };
1069
+
1070
+ const parseText = text => {
1071
+ return text.replaceAll('&gt;', '>').replaceAll('&lt;', '<').replaceAll('&amp;', '&');
1072
+ };
1073
+
1074
+ class UnexpectedTokenError extends Error {
1075
+ constructor() {
1076
+ super('Unexpected token');
1077
+ this.name = 'UnexpectedTokenError';
1078
+ }
1079
+ }
1080
+
1081
+ const State = {
1082
+ TopLevelContent: 1,
1083
+ AfterOpeningAngleBracket: 2,
1084
+ InsideOpeningTag: 3,
1085
+ AfterClosingTagSlash: 4,
1086
+ AfterClosingTagName: 5,
1087
+ InsideOpeningTagAfterWhitespace: 6,
1088
+ AfterAttributeName: 7,
1089
+ AfterAttributeEqualSign: 8,
1090
+ InsideAttributeAfterDoubleQuote: 9,
1091
+ AfterAttributeValueInsideDoubleQuote: 10,
1092
+ AfterAttributeValueClosingQuote: 11,
1093
+ AfterExclamationMark: 16,
1094
+ InsideComment: 17
1095
+ };
1096
+ const RE_ANGLE_BRACKET_OPEN = /^</;
1097
+ const RE_ANGLE_BRACKET_OPEN_TAG = /^<(?![\s!%])/;
1098
+ const RE_ANGLE_BRACKET_CLOSE = /^>/;
1099
+ const RE_SLASH = /^\//;
1100
+ const RE_TAGNAME = /^[a-zA-Z\d$]+/;
1101
+ const RE_CONTENT = /^[^<>]+/;
1102
+ const RE_WHITESPACE = /^\s+/;
1103
+ const RE_ATTRIBUTE_NAME = /^[a-zA-Z\d-]+/;
1104
+ const RE_EQUAL_SIGN = /^=/;
1105
+ const RE_DOUBLE_QUOTE = /^"/;
1106
+ const RE_ATTRIBUTE_VALUE_INSIDE_DOUBLE_QUOTE = /^[^"\n]+/;
1107
+ const RE_TEXT = /^[^<>]+/;
1108
+ const RE_EXCLAMATION_MARK = /^!/;
1109
+ const RE_DASH_DASH = /^--/;
1110
+ const RE_DOCTYPE = /^doctype/i;
1111
+ const RE_BLOCK_COMMENT_CONTENT = /^[a-zA-Z\s]+/;
1112
+ const RE_COMMENT_END = /^-->/;
1113
+ const RE_TAG_TEXT = /^[^\s>]+/;
1114
+ const RE_ANY_TEXT = /^[^\n]+/;
1115
+ const RE_BLOCK_COMMENT_START = /^<!--/;
1116
+ const RE_SELF_CLOSING = /^\/>/;
1117
+ const tokenizeHtml = text => {
1118
+ string(text);
1119
+ let state = State.TopLevelContent;
1120
+ let index = 0;
1121
+ let next;
1122
+ const tokens = [];
1123
+ let token = None$1;
1124
+ while (index < text.length) {
1125
+ const part = text.slice(index);
1126
+ switch (state) {
1127
+ case State.TopLevelContent:
1128
+ if (next = part.match(RE_ANGLE_BRACKET_OPEN_TAG)) {
1129
+ token = OpeningAngleBracket;
1130
+ state = State.AfterOpeningAngleBracket;
1131
+ } else if (next = part.match(RE_CONTENT)) {
1132
+ token = Content;
1133
+ state = State.TopLevelContent;
1134
+ } else if (next = part.match(RE_BLOCK_COMMENT_START)) {
1135
+ token = CommentStart;
1136
+ state = State.InsideComment;
1137
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1138
+ token = Content;
1139
+ state = State.TopLevelContent;
1140
+ } else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
1141
+ token = Text;
1142
+ state = State.TopLevelContent;
1143
+ } else {
1144
+ throw new UnexpectedTokenError();
1145
+ }
1146
+ break;
1147
+ case State.AfterOpeningAngleBracket:
1148
+ if (next = part.match(RE_TAGNAME)) {
1149
+ token = TagNameStart;
1150
+ state = State.InsideOpeningTag;
1151
+ } else if (next = part.match(RE_SLASH)) {
1152
+ token = ClosingTagSlash;
1153
+ state = State.AfterClosingTagSlash;
1154
+ } else if (next = part.match(RE_WHITESPACE)) {
1155
+ token = WhitespaceAfterOpeningTagOpenAngleBracket;
1156
+ state = State.TopLevelContent;
1157
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1158
+ token = ClosingAngleBracket;
1159
+ state = State.TopLevelContent;
1160
+ } else if (next = part.match(RE_EXCLAMATION_MARK)) {
1161
+ token = ExclamationMark;
1162
+ state = State.AfterExclamationMark;
1163
+ } else if (next = part.match(RE_ANY_TEXT)) {
1164
+ token = Text;
1165
+ state = State.TopLevelContent;
1166
+ } else {
1167
+ text.slice(index); // ?
1168
+ throw new UnexpectedTokenError();
1169
+ }
1170
+ break;
1171
+ case State.AfterExclamationMark:
1172
+ if (next = part.match(RE_DASH_DASH)) {
1173
+ token = StartCommentDashes;
1174
+ state = State.InsideComment;
1175
+ } else if (next = part.match(RE_DOCTYPE)) {
1176
+ token = Doctype;
1177
+ state = State.InsideOpeningTag;
1178
+ } else {
1179
+ text.slice(index); // ?
1180
+ throw new UnexpectedTokenError();
1181
+ }
1182
+ break;
1183
+ case State.InsideComment:
1184
+ if (next = part.match(RE_BLOCK_COMMENT_CONTENT)) {
1185
+ token = Comment;
1186
+ state = State.InsideComment;
1187
+ } else if (next = part.match(RE_COMMENT_END)) {
1188
+ token = EndCommentTag;
1189
+ state = State.TopLevelContent;
1190
+ } else {
1191
+ text.slice(index); // ?
1192
+ throw new UnexpectedTokenError();
1193
+ }
1194
+ break;
1195
+ case State.InsideOpeningTag:
1196
+ if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1197
+ token = ClosingAngleBracket;
1198
+ state = State.TopLevelContent;
1199
+ } else if (next = part.match(RE_WHITESPACE)) {
1200
+ token = WhitespaceInsideOpeningTag;
1201
+ state = State.InsideOpeningTagAfterWhitespace;
1202
+ } else if (next = part.match(RE_TAG_TEXT)) {
1203
+ token = Text;
1204
+ state = State.TopLevelContent;
1205
+ } else {
1206
+ throw new UnexpectedTokenError();
1207
+ }
1208
+ break;
1209
+ case State.InsideOpeningTagAfterWhitespace:
1210
+ if (next = part.match(RE_ATTRIBUTE_NAME)) {
1211
+ token = AttributeName;
1212
+ state = State.AfterAttributeName;
1213
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1214
+ token = ClosingAngleBracket;
1215
+ state = State.TopLevelContent;
1216
+ } else if (next = part.match(RE_SELF_CLOSING)) {
1217
+ token = ClosingAngleBracket;
1218
+ state = State.TopLevelContent;
1219
+ } else if (next = part.match(RE_TEXT)) {
1220
+ token = AttributeName;
1221
+ state = State.AfterAttributeName;
1222
+ } else {
1223
+ text.slice(index).match(RE_TEXT); // ?
1224
+ text.slice(index); // ?
1225
+ throw new UnexpectedTokenError();
1226
+ }
1227
+ break;
1228
+ case State.AfterAttributeName:
1229
+ if (next = part.match(RE_EQUAL_SIGN)) {
1230
+ token = AttributeEqualSign;
1231
+ state = State.AfterAttributeEqualSign;
1232
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1233
+ token = ClosingAngleBracket;
1234
+ state = State.TopLevelContent;
1235
+ } else if (next = part.match(RE_WHITESPACE)) {
1236
+ token = WhitespaceInsideOpeningTag;
1237
+ state = State.InsideOpeningTagAfterWhitespace;
1238
+ } else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
1239
+ token = OpeningAngleBracket;
1240
+ state = State.AfterOpeningAngleBracket;
1241
+ } else {
1242
+ text.slice(index); // ?
1243
+ throw new UnexpectedTokenError();
1244
+ }
1245
+ break;
1246
+ case State.AfterAttributeEqualSign:
1247
+ if (next = part.match(RE_DOUBLE_QUOTE)) {
1248
+ token = AttributeQuoteStart;
1249
+ state = State.InsideAttributeAfterDoubleQuote;
1250
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1251
+ token = ClosingAngleBracket;
1252
+ state = State.TopLevelContent;
1253
+ } else {
1254
+ throw new UnexpectedTokenError();
1255
+ }
1256
+ break;
1257
+ case State.InsideAttributeAfterDoubleQuote:
1258
+ if (next = text.slice(index).match(RE_ATTRIBUTE_VALUE_INSIDE_DOUBLE_QUOTE)) {
1259
+ token = AttributeValue;
1260
+ state = State.AfterAttributeValueInsideDoubleQuote;
1261
+ } else if (next = part.match(RE_DOUBLE_QUOTE)) {
1262
+ token = AttributeQuoteEnd;
1263
+ state = State.AfterAttributeValueClosingQuote;
1264
+ } else {
1265
+ throw new UnexpectedTokenError();
1266
+ }
1267
+ break;
1268
+ case State.AfterAttributeValueInsideDoubleQuote:
1269
+ if (next = part.match(RE_DOUBLE_QUOTE)) {
1270
+ token = AttributeQuoteEnd;
1271
+ state = State.AfterAttributeValueClosingQuote;
1272
+ } else {
1273
+ throw new UnexpectedTokenError();
1274
+ }
1275
+ break;
1276
+ case State.AfterAttributeValueClosingQuote:
1277
+ if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1278
+ token = ClosingAngleBracket;
1279
+ state = State.TopLevelContent;
1280
+ } else if (next = part.match(RE_WHITESPACE)) {
1281
+ token = WhitespaceInsideOpeningTag;
1282
+ state = State.InsideOpeningTagAfterWhitespace;
1283
+ } else if (next = part.match(RE_SELF_CLOSING)) {
1284
+ token = ClosingAngleBracket;
1285
+ state = State.TopLevelContent;
1286
+ } else {
1287
+ throw new UnexpectedTokenError();
1288
+ }
1289
+ break;
1290
+ case State.AfterClosingTagSlash:
1291
+ if (next = part.match(RE_TAGNAME)) {
1292
+ token = TagNameEnd;
1293
+ state = State.AfterClosingTagName;
1294
+ } else if (next = part.match(RE_WHITESPACE)) {
1295
+ token = WhitespaceAfterClosingTagSlash;
1296
+ state = State.TopLevelContent;
1297
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1298
+ token = ClosingAngleBracket;
1299
+ state = State.TopLevelContent;
1300
+ } else {
1301
+ throw new UnexpectedTokenError();
1302
+ }
1303
+ break;
1304
+ case State.AfterClosingTagName:
1305
+ if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1306
+ token = ClosingAngleBracket;
1307
+ state = State.TopLevelContent;
1308
+ } else if (next = part.match(RE_WHITESPACE)) {
1309
+ token = Content;
1310
+ state = State.TopLevelContent;
1311
+ } else {
1312
+ throw new UnexpectedTokenError();
1313
+ }
1314
+ break;
1315
+ default:
1316
+ throw new UnexpectedTokenError();
1317
+ }
1318
+ const tokenText = next[0];
1319
+ tokens.push({
1320
+ type: token,
1321
+ text: tokenText
1322
+ });
1323
+ index += tokenText.length;
1324
+ }
1325
+ return tokens;
1326
+ };
1327
+
1328
+ const parseHtml = (html, allowedAttributes) => {
1329
+ string(html);
1330
+ array(allowedAttributes);
1331
+ const tokens = tokenizeHtml(html);
1332
+ const dom = [];
1333
+ const root = {
1334
+ type: 0,
1335
+ childCount: 0
1336
+ };
1337
+ let current = root;
1338
+ const stack = [root];
1339
+ let attributeName = '';
1340
+ for (const token of tokens) {
1341
+ switch (token.type) {
1342
+ case TagNameStart:
1343
+ current.childCount++;
1344
+ current = {
1345
+ type: getVirtualDomTag(token.text),
1346
+ childCount: 0
1347
+ };
1348
+ dom.push(current);
1349
+ if (!isSelfClosingTag(token.text)) {
1350
+ stack.push(current);
1351
+ }
1352
+ break;
1353
+ case TagNameEnd:
1354
+ stack.pop();
1355
+ current = stack.at(-1) || root;
1356
+ break;
1357
+ case Content:
1358
+ current.childCount++;
1359
+ dom.push(text(parseText(token.text)));
1360
+ break;
1361
+ case AttributeName:
1362
+ attributeName = token.text;
1363
+ if (attributeName === 'class') {
1364
+ attributeName = 'className';
1365
+ }
1366
+ break;
1367
+ case AttributeValue:
1368
+ if (allowedAttributes.includes(attributeName)) {
1369
+ current[attributeName] = token.text;
1370
+ }
1371
+ attributeName = '';
1372
+ break;
1373
+ }
1374
+ }
1375
+ return dom;
1376
+ };
1377
+
1378
+ const getMarkdownVirtualDom = html => {
1379
+ string(html);
1380
+ const dom = parseHtml(html, allowedMarkdownAttributes);
1381
+ return dom;
1382
+ };
1383
+
1384
+ const getVirtualDomChildCount = markdownDom => {
1385
+ const max = markdownDom.length - 1;
1386
+ let stack = [];
1387
+ for (let i = max; i >= 0; i--) {
1388
+ const element = markdownDom[i];
1389
+ if (element.childCount > 0) {
1390
+ stack = stack.slice(element.childCount);
1391
+ }
1392
+ stack.unshift(element);
1393
+ }
1394
+ return stack.length;
1395
+ };
1396
+
1397
+ const joinBySpace = (...items) => {
1398
+ return items.join(' ');
1399
+ };
1400
+
1401
+ const mergeClassNames = (...classNames) => {
1402
+ return joinBySpace(...classNames.filter(Boolean));
1403
+ };
1404
+
1405
+ const getExtensionDetailVirtualDom = (extensionDetail, sanitizedReadmeHtml) => {
1406
+ const markdownDom = getMarkdownVirtualDom(sanitizedReadmeHtml);
1407
+ const childCount = getVirtualDomChildCount(markdownDom);
1408
+ const dom = [{
1409
+ type: Div$1,
1410
+ className: mergeClassNames(Viewlet, ExtensionDetail),
1411
+ childCount: childCount + 1
1412
+ }, ...getExtensionDetailHeaderVirtualDom(extensionDetail), {
1413
+ type: Div$1,
1414
+ className: Markdown,
1415
+ role: Document,
1416
+ onContextMenu: HandleReadmeContextMenu,
1417
+ childCount
1418
+ }, ...markdownDom];
1419
+ return dom;
1420
+ };
1421
+
1422
+ const emptyObject = {};
1423
+ const RE_PLACEHOLDER = /\{(PH\d+)\}/g;
1424
+ const i18nString = (key, placeholders = emptyObject) => {
1425
+ if (placeholders === emptyObject) {
1426
+ return key;
1427
+ }
1428
+ const replacer = (match, rest) => {
1429
+ return placeholders[rest];
1430
+ };
1431
+ return key.replaceAll(RE_PLACEHOLDER, replacer);
1432
+ };
1433
+
1434
+ const UiStrings = {
1435
+ Copy: 'Copy',
1436
+ OpenInNewTab: 'Open in New Tab',
1437
+ OpenImageInNewTab: 'Open Image in New Tab',
1438
+ SaveImageAs: 'Save Image as'
1439
+ };
1440
+ const copy = () => {
1441
+ return i18nString(UiStrings.Copy);
1442
+ };
1443
+ const openInNewTab = () => {
1444
+ return i18nString(UiStrings.OpenInNewTab);
1445
+ };
1446
+ const openImageInNewTab = () => {
1447
+ return i18nString(UiStrings.OpenImageInNewTab);
1448
+ };
1449
+ const saveImageAs = () => {
1450
+ return i18nString(UiStrings.SaveImageAs);
1451
+ };
1452
+
1453
+ const None = 0;
1454
+
1455
+ const getCopyMenuEntry = () => ({
1456
+ id: 'copy',
1457
+ label: copy(),
1458
+ flags: None,
1459
+ command: 'ClipBoard.execCopy'
1460
+ });
1461
+
1462
+ const getImageMenuEntries = props => {
1463
+ if (!props.isImage) {
1464
+ return [];
1465
+ }
1466
+ return [{
1467
+ id: 'openImageInNewTab',
1468
+ label: openImageInNewTab(),
1469
+ flags: None,
1470
+ command: 'Open.openUrl',
1471
+ args: [props.url || '']
1472
+ }, {
1473
+ id: 'saveImageAs',
1474
+ label: saveImageAs(),
1475
+ flags: None,
1476
+ command: 'SaveFileAs.saveFileAs',
1477
+ args: ['image.png', props.url || '']
1478
+ }];
1479
+ };
1480
+
1481
+ const getLinkMenuEntries = props => {
1482
+ if (!props.isLink) {
1483
+ return [];
1484
+ }
1485
+ return [{
1486
+ id: 'openInNewTab',
1487
+ label: openInNewTab(),
1488
+ flags: None,
1489
+ command: 'Open.openUrl',
1490
+ args: [props.url || '']
1491
+ }];
1492
+ };
1493
+
1494
+ const getMenuEntries = props => [...getLinkMenuEntries(props), ...getImageMenuEntries(props), getCopyMenuEntry()];
1495
+
1496
+ const assetDir = '';
1497
+
1498
+ const ExtensionDefaultIcon = `${assetDir}/icons/extensionDefaultIcon.png`;
1499
+ const ExtensionLanguageBasics = `${assetDir}/icons/language-icon.svg`;
1500
+ const ExtensionTheme = `${assetDir}/icons/theme-icon.png`;
1501
+
1502
+ const handleIconError = state => {
1503
+ const {
1504
+ iconSrc
1505
+ } = state;
1506
+ if (iconSrc === ExtensionDefaultIcon) {
1507
+ return state;
1508
+ }
1509
+ return {
1510
+ ...state,
1511
+ iconSrc: ExtensionDefaultIcon
1512
+ };
1513
+ };
1514
+
1515
+ const Web = 1;
1516
+ const Electron = 2;
1517
+ const Remote = 3;
1518
+
1519
+ const isLanguageBasicsExtension = extension => {
1520
+ return extension.name && extension.name.startsWith('Language Basics');
1521
+ };
1522
+ const isThemeExtension = extension => {
1523
+ return extension.name && extension.name.endsWith(' Theme');
1524
+ };
1525
+ const getIcon = (extension, platform) => {
1526
+ if (!extension) {
1527
+ return ExtensionDefaultIcon;
1528
+ }
1529
+ if (!extension.path || !extension.icon) {
1530
+ if (isLanguageBasicsExtension(extension)) {
1531
+ return ExtensionLanguageBasics;
1532
+ }
1533
+ if (isThemeExtension(extension)) {
1534
+ return ExtensionTheme;
1535
+ }
1536
+ return ExtensionDefaultIcon;
1537
+ }
1538
+ if (platform === Remote || platform === Electron) {
1539
+ if (extension.builtin) {
1540
+ return `${assetDir}/extensions/${extension.id}/${extension.icon}`;
1541
+ }
1542
+ return `/remote/${extension.path}/${extension.icon}`; // TODO support windows paths
1543
+ }
1544
+ return '';
1545
+ };
1546
+
1547
+ // TODO handle case when extension is of type number|array|null|string
1548
+
1549
+ const getName = extension => {
1550
+ if (extension && extension.name) {
1551
+ return extension.name;
1552
+ }
1553
+ if (extension && extension.id) {
1554
+ return extension.id;
1555
+ }
1556
+ return 'n/a';
1557
+ };
1558
+ const getDescription = extension => {
1559
+ if (!extension || !extension.description) {
1560
+ return 'n/a';
1561
+ }
1562
+ return extension.description;
1563
+ };
1564
+
1565
+ const state = {
1566
+ rpc: undefined
1567
+ };
1568
+ const invoke = (method, ...params) => {
1569
+ const rpc = state.rpc;
1570
+ // @ts-ignore
1571
+ return rpc.invoke(method, ...params);
1572
+ };
1573
+ const setRpc = rpc => {
1574
+ state.rpc = rpc;
1575
+ };
1576
+
1577
+ const getAllExtensions = async platform => {
1578
+ if (platform === Web) {
1579
+ return [];
1580
+ }
1581
+ return invoke('ExtensionManagement.getAllExtensions');
1582
+ };
1583
+ const getExtension = async (id, platform) => {
1584
+ const allExtensions = await getAllExtensions(platform);
1585
+ for (const extension of allExtensions) {
1586
+ if (extension.id === id) {
1587
+ return extension;
1588
+ }
1589
+ }
1590
+ return undefined;
1591
+ };
1592
+
1593
+ const getRemoteSrc = uri => {
1594
+ const src = `/remote${uri}`;
1595
+ return src;
1596
+ };
1597
+
1598
+ const getBaseUrl = (extensionPath, platform) => {
1599
+ switch (platform) {
1600
+ case Remote:
1601
+ case Electron:
1602
+ return getRemoteSrc(extensionPath + '/');
1603
+ default:
1604
+ return extensionPath;
1605
+ }
1606
+ };
1607
+
1608
+ const Small = 1;
1609
+ const Normal = 2;
1610
+ const Large = 3;
1611
+
1612
+ const getViewletSize = width => {
1613
+ if (width < 180) {
1614
+ return Small;
1615
+ }
1616
+ if (width < 768) {
1617
+ return Normal;
1618
+ }
1619
+ return Large;
1620
+ };
1621
+
1622
+ const readFile = async uri => {
1623
+ return invoke('FileSystem.readFile', uri);
1624
+ };
1625
+
1626
+ const ENOENT = 'ENOENT';
1627
+
1628
+ const isEnoentError = error => {
1629
+ return error && error.code === ENOENT;
1630
+ };
1631
+
1632
+ const join = (pathSeparator, ...parts) => {
1633
+ return parts.join(pathSeparator);
1634
+ };
1635
+
1636
+ const loadReadmeContent = async path => {
1637
+ try {
1638
+ const readmeUrl = join('/', path, 'README.md');
1639
+ const readmeContent = await readFile(readmeUrl);
1640
+ return readmeContent;
1641
+ } catch (error) {
1642
+ if (isEnoentError(error)) {
1643
+ return '';
1644
+ }
1645
+ console.error(error);
1646
+ return `${error}`;
1647
+ }
1648
+ };
1649
+
802
1650
  /**
803
1651
  * marked v15.0.3 - a markdown parser
804
1652
  * Copyright (c) 2011-2024, Christopher Jeffrey. (MIT Licensed)
@@ -3274,19 +4122,53 @@ marked.Tokenizer = _Tokenizer;
3274
4122
  marked.Hooks = _Hooks;
3275
4123
  marked.parse = marked;
3276
4124
 
3277
- const renderMarkdown = async markdown => {
4125
+ const renderMarkdown = async (markdown, options = {}) => {
3278
4126
  const html = await marked(markdown, {});
3279
4127
  return html;
3280
4128
  };
3281
4129
 
4130
+ const loadContent = async (state, platform) => {
4131
+ const {
4132
+ uri,
4133
+ width
4134
+ } = state;
4135
+ const id = uri.slice('extension-detail://'.length);
4136
+ const extension = await getExtension(id, platform);
4137
+ const readmeContent = await loadReadmeContent(extension.path);
4138
+ // @ts-ignore
4139
+ const baseUrl = getBaseUrl(extension.path, platform);
4140
+ const readmeHtml = await renderMarkdown(readmeContent, {
4141
+ baseUrl
4142
+ });
4143
+ const sanitizedReadmeHtml = readmeHtml;
4144
+ const normalizedReadmeHtml = sanitizedReadmeHtml;
4145
+ const iconSrc = getIcon(extension, platform);
4146
+ const description = getDescription(extension);
4147
+ const name = getName(extension);
4148
+ const size = getViewletSize(width);
4149
+ return {
4150
+ ...state,
4151
+ sanitizedReadmeHtml: normalizedReadmeHtml,
4152
+ iconSrc,
4153
+ name,
4154
+ description,
4155
+ size
4156
+ };
4157
+ };
4158
+
3282
4159
  const commandMap = {
3283
- 'RenderMarkdown.renderMarkdown': renderMarkdown
4160
+ 'RenderMarkdown.renderMarkdown': renderMarkdown,
4161
+ 'HandleIconError.handleIconError': handleIconError,
4162
+ 'ExtensionDetail.loadContent': loadContent,
4163
+ 'ExtensionDetail.getMenuEntries': getMenuEntries,
4164
+ 'ExtensionDetail.getVirtualDom': getExtensionDetailVirtualDom
3284
4165
  };
3285
4166
 
3286
4167
  const listen = async () => {
3287
- await WebWorkerRpcClient.create({
4168
+ const rpc = await WebWorkerRpcClient.create({
3288
4169
  commandMap: commandMap
3289
4170
  });
4171
+ setRpc(rpc);
3290
4172
  };
3291
4173
 
3292
4174
  const main = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/extension-detail-view",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "Extension Detail View Worker",
5
5
  "main": "dist/extensionDetailViewWorkerMain.js",
6
6
  "type": "module",