@native-sdk/cli 0.10.0 → 0.11.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.
- package/README.md +8 -6
- package/bin/native.js +2 -1
- package/build/app.zig +320 -60
- package/build/corewire.zig +53 -0
- package/build/test_shards.zig +49 -0
- package/build/ts_run.mjs +3 -3
- package/build.zig +1818 -87
- package/package.json +19 -14
- package/packages/core/compile-surface/core.ts +134 -72
- package/packages/core/package-lock.json +75 -357
- package/packages/core/package.json +25 -4
- package/packages/core/scripts/compiler_command.mjs +17 -12
- package/packages/core/scripts/compiler_typecheck.mjs +1 -0
- package/packages/core/scripts/gen_declarations.mjs +3 -2
- package/packages/core/scripts/gen_service_surface.mjs +22 -10
- package/packages/core/scripts/install_scriptc.mjs +182 -0
- package/packages/core/scripts/invoke_scriptc.mjs +18 -0
- package/packages/core/scripts/merge_static_archives.mjs +6 -1
- package/packages/core/scripts/npm_static_spike.mjs +2 -2
- package/packages/core/scripts/run_external_core_compiler.mjs +22 -10
- package/packages/core/scripts/run_external_service_compiler.mjs +15 -15
- package/packages/core/scripts/run_library_compiler.mjs +51 -0
- package/packages/core/scripts/run_native_tests.mjs +22 -0
- package/packages/core/scripts/scriptc-toolchain.json +101 -0
- package/packages/core/scripts/scriptc_toolchain.mjs +121 -0
- package/packages/core/scripts/stage_external_core.mjs +57 -1
- package/packages/core/scripts/surface_manifest_diff.mjs +19 -12
- package/packages/core/sdk/bytes.d.ts +2 -0
- package/packages/core/sdk/bytes.ts +86 -0
- package/packages/core/sdk/core.d.ts +308 -4
- package/packages/core/sdk/core.ts +320 -84
- package/packages/core/sdk/events.d.ts +247 -0
- package/packages/core/sdk/events.ts +230 -2
- package/packages/core/sdk/internal/image_identity.d.ts +7 -0
- package/packages/core/sdk/internal/image_identity.ts +71 -0
- package/packages/core/sdk/internal/markdown_policy.d.ts +5 -0
- package/packages/core/sdk/internal/markdown_policy.ts +878 -0
- package/packages/core/sdk/text.d.ts +27 -0
- package/packages/core/sdk/text.ts +206 -15
- package/packages/core/sdk/theme.d.ts +251 -0
- package/packages/core/sdk/theme.ts +267 -0
- package/packages/core/src/authorization.ts +137 -0
- package/packages/core/src/automation_commands.ts +201 -0
- package/packages/core/src/automation_interactions.ts +92 -0
- package/packages/core/src/bridge_descriptors.ts +104 -0
- package/packages/core/src/builtin_coordination.ts +172 -0
- package/packages/core/src/canvas_frame_coordination.ts +81 -0
- package/packages/core/src/canvas_presentation_lifecycle.ts +84 -0
- package/packages/core/src/canvas_scene_observation.ts +145 -0
- package/packages/core/src/chart_content.ts +161 -0
- package/packages/core/src/chart_plans.ts +432 -0
- package/packages/core/src/checker.ts +166 -19
- package/packages/core/src/code_content.ts +580 -0
- package/packages/core/src/command_coordination.ts +140 -0
- package/packages/core/src/component_composition.ts +94 -0
- package/packages/core/src/component_construction.ts +149 -0
- package/packages/core/src/contract.ts +25 -7
- package/packages/core/src/control_appearance.ts +232 -0
- package/packages/core/src/control_commands.ts +115 -0
- package/packages/core/src/control_content.ts +105 -0
- package/packages/core/src/control_geometry.ts +107 -0
- package/packages/core/src/control_payloads.ts +119 -0
- package/packages/core/src/control_primitives.ts +134 -0
- package/packages/core/src/devhost.ts +82 -7
- package/packages/core/src/devhost_journal.mjs +30 -7
- package/packages/core/src/effect_plans.ts +71 -0
- package/packages/core/src/flow_measurement.ts +121 -0
- package/packages/core/src/font_registry.ts +37 -0
- package/packages/core/src/glyph_atlas.ts +86 -0
- package/packages/core/src/gpu_packet_preparation.ts +130 -0
- package/packages/core/src/gpu_planning.ts +123 -0
- package/packages/core/src/gpu_presentation.ts +55 -0
- package/packages/core/src/host_flow.ts +89 -0
- package/packages/core/src/host_input_regions.ts +165 -0
- package/packages/core/src/image_registry.ts +66 -0
- package/packages/core/src/indicator_plans.ts +132 -0
- package/packages/core/src/infer.ts +150 -8
- package/packages/core/src/input_composition.ts +116 -0
- package/packages/core/src/intrinsic_measure.ts +100 -0
- package/packages/core/src/layout_coordination.ts +207 -0
- package/packages/core/src/leaf_plans.ts +134 -0
- package/packages/core/src/markup_development.ts +160 -0
- package/packages/core/src/measurement_coordination.ts +185 -0
- package/packages/core/src/modules.ts +1 -1
- package/packages/core/src/motion_sampling.ts +100 -0
- package/packages/core/src/paragraph_layout.ts +192 -0
- package/packages/core/src/portable_sources.json +74 -0
- package/packages/core/src/portable_sources.ts +17 -0
- package/packages/core/src/registered_font.ts +93 -0
- package/packages/core/src/render_animation_lifecycle.ts +128 -0
- package/packages/core/src/render_coordination.ts +170 -0
- package/packages/core/src/render_resources.ts +234 -0
- package/packages/core/src/runtime_policy.ts +3930 -0
- package/packages/core/src/scalar_text.ts +84 -0
- package/packages/core/src/service_contract.ts +2 -1
- package/packages/core/src/session_codecs.ts +309 -0
- package/packages/core/src/shell_admission.ts +126 -0
- package/packages/core/src/shell_coordination.ts +93 -0
- package/packages/core/src/sqlite_cli.ts +11 -0
- package/packages/core/src/status_validation.ts +232 -0
- package/packages/core/src/stream_policy.ts +63 -0
- package/packages/core/src/surface_recipes.ts +136 -0
- package/packages/core/src/system_admission.ts +60 -0
- package/packages/core/src/terminal_paint.ts +436 -0
- package/packages/core/src/text_document_width.ts +81 -0
- package/packages/core/src/text_measurement_cache.ts +69 -0
- package/packages/core/src/text_query_layout.ts +179 -0
- package/packages/core/src/text_run_layout.ts +410 -0
- package/packages/core/src/text_span_queries.ts +355 -0
- package/packages/core/src/typed_ast.ts +18 -1
- package/packages/core/src/types.ts +7 -5
- package/packages/core/src/vector_effects.ts +98 -0
- package/packages/core/src/vector_raster.ts +243 -0
- package/packages/core/src/view_components.ts +2415 -0
- package/packages/core/src/view_coordination.ts +198 -0
- package/packages/core/src/view_frontend.ts +980 -0
- package/packages/core/src/widget_audits.ts +202 -0
- package/packages/core/src/widget_changes.ts +258 -0
- package/packages/core/src/widget_metrics.ts +139 -0
- package/packages/core/src/widget_motion.ts +171 -0
- package/packages/core/src/widget_paint.ts +272 -0
- package/packages/core/src/widget_paint_walk.ts +203 -0
- package/packages/core/src/widget_presentation.ts +141 -0
- package/packages/core/src/widget_routing.ts +262 -0
- package/packages/core/src/window_lifecycle.ts +92 -0
- package/packages/core/testing/index.ts +430 -0
- package/schemas/app.schema.json +13 -0
- package/skill-data/core/SKILL.md +2 -2
- package/skill-data/core/references/web-engines-packaging-debugging.md +2 -1
- package/skill-data/native-ui/SKILL.md +76 -17
- package/skill-data/ts-core/SKILL.md +113 -9
- package/skill-data/ts-services/SKILL.md +1 -1
- package/skill-data/ts-services/references/service-surface.md +264 -144
- package/src/app_runner/root.zig +25 -12
- package/src/app_runner/ts_compiled_view.zig +1631 -0
- package/src/app_runner/ts_core_main.zig +143 -10
- package/src/app_runner/ts_core_mobile.zig +24 -12
- package/src/automation/server.zig +1 -3
- package/src/automation/snapshot.zig +9 -637
- package/src/automation/snapshot_reference.zig +656 -0
- package/src/automation/snapshot_serialization.zig +566 -0
- package/src/platform/linux/gtk_host.c +36 -24
- package/src/platform/linux/gtk_pixels.h +118 -0
- package/src/platform/linux/gtk_pixels_test.c +241 -0
- package/src/platform/linux/root.zig +2 -0
- package/src/platform/macos/appkit_host.m +188 -30
- package/src/platform/macos/pointer_motion_test.m +96 -0
- package/src/platform/macos/scroll_driver_test.m +105 -0
- package/src/platform/macos/spectrum_bins.h +34 -0
- package/src/platform/macos/spectrum_bins_test.c +56 -0
- package/src/platform/macos/text_keyboard_test.m +80 -0
- package/src/platform/macos/widget_focus_test.m +66 -0
- package/src/platform/null_platform.zig +26 -2
- package/src/platform/null_platform_tests.zig +13 -0
- package/src/platform/root.zig +1 -0
- package/src/platform/types.zig +13 -0
- package/src/platform/windows/root.zig +2 -0
- package/src/platform/windows/webview2_host.cpp +7 -2
- package/src/primitives/app_manifest/comptime_scene.zig +1 -1
- package/src/primitives/app_manifest/root.zig +2 -0
- package/src/primitives/app_manifest/tests.zig +4 -1
- package/src/primitives/app_manifest/validation.zig +2 -0
- package/src/primitives/canvas/a11y_audit.zig +3 -1
- package/src/primitives/canvas/canvas_frame_coordination_policy.zig +134 -0
- package/src/primitives/canvas/chart_content_policy.zig +75 -0
- package/src/primitives/canvas/chart_plan_policy.zig +196 -0
- package/src/primitives/canvas/code_content_policy.zig +162 -0
- package/src/primitives/canvas/commands.zig +29 -1
- package/src/primitives/canvas/component_composition.zig +125 -0
- package/src/primitives/canvas/component_composition_policy.zig +44 -0
- package/src/primitives/canvas/component_construction_policy.zig +187 -0
- package/src/primitives/canvas/container_layout_policy.zig +87 -0
- package/src/primitives/canvas/control_appearance_policy.zig +142 -0
- package/src/primitives/canvas/control_command_policy.zig +75 -0
- package/src/primitives/canvas/control_content_policy.zig +90 -0
- package/src/primitives/canvas/control_geometry_policy.zig +165 -0
- package/src/primitives/canvas/control_payload_policy.zig +48 -0
- package/src/primitives/canvas/control_primitive_policy.zig +129 -0
- package/src/primitives/canvas/effect_plan_policy.zig +96 -0
- package/src/primitives/canvas/events.zig +1027 -1
- package/src/primitives/canvas/flow_measurement_policy.zig +237 -0
- package/src/primitives/canvas/frame.zig +92 -23
- package/src/primitives/canvas/glyph_atlas_policy.zig +40 -0
- package/src/primitives/canvas/gpu.zig +10 -0
- package/src/primitives/canvas/gpu_packet_preparation_policy.zig +325 -0
- package/src/primitives/canvas/gpu_planning_policy.zig +364 -0
- package/src/primitives/canvas/gpu_presentation_policy.zig +74 -0
- package/src/primitives/canvas/grid_layout_policy.zig +51 -0
- package/src/primitives/canvas/indicator_plan_policy.zig +128 -0
- package/src/primitives/canvas/intrinsic_layout_policy.zig +87 -0
- package/src/primitives/canvas/intrinsic_measure_policy.zig +128 -0
- package/src/primitives/canvas/layout_audit.zig +5 -2
- package/src/primitives/canvas/layout_coordination_policy.zig +262 -0
- package/src/primitives/canvas/leaf_plan_policy.zig +117 -0
- package/src/primitives/canvas/markdown.zig +13 -3
- package/src/primitives/canvas/markdown_content_policy.zig +227 -0
- package/src/primitives/canvas/markdown_image_binding.zig +81 -0
- package/src/primitives/canvas/measurement_coordination_policy.zig +288 -0
- package/src/primitives/canvas/numeric_capabilities.zig +14 -0
- package/src/primitives/canvas/reference.zig +12 -6
- package/src/primitives/canvas/registered_font_policy.zig +152 -0
- package/src/primitives/canvas/render.zig +48 -0
- package/src/primitives/canvas/render_cache_policy.zig +184 -0
- package/src/primitives/canvas/render_coordination_policy.zig +163 -0
- package/src/primitives/canvas/render_damage_policy.zig +160 -0
- package/src/primitives/canvas/render_effects.zig +14 -0
- package/src/primitives/canvas/render_generic_resources.zig +14 -0
- package/src/primitives/canvas/render_images.zig +14 -0
- package/src/primitives/canvas/render_layers.zig +14 -0
- package/src/primitives/canvas/render_override_policy.zig +119 -0
- package/src/primitives/canvas/render_paths.zig +14 -0
- package/src/primitives/canvas/render_plan_policy.zig +199 -0
- package/src/primitives/canvas/render_resource_policy.zig +476 -0
- package/src/primitives/canvas/root.zig +100 -0
- package/src/primitives/canvas/semantic_tree_policy.zig +158 -0
- package/src/primitives/canvas/serialization.zig +8 -1
- package/src/primitives/canvas/surface_layout_policy.zig +36 -0
- package/src/primitives/canvas/surface_recipe_policy.zig +231 -0
- package/src/primitives/canvas/terminal_grid.zig +10 -696
- package/src/primitives/canvas/terminal_grid_reference.zig +709 -0
- package/src/primitives/canvas/terminal_paint_policy.zig +191 -0
- package/src/primitives/canvas/text.zig +1 -0
- package/src/primitives/canvas/text_atlas.zig +113 -0
- package/src/primitives/canvas/text_cache_policy.zig +94 -0
- package/src/primitives/canvas/text_document_policy.zig +120 -0
- package/src/primitives/canvas/text_layout.zig +51 -2
- package/src/primitives/canvas/text_layout_cache.zig +29 -0
- package/src/primitives/canvas/text_layout_hash.zig +1 -0
- package/src/primitives/canvas/text_layout_types.zig +3 -0
- package/src/primitives/canvas/text_measure_cache.zig +46 -0
- package/src/primitives/canvas/text_measurement_policy.zig +124 -0
- package/src/primitives/canvas/text_metrics.zig +23 -0
- package/src/primitives/canvas/text_query_policy.zig +156 -0
- package/src/primitives/canvas/text_run_policy.zig +252 -0
- package/src/primitives/canvas/text_scalar_policy.zig +156 -0
- package/src/primitives/canvas/text_span_query_policy.zig +232 -0
- package/src/primitives/canvas/text_spans.zig +280 -8
- package/src/primitives/canvas/text_tests.zig +10 -0
- package/src/primitives/canvas/tokens.zig +38 -0
- package/src/primitives/canvas/ui.zig +250 -83
- package/src/primitives/canvas/ui_markup.zig +139 -33
- package/src/primitives/canvas/ui_markup_compiled.zig +319 -59
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +225 -0
- package/src/primitives/canvas/ui_markup_contract.zig +146 -14
- package/src/primitives/canvas/ui_markup_contract_tests.zig +77 -0
- package/src/primitives/canvas/ui_markup_reflect.zig +56 -0
- package/src/primitives/canvas/ui_markup_tests.zig +116 -0
- package/src/primitives/canvas/ui_markup_view.zig +258 -32
- package/src/primitives/canvas/ui_markup_view_tests.zig +142 -10
- package/src/primitives/canvas/ui_schema.zig +13 -1
- package/src/primitives/canvas/ui_schema_tests.zig +11 -7
- package/src/primitives/canvas/ui_tests.zig +1 -0
- package/src/primitives/canvas/vector.zig +36 -2
- package/src/primitives/canvas/vector_effect_policy.zig +236 -0
- package/src/primitives/canvas/vector_raster_policy.zig +190 -0
- package/src/primitives/canvas/virtual_extent_policy.zig +292 -0
- package/src/primitives/canvas/virtual_extents.zig +99 -3
- package/src/primitives/canvas/virtual_flow_policy.zig +108 -0
- package/src/primitives/canvas/widget_audit_policy.zig +293 -0
- package/src/primitives/canvas/widget_change_policy.zig +298 -0
- package/src/primitives/canvas/widget_invalidation.zig +113 -2
- package/src/primitives/canvas/widget_layout.zig +673 -128
- package/src/primitives/canvas/widget_layout_tests.zig +4 -0
- package/src/primitives/canvas/widget_metric_policy.zig +126 -0
- package/src/primitives/canvas/widget_metrics.zig +45 -6
- package/src/primitives/canvas/widget_paint_policy.zig +166 -0
- package/src/primitives/canvas/widget_paint_walk_policy.zig +252 -0
- package/src/primitives/canvas/widget_presentation_policy.zig +168 -0
- package/src/primitives/canvas/widget_render.zig +744 -74
- package/src/primitives/canvas/widget_render_controls.zig +1217 -133
- package/src/primitives/canvas/widget_render_scroll.zig +11 -3
- package/src/primitives/canvas/widget_render_style.zig +94 -0
- package/src/primitives/canvas/widget_render_surfaces.zig +61 -50
- package/src/primitives/canvas/widget_routing.zig +75 -0
- package/src/primitives/canvas/widget_routing_policy.zig +135 -0
- package/src/primitives/canvas/widget_runtime.zig +20 -1
- package/src/primitives/canvas/widget_semantics.zig +25 -0
- package/src/primitives/canvas/widget_text_input.zig +86 -23
- package/src/primitives/canvas/widget_text_select.zig +1 -0
- package/src/primitives/canvas/widget_tree.zig +8 -1
- package/src/primitives/canvas/widgets.zig +42 -12
- package/src/primitives/canvas/wrapped_layout_policy.zig +104 -0
- package/src/root.zig +21 -0
- package/src/runtime/accessibility_policy.zig +122 -0
- package/src/runtime/api.zig +28 -0
- package/src/runtime/app_dispatch_policy.zig +68 -0
- package/src/runtime/authorization_policy.zig +150 -0
- package/src/runtime/automation_command_policy.zig +241 -0
- package/src/runtime/automation_interaction_policy.zig +107 -0
- package/src/runtime/automation_snapshot.zig +52 -22
- package/src/runtime/automation_snapshot_reference.zig +290 -0
- package/src/runtime/automation_widget_dispatch.zig +267 -355
- package/src/runtime/automation_widget_dispatch_reference.zig +900 -0
- package/src/runtime/blob_policy.zig +58 -0
- package/src/runtime/blob_reference.zig +64 -0
- package/src/runtime/bridge_descriptor_policy.zig +206 -0
- package/src/runtime/builtin_bridge.zig +180 -44
- package/src/runtime/builtin_coordination_policy.zig +121 -0
- package/src/runtime/callback_policy_test_reference.zig +97 -0
- package/src/runtime/canvas_drag_policy.zig +94 -0
- package/src/runtime/canvas_fonts.zig +38 -10
- package/src/runtime/canvas_frame.zig +590 -134
- package/src/runtime/canvas_frame_helpers.zig +1 -0
- package/src/runtime/canvas_hover_policy.zig +131 -0
- package/src/runtime/canvas_images.zig +59 -30
- package/src/runtime/canvas_keyboard_focus_policy.zig +154 -0
- package/src/runtime/canvas_limits.zig +1 -1
- package/src/runtime/canvas_presentation_lifecycle_policy.zig +86 -0
- package/src/runtime/canvas_scene_observation_policy.zig +181 -0
- package/src/runtime/canvas_widget_context_menu.zig +4 -4
- package/src/runtime/canvas_widget_display.zig +110 -9
- package/src/runtime/canvas_widget_events.zig +718 -328
- package/src/runtime/canvas_widget_runtime.zig +437 -81
- package/src/runtime/canvas_widget_scroll_drivers.zig +39 -1
- package/src/runtime/canvas_widget_scroll_tests.zig +31 -0
- package/src/runtime/canvas_widget_state.zig +93 -56
- package/src/runtime/channel_sources.zig +80 -0
- package/src/runtime/command_policy.zig +161 -0
- package/src/runtime/component_policy.zig +83 -0
- package/src/runtime/core.zig +156 -6
- package/src/runtime/desktop_files.zig +189 -0
- package/src/runtime/effects.zig +466 -29
- package/src/runtime/effects_tests.zig +28 -0
- package/src/runtime/file_policy_test_reference.zig +113 -0
- package/src/runtime/flow.zig +418 -341
- package/src/runtime/flow_reference.zig +1426 -0
- package/src/runtime/font_registry_policy.zig +72 -0
- package/src/runtime/frame_profile.zig +24 -33
- package/src/runtime/gpu_surface_events.zig +97 -35
- package/src/runtime/host_event_pump.zig +303 -0
- package/src/runtime/host_event_pump_reference.zig +268 -0
- package/src/runtime/host_flow_policy.zig +155 -0
- package/src/runtime/host_input_regions_policy.zig +156 -0
- package/src/runtime/image_registry_policy.zig +94 -0
- package/src/runtime/input_composition_policy.zig +138 -0
- package/src/runtime/journal_policy.zig +66 -0
- package/src/runtime/journal_reference.zig +53 -0
- package/src/runtime/lifecycle_policy_test_reference.zig +97 -0
- package/src/runtime/markup_development_policy.zig +91 -0
- package/src/runtime/media_surface.zig +40 -1
- package/src/runtime/motion_sampling_policy.zig +159 -0
- package/src/runtime/recording_policy.zig +42 -0
- package/src/runtime/render_animation_policy.zig +206 -0
- package/src/runtime/replay_policy.zig +66 -0
- package/src/runtime/replay_policy_reference.zig +326 -0
- package/src/runtime/replay_progress_policy.zig +64 -0
- package/src/runtime/replay_progress_reference.zig +34 -0
- package/src/runtime/request_policy_test_reference.zig +188 -0
- package/src/runtime/root.zig +13 -0
- package/src/runtime/session_blobs.zig +157 -58
- package/src/runtime/session_blobs_reference.zig +692 -0
- package/src/runtime/session_codec.zig +361 -0
- package/src/runtime/session_codec_reference.zig +1027 -0
- package/src/runtime/session_journal.zig +246 -983
- package/src/runtime/session_record.zig +224 -45
- package/src/runtime/session_replay.zig +157 -415
- package/src/runtime/session_tests.zig +552 -0
- package/src/runtime/shell_admission_policy.zig +225 -0
- package/src/runtime/shell_coordination_policy.zig +138 -0
- package/src/runtime/shell_layout.zig +126 -1
- package/src/runtime/snapshot_policy.zig +60 -0
- package/src/runtime/snapshot_reference.zig +64 -0
- package/src/runtime/state.zig +16 -0
- package/src/runtime/status_validation.zig +190 -0
- package/src/runtime/system_admission_policy.zig +140 -0
- package/src/runtime/system_services.zig +46 -36
- package/src/runtime/tests.zig +1 -0
- package/src/runtime/ts_canvas_hooks.zig +261 -0
- package/src/runtime/ts_canvas_hooks_tests.zig +279 -0
- package/src/runtime/ts_canvas_icons.zig +234 -0
- package/src/runtime/ts_core_host.zig +2325 -411
- package/src/runtime/ts_core_host_tests.zig +1982 -0
- package/src/runtime/ts_test_host.zig +699 -0
- package/src/runtime/ts_ui_app.zig +1087 -45
- package/src/runtime/ui_app.zig +1117 -629
- package/src/runtime/ui_app_provenance.zig +147 -5
- package/src/runtime/ui_app_provenance_reference.zig +370 -0
- package/src/runtime/ui_app_tests.zig +619 -9
- package/src/runtime/ui_app_window_tests.zig +89 -0
- package/src/runtime/view.zig +25 -0
- package/src/runtime/view_canvas.zig +30 -6
- package/src/runtime/view_coordination_policy.zig +147 -0
- package/src/runtime/view_widget_control.zig +51 -14
- package/src/runtime/view_widget_scroll.zig +16 -5
- package/src/runtime/view_widget_text.zig +153 -68
- package/src/runtime/view_widget_tree.zig +460 -56
- package/src/runtime/webview_view_bridge_tests.zig +2 -0
- package/src/runtime/widget_motion_policy.zig +238 -0
- package/src/runtime/window_command_bridge_tests.zig +63 -0
- package/src/runtime/window_lifecycle_policy.zig +127 -0
- package/src/runtime/window_storage.zig +74 -8
- package/src/runtime/window_view_runtime.zig +148 -33
- package/src/runtime/window_views.zig +163 -10
- package/src/tooling/codesign.zig +125 -74
- package/src/tooling/components/identity_tests.zig +62 -36
- package/src/tooling/components/stepper.native +24 -0
- package/src/tooling/components/stepper.zig +0 -19
- package/src/tooling/components/timeline-item.native +22 -0
- package/src/tooling/components/timeline.native +5 -3
- package/src/tooling/components/timeline_item.zig +0 -32
- package/src/tooling/eject_components.zig +76 -8
- package/src/tooling/ios.zig +66 -12
- package/src/tooling/manifest.zig +35 -0
- package/src/tooling/package.zig +85 -1
- package/src/tooling/raw_manifest.zig +6 -0
- package/src/tooling/templates.zig +15 -3
- package/src/tooling/ts_core.zig +13 -3
- package/src/tooling/verbs.zig +22 -0
- package/tools/corewire/build_profile.mjs +54 -0
- package/tools/corewire/contract_intake.ts +303 -0
- package/tools/corewire/contract_intake.zig +82 -0
- package/tools/corewire/core.test.ts +166 -0
- package/tools/corewire/core_abi.zig +51 -0
- package/tools/corewire/core_cases.ts +233 -0
- package/tools/corewire/core_contract.ts +407 -0
- package/tools/corewire/core_emission.ts +136 -0
- package/tools/corewire/core_emission.zig +69 -0
- package/tools/corewire/core_goldens.json +3110 -0
- package/tools/corewire/core_policy.ts +14 -0
- package/tools/corewire/core_policy.zig +48 -0
- package/tools/corewire/core_projection.ts +360 -0
- package/tools/corewire/core_vocabulary.ts +6 -0
- package/tools/corewire/emit.zig +19 -1590
- package/tools/corewire/emit_facade.ts +714 -0
- package/tools/corewire/emit_facade.zig +10 -3752
- package/tools/corewire/emit_mirror.ts +290 -0
- package/tools/corewire/emit_profile.ts +50 -0
- package/tools/corewire/emit_service.ts +331 -0
- package/tools/corewire/emit_service.zig +23 -842
- package/tools/corewire/entry.test.ts +95 -0
- package/tools/corewire/entry_coordination.ts +127 -0
- package/tools/corewire/entry_coordination.zig +190 -0
- package/tools/corewire/facade_templates.ts +85 -0
- package/tools/corewire/filesystem.test.ts +166 -0
- package/tools/corewire/filesystem_coordination.ts +213 -0
- package/tools/corewire/filesystem_coordination.zig +245 -0
- package/tools/corewire/intake.test.ts +35 -0
- package/tools/corewire/intake_cases.ts +55 -0
- package/tools/corewire/intake_goldens.json +12237 -0
- package/tools/corewire/invocation.test.ts +24 -0
- package/tools/corewire/invocation.ts +175 -0
- package/tools/corewire/invocation.zig +136 -0
- package/tools/corewire/invocation_cases.ts +60 -0
- package/tools/corewire/invocation_fixture.ts +33 -0
- package/tools/corewire/invocation_goldens.json +1143 -0
- package/tools/corewire/invocation_tests.zig +111 -0
- package/tools/corewire/invocation_usage.ts +2 -0
- package/tools/corewire/lossless_projection.ts +96 -0
- package/tools/corewire/main.zig +7 -680
- package/tools/corewire/main_reference.zig +415 -0
- package/tools/corewire/mirror_templates.ts +55 -0
- package/tools/corewire/profile.test.ts +248 -0
- package/tools/corewire/profile.zig +56 -0
- package/tools/corewire/profile_emission.ts +202 -0
- package/tools/corewire/profile_library.json +29 -0
- package/tools/corewire/profile_tests.zig +136 -0
- package/tools/corewire/service.test.ts +75 -0
- package/tools/corewire/service_cases.ts +61 -0
- package/tools/corewire/service_contract.zig +40 -146
- package/tools/corewire/service_goldens.json +650 -0
- package/tools/corewire/service_projection.zig +52 -0
- package/tools/corewire/service_templates.ts +14 -0
- package/tools/corewire/shim_rt.zig +9 -0
- package/tools/corewire/sidecar.zig +267 -1438
- package/tools/corewire/emit_profile.zig +0 -521
|
@@ -0,0 +1,878 @@
|
|
|
1
|
+
// Portable Markdown presentation. Sources and payloads remain raw bytes;
|
|
2
|
+
// native owns the copied recipe, typed routes, registered images and drawing.
|
|
3
|
+
interface NscMdLink { text: Uint8Array; target: Uint8Array; consumed: number }
|
|
4
|
+
interface NscMdTag { name: Uint8Array; attributes: Uint8Array; closing: boolean; selfClosing: boolean; consumed: number; kind: number; level: number }
|
|
5
|
+
interface NscMdSlot { valid: boolean; from: number; pos: number }
|
|
6
|
+
function nscMdSlot(): NscMdSlot { return { valid: false, from: 0, pos: -1 }; }
|
|
7
|
+
class NscMdCache {
|
|
8
|
+
bracket = nscMdSlot(); paren = nscMdSlot(); angle = nscMdSlot(); space = nscMdSlot(); title = nscMdSlot(); scheme = nscMdSlot(); comment = nscMdSlot(); tick = nscMdSlot();
|
|
9
|
+
}
|
|
10
|
+
function nscMdBytes(s: string): Uint8Array {
|
|
11
|
+
let byteLength = 0;
|
|
12
|
+
for (let i = 0; i < s.length; i++) {
|
|
13
|
+
const code = s.charCodeAt(i);
|
|
14
|
+
if (code <= 0x7f) {
|
|
15
|
+
byteLength += 1;
|
|
16
|
+
} else if (code <= 0x7ff) {
|
|
17
|
+
byteLength += 2;
|
|
18
|
+
} else if (code >= 0xd800 && code <= 0xdbff && i + 1 < s.length) {
|
|
19
|
+
const next = s.charCodeAt(i + 1);
|
|
20
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
21
|
+
byteLength += 4;
|
|
22
|
+
i += 1;
|
|
23
|
+
} else {
|
|
24
|
+
byteLength += 3;
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
byteLength += 3;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const out = new Uint8Array(byteLength);
|
|
32
|
+
let at = 0;
|
|
33
|
+
for (let i = 0; i < s.length; i++) {
|
|
34
|
+
let code = s.charCodeAt(i);
|
|
35
|
+
if (code >= 0xd800 && code <= 0xdbff && i + 1 < s.length) {
|
|
36
|
+
const next = s.charCodeAt(i + 1);
|
|
37
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
38
|
+
code = 0x10000 + ((code - 0xd800) << 10) + (next - 0xdc00);
|
|
39
|
+
i += 1;
|
|
40
|
+
} else {
|
|
41
|
+
code = 0xfffd;
|
|
42
|
+
}
|
|
43
|
+
} else if (code >= 0xd800 && code <= 0xdfff) {
|
|
44
|
+
code = 0xfffd;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (code <= 0x7f) {
|
|
48
|
+
out[at] = code;
|
|
49
|
+
at += 1;
|
|
50
|
+
} else if (code <= 0x7ff) {
|
|
51
|
+
out[at] = 0xc0 | (code >> 6);
|
|
52
|
+
out[at + 1] = 0x80 | (code & 0x3f);
|
|
53
|
+
at += 2;
|
|
54
|
+
} else if (code <= 0xffff) {
|
|
55
|
+
out[at] = 0xe0 | (code >> 12);
|
|
56
|
+
out[at + 1] = 0x80 | ((code >> 6) & 0x3f);
|
|
57
|
+
out[at + 2] = 0x80 | (code & 0x3f);
|
|
58
|
+
at += 3;
|
|
59
|
+
} else {
|
|
60
|
+
out[at] = 0xf0 | (code >> 18);
|
|
61
|
+
out[at + 1] = 0x80 | ((code >> 12) & 0x3f);
|
|
62
|
+
out[at + 2] = 0x80 | ((code >> 6) & 0x3f);
|
|
63
|
+
out[at + 3] = 0x80 | (code & 0x3f);
|
|
64
|
+
at += 4;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
function nscMdEqual(a: Uint8Array, b: Uint8Array, insensitive: boolean): boolean {
|
|
72
|
+
if (a.length !== b.length) return false;
|
|
73
|
+
for (let i = 0; i < a.length; i++) { let x = a[i]!, y = b[i]!; if (insensitive) { if (x >= 65 && x <= 90) x += 32; if (y >= 65 && y <= 90) y += 32; } if (x !== y) return false; }
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
function nscMdStarts(a: Uint8Array, at: number, text: string, insensitive: boolean): boolean {
|
|
77
|
+
if (at + text.length > a.length) return false;
|
|
78
|
+
for (let i = 0; i < text.length; i++) { let x = a[at + i]!, y = text.charCodeAt(i); if (insensitive) { if (x >= 65 && x <= 90) x += 32; if (y >= 65 && y <= 90) y += 32; } if (x !== y) return false; } return true;
|
|
79
|
+
}
|
|
80
|
+
function nscMdFind(a: Uint8Array, at: number, text: string, insensitive: boolean): number {
|
|
81
|
+
for (let i = at; i + text.length <= a.length; i++) if (nscMdStarts(a, i, text, insensitive)) return i; return -1;
|
|
82
|
+
}
|
|
83
|
+
function nscMdFindByte(a: Uint8Array, at: number, byte: number): number { for (let i = at; i < a.length; i++) if (a[i] === byte) return i; return -1; }
|
|
84
|
+
function nscMdScan(slot: NscMdSlot, text: Uint8Array, from: number, pattern: string): number {
|
|
85
|
+
if (slot.valid && from >= slot.from && (slot.pos < 0 || from <= slot.pos)) return slot.pos;
|
|
86
|
+
slot.valid = true; slot.from = from; slot.pos = nscMdFind(text, from, pattern, false); return slot.pos;
|
|
87
|
+
}
|
|
88
|
+
function nscMdAlpha(b: number): boolean { return b >= 65 && b <= 90 || b >= 97 && b <= 122; }
|
|
89
|
+
function nscMdDigit(b: number): boolean { return b >= 48 && b <= 57; }
|
|
90
|
+
function nscMdWord(b: number): boolean { return nscMdAlpha(b) || nscMdDigit(b) || b === 95; }
|
|
91
|
+
function nscMdSpace(b: number): boolean { return b === 32 || b === 9; }
|
|
92
|
+
function nscMdWhite(b: number): boolean { return b === 32 || b >= 9 && b <= 13; }
|
|
93
|
+
function nscMdPunctuation(b: number): boolean { return b >= 33 && b <= 47 || b >= 58 && b <= 64 || b >= 91 && b <= 96 || b >= 123 && b <= 126; }
|
|
94
|
+
function nscMdTrim(a: Uint8Array, chars: string, left: boolean, right: boolean): Uint8Array {
|
|
95
|
+
let start = 0, end = a.length;
|
|
96
|
+
if (left) while (start < end && chars.indexOf(String.fromCharCode(a[start]!)) >= 0) start++;
|
|
97
|
+
if (right) while (end > start && chars.indexOf(String.fromCharCode(a[end - 1]!)) >= 0) end--;
|
|
98
|
+
return a.subarray(start, end);
|
|
99
|
+
}
|
|
100
|
+
function nscMdConcat(a: Uint8Array, b: Uint8Array): Uint8Array { const out = new Uint8Array(a.length + b.length); out.set(a); out.set(b, a.length); return out; }
|
|
101
|
+
class NscMdLines {
|
|
102
|
+
source: Uint8Array; index = 0; pending: Uint8Array | null = null;
|
|
103
|
+
constructor(source: Uint8Array) { this.source = source; }
|
|
104
|
+
copy(): NscMdLines { const out = new NscMdLines(this.source); out.index = this.index; out.pending = this.pending; return out; }
|
|
105
|
+
restore(other: NscMdLines): void { this.index = other.index; this.pending = other.pending; }
|
|
106
|
+
next(): Uint8Array | null {
|
|
107
|
+
if (this.pending !== null) { const line = this.pending; this.pending = null; return line; }
|
|
108
|
+
if (this.index >= this.source.length) return null;
|
|
109
|
+
const start = this.index, found = nscMdFindByte(this.source, start, 10), end = found < 0 ? this.source.length : found;
|
|
110
|
+
this.index = Math.min(end + 1, this.source.length); return nscMdTrim(this.source.subarray(start, end), "\r", false, true);
|
|
111
|
+
}
|
|
112
|
+
prepend(line: Uint8Array): void { if (this.pending !== null) throw new Error("occupied Markdown line slot"); this.pending = line; }
|
|
113
|
+
peek(): Uint8Array | null { return this.copy().next(); }
|
|
114
|
+
second(): Uint8Array | null { const copy = this.copy(); if (copy.next() === null) return null; return copy.next(); }
|
|
115
|
+
}
|
|
116
|
+
// Tag kind numbers are recipe-local and deliberately independent of native enums.
|
|
117
|
+
function nscMdClassify(name: Uint8Array): number {
|
|
118
|
+
const names = ["b strong", "i em var cite", "s strike del", "u ins", "code kbd samp tt", "mark", "small sub sup", "a", "img", "br", "wbr", "q", "", "hr", "p", "blockquote", "pre", "ol ul dl", "li dt dd", "table", "thead tbody tfoot", "tr", "td th", "abbr bdo caption center div span section article header footer main figure figcaption time ruby rt rp"];
|
|
119
|
+
if (name.length === 2 && (name[0] === 104 || name[0] === 72) && name[1]! >= 49 && name[1]! <= 54) return 12;
|
|
120
|
+
for (let kind = 0; kind < names.length; kind++) {
|
|
121
|
+
const list = names[kind]!, words = list.split(" ");
|
|
122
|
+
for (const word of words) if (word.length > 0 && nscMdEqual(name, nscMdBytes(word), true)) return kind;
|
|
123
|
+
} return -1;
|
|
124
|
+
}
|
|
125
|
+
function nscMdTag(text: Uint8Array, at: number, classified: boolean): NscMdTag | null {
|
|
126
|
+
if (at >= text.length || text[at] !== 60) return null;
|
|
127
|
+
let i = at + 1, closing = false;
|
|
128
|
+
if (i < text.length && text[i] === 47) { closing = true; i++; }
|
|
129
|
+
if (i >= text.length || !nscMdAlpha(text[i]!)) return null;
|
|
130
|
+
const start = i;
|
|
131
|
+
while (i < text.length && (nscMdAlpha(text[i]!) || nscMdDigit(text[i]!) || text[i] === 45)) i++;
|
|
132
|
+
const name = text.subarray(start, i), kind = nscMdClassify(name);
|
|
133
|
+
if (classified && kind < 0) return null;
|
|
134
|
+
if (i < text.length && text[i] !== 62 && text[i] !== 47 && !nscMdWhite(text[i]!)) return null;
|
|
135
|
+
const attrs = i, limit = Math.min(text.length, at + 1024); let quote = -1;
|
|
136
|
+
for (; i < limit; i++) {
|
|
137
|
+
const b = text[i]!;
|
|
138
|
+
if (quote >= 0) { if (b === quote) quote = -1; continue; }
|
|
139
|
+
if (b === 34 || b === 39) { quote = b; continue; }
|
|
140
|
+
if (b === 60) return null;
|
|
141
|
+
if (b !== 62) continue;
|
|
142
|
+
const attributes = text.subarray(attrs, i), trimmed = nscMdTrim(attributes, " \t\r\n", true, true);
|
|
143
|
+
return { name, attributes, closing, selfClosing: !closing && trimmed.length > 0 && trimmed[trimmed.length - 1] === 47, consumed: i + 1 - at, kind, level: kind === 12 ? name[1]! - 48 : 0 };
|
|
144
|
+
} return null;
|
|
145
|
+
}
|
|
146
|
+
function nscMdName(tag: NscMdTag, name: string): boolean { return nscMdEqual(tag.name, nscMdBytes(name), true); }
|
|
147
|
+
function nscMdAttr(tag: NscMdTag, wanted: string): Uint8Array | null {
|
|
148
|
+
const a = tag.attributes; let i = 0;
|
|
149
|
+
while (i < a.length) {
|
|
150
|
+
while (i < a.length && nscMdWhite(a[i]!)) i++;
|
|
151
|
+
if (i >= a.length || a[i] === 47) break;
|
|
152
|
+
const start = i;
|
|
153
|
+
while (i < a.length && (nscMdAlpha(a[i]!) || nscMdDigit(a[i]!) || a[i] === 45 || a[i] === 95 || a[i] === 58)) i++;
|
|
154
|
+
if (i === start) { i++; continue; }
|
|
155
|
+
const name = a.subarray(start, i);
|
|
156
|
+
while (i < a.length && nscMdWhite(a[i]!)) i++;
|
|
157
|
+
if (i >= a.length || a[i] !== 61) continue;
|
|
158
|
+
i++; while (i < a.length && nscMdWhite(a[i]!)) i++;
|
|
159
|
+
if (i >= a.length) return null;
|
|
160
|
+
let value: Uint8Array;
|
|
161
|
+
if (a[i] === 34 || a[i] === 39) { const quote = a[i++]!, v = i; while (i < a.length && a[i] !== quote) i++; if (i >= a.length) return null; value = a.subarray(v, i++); }
|
|
162
|
+
else { const v = i; while (i < a.length && !nscMdWhite(a[i]!)) i++; value = a.subarray(v, i); }
|
|
163
|
+
if (nscMdEqual(name, nscMdBytes(wanted), true)) return value;
|
|
164
|
+
} return null;
|
|
165
|
+
}
|
|
166
|
+
function nscMdVoid(name: Uint8Array): boolean { for (const word of ["area", "base", "col", "embed", "input", "link", "meta", "param", "source", "track"]) if (nscMdEqual(name, nscMdBytes(word), true)) return true; return false; }
|
|
167
|
+
function nscMdBlock(tag: NscMdTag): boolean { return tag.kind >= 12 && tag.kind <= 22 || tag.kind === 23 && nscMdStructural(tag); }
|
|
168
|
+
function nscMdStructural(tag: NscMdTag): boolean {
|
|
169
|
+
if (tag.kind !== 23) return tag.kind >= 12 && tag.kind <= 22;
|
|
170
|
+
for (const word of ["center", "div", "section", "article", "header", "footer", "main", "figure", "figcaption", "caption"]) if (nscMdName(tag, word)) return true; return false;
|
|
171
|
+
}
|
|
172
|
+
function nscMdAlignment(tag: NscMdTag): number {
|
|
173
|
+
if (nscMdName(tag, "center")) return 1;
|
|
174
|
+
const value = nscMdAttr(tag, "align"); if (value === null) return -1;
|
|
175
|
+
if (nscMdEqual(value, nscMdBytes("center"), true)) return 1;
|
|
176
|
+
if (nscMdEqual(value, nscMdBytes("right"), true) || nscMdEqual(value, nscMdBytes("end"), true)) return 2;
|
|
177
|
+
if (nscMdEqual(value, nscMdBytes("left"), true) || nscMdEqual(value, nscMdBytes("start"), true)) return 0; return -1;
|
|
178
|
+
}
|
|
179
|
+
class NscMdOpaque { name: Uint8Array = new Uint8Array(0); depth = 0; comment = false; active(): boolean { return this.depth > 0 || this.comment; } }
|
|
180
|
+
interface NscMdMatch { start: number; end: number; tag: NscMdTag }
|
|
181
|
+
class NscMdTagScan {
|
|
182
|
+
cursor = 0; cache = new NscMdCache(); opaque: NscMdOpaque;
|
|
183
|
+
constructor(opaque: NscMdOpaque) { this.opaque = opaque; }
|
|
184
|
+
next(source: Uint8Array): NscMdMatch | null {
|
|
185
|
+
while (this.cursor < source.length) {
|
|
186
|
+
if (this.opaque.comment) { const close = nscMdFind(source, this.cursor, "-->", false); if (close < 0) { this.cursor = source.length; return null; } this.opaque.comment = false; this.cursor = close + 3; continue; }
|
|
187
|
+
const start = nscMdFindByte(source, this.cursor, 60); if (start < 0) return null;
|
|
188
|
+
if (this.opaque.depth === 0) {
|
|
189
|
+
const tick = nscMdScan(this.cache.tick, source, this.cursor, "`");
|
|
190
|
+
if (tick >= 0 && tick < start) { const end = nscMdScan(this.cache.tick, source, tick + 1, "`"); this.cursor = end < 0 ? tick + 1 : end + 1; continue; }
|
|
191
|
+
}
|
|
192
|
+
if (nscMdStarts(source, start, "<!--", false)) { const close = nscMdFind(source, start + 4, "-->", false); if (close < 0) { this.opaque.comment = true; this.cursor = source.length; return null; } this.cursor = close + 3; continue; }
|
|
193
|
+
const tag = nscMdTag(source, start, false); if (tag === null) { this.cursor = start + 1; continue; }
|
|
194
|
+
this.cursor = start + tag.consumed;
|
|
195
|
+
if (this.opaque.depth > 0) {
|
|
196
|
+
if (!nscMdEqual(tag.name, this.opaque.name, true)) continue;
|
|
197
|
+
if (tag.closing) { this.opaque.depth--; if (this.opaque.depth === 0) this.opaque.name = new Uint8Array(0); }
|
|
198
|
+
else if (!tag.selfClosing) this.opaque.depth++;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (tag.kind >= 0) return { start, end: this.cursor, tag };
|
|
202
|
+
if (!tag.closing && !tag.selfClosing && !nscMdVoid(tag.name)) { this.opaque.name = tag.name; this.opaque.depth = 1; }
|
|
203
|
+
} return null;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function nscMdHasClose(source: Uint8Array, from: number, name: Uint8Array): boolean {
|
|
207
|
+
const scan = new NscMdTagScan(new NscMdOpaque()); scan.cursor = from; let depth = 0;
|
|
208
|
+
for (;;) { const match = scan.next(source); if (match === null) return false; const tag = match.tag; if (!nscMdEqual(tag.name, name, true)) continue; if (!tag.closing) { if (!tag.selfClosing) depth++; } else { if (depth === 0) return true; depth--; } }
|
|
209
|
+
}
|
|
210
|
+
function nscMdUnbalanced(source: Uint8Array, name: Uint8Array | null): NscMdMatch | null {
|
|
211
|
+
const scan = new NscMdTagScan(new NscMdOpaque()); let depth = 0;
|
|
212
|
+
for (;;) { const match = scan.next(source); if (match === null) return null; const tag = match.tag; if (name === null ? !nscMdStructural(tag) : !nscMdEqual(tag.name, name, true)) continue; if (!tag.closing) { if (!tag.selfClosing) depth++; } else { if (depth === 0) return match; depth--; } }
|
|
213
|
+
}
|
|
214
|
+
function nscMdUnsupported(source: Uint8Array, at: number, opening: NscMdTag): number {
|
|
215
|
+
let cursor = at + opening.consumed, depth = 1; const cache = new NscMdCache();
|
|
216
|
+
if (opening.closing || opening.selfClosing || nscMdVoid(opening.name)) return cursor;
|
|
217
|
+
for (;;) {
|
|
218
|
+
const start = nscMdFindByte(source, cursor, 60); if (start < 0) return source.length;
|
|
219
|
+
const comment = nscMdComment(source, start, cache); if (comment >= 0) { cursor = start + comment; continue; }
|
|
220
|
+
const tag = nscMdTag(source, start, false); if (tag === null) { cursor = start + 1; continue; }
|
|
221
|
+
cursor = start + tag.consumed;
|
|
222
|
+
if (!nscMdEqual(tag.name, opening.name, true)) continue;
|
|
223
|
+
if (tag.closing) { depth--; if (depth === 0) return cursor; } else if (!tag.selfClosing) depth++;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function nscMdComment(source: Uint8Array, at: number, cache: NscMdCache): number { if (!nscMdStarts(source, at, "<!--", false)) return -1; const end = nscMdScan(cache.comment, source, at + 4, "-->"); return end < 0 ? -1 : end + 3 - at; }
|
|
227
|
+
function nscMdSingle(line: Uint8Array, opening: NscMdTag): Uint8Array | null {
|
|
228
|
+
if (opening.closing || opening.selfClosing) return null;
|
|
229
|
+
let at = line.length - 1; while (at >= 0 && line[at] !== 60) at--;
|
|
230
|
+
if (at < opening.consumed) return null;
|
|
231
|
+
const closing = nscMdTag(line, at, true);
|
|
232
|
+
if (closing === null || !closing.closing || closing.consumed !== line.length - at || !nscMdEqual(opening.name, closing.name, true)) return null;
|
|
233
|
+
return line.subarray(opening.consumed, at);
|
|
234
|
+
}
|
|
235
|
+
function nscMdEntity(rest: Uint8Array): { text: Uint8Array; consumed: number } | null {
|
|
236
|
+
const names = ["&", "<", ">", """, "'", " "], values = ["&", "<", ">", '"', "'", "\u00a0"];
|
|
237
|
+
for (let i = 0; i < names.length; i++) if (nscMdStarts(rest, 0, names[i]!, false)) return { text: nscMdBytes(values[i]!), consumed: names[i]!.length };
|
|
238
|
+
if (!nscMdStarts(rest, 0, "&#", false)) return null;
|
|
239
|
+
const semi = nscMdFindByte(rest.subarray(0, Math.min(rest.length, 14)), 2, 59); if (semi < 0) return null;
|
|
240
|
+
let at = 2, radix = 10; if (semi - at > 1 && (rest[at] === 120 || rest[at] === 88)) { radix = 16; at++; }
|
|
241
|
+
if (at === semi) return null; let value = 0;
|
|
242
|
+
// The unsigned parser accepts embedded underscores, never edge ones.
|
|
243
|
+
if (rest[at] === 95 || rest[semi - 1] === 95) return null;
|
|
244
|
+
let digits = 0;
|
|
245
|
+
for (; at < semi; at++) { const b = rest[at]!; if (b === 95) continue; const d = b >= 48 && b <= 57 ? b - 48 : b >= 65 && b <= 70 ? b - 55 : b >= 97 && b <= 102 ? b - 87 : -1; if (d < 0 || d >= radix) return null; value = value * radix + d; digits++; if (value > 0x1fffff) return null; }
|
|
246
|
+
if (digits === 0 || value === 0 || value > 0x10ffff || value >= 0xd800 && value <= 0xdfff) return null;
|
|
247
|
+
const out = new Uint8Array(value <= 127 ? 1 : value <= 2047 ? 2 : value <= 65535 ? 3 : 4);
|
|
248
|
+
if (out.length === 1) out[0] = value;
|
|
249
|
+
else { for (let i = out.length - 1; i > 0; i--) { out[i] = 128 | value & 63; value = Math.floor(value / 64); } out[0] = (out.length === 2 ? 192 : out.length === 3 ? 224 : 240) | value; }
|
|
250
|
+
return { text: out, consumed: semi + 1 };
|
|
251
|
+
}
|
|
252
|
+
function nscMdDecode(text: Uint8Array): Uint8Array {
|
|
253
|
+
if (nscMdFindByte(text, 0, 38) < 0) return text;
|
|
254
|
+
const out = new Uint8Array(text.length); let at = 0, len = 0;
|
|
255
|
+
while (at < text.length) { const entity = text[at] === 38 ? nscMdEntity(text.subarray(at)) : null; if (entity !== null) { out.set(entity.text, len); len += entity.text.length; at += entity.consumed; } else out[len++] = text[at++]!; }
|
|
256
|
+
return out.subarray(0, len);
|
|
257
|
+
}
|
|
258
|
+
function nscMdLink(text: Uint8Array, index: number, cache: NscMdCache): NscMdLink | null {
|
|
259
|
+
if (text.length - index < 4 || text[index] !== 91) return null;
|
|
260
|
+
const bracket = nscMdScan(cache.bracket, text, index, "]"); if (bracket < 0 || bracket + 1 >= text.length || text[bracket + 1] !== 40) return null;
|
|
261
|
+
const paren = nscMdScan(cache.paren, text, bracket + 2, ")"); if (paren < 0) return null;
|
|
262
|
+
const space = nscMdScan(cache.title, text, bracket + 2, " "), end = space >= 0 && space < paren ? space : paren;
|
|
263
|
+
if (bracket === index + 1 || end === bracket + 2) return null;
|
|
264
|
+
return { text: text.subarray(index + 1, bracket), target: text.subarray(bracket + 2, end), consumed: paren + 1 - index };
|
|
265
|
+
}
|
|
266
|
+
function nscMdAutolink(text: Uint8Array, index: number, cache: NscMdCache): NscMdLink | null {
|
|
267
|
+
if (text.length - index < 3 || text[index] !== 60) return null;
|
|
268
|
+
const close = nscMdScan(cache.angle, text, index, ">"); if (close < 0) return null;
|
|
269
|
+
const sep = nscMdScan(cache.scheme, text, index + 1, "://"); if (sep < 0 || sep + 3 > close) return null;
|
|
270
|
+
const space = nscMdScan(cache.space, text, index + 1, " "); if (space >= 0 && space < close) return null;
|
|
271
|
+
const target = text.subarray(index + 1, close); return { text: target, target, consumed: close + 1 - index };
|
|
272
|
+
}
|
|
273
|
+
function nscMdBoundary(text: Uint8Array, at: number): boolean { return at === 0 || !nscMdWord(text[at - 1]!) && text[at - 1] !== 47 && text[at - 1] !== 38; }
|
|
274
|
+
function nscMdBare(rest: Uint8Array): NscMdLink | null {
|
|
275
|
+
const prefix = nscMdStarts(rest, 0, "https://", false) ? 8 : nscMdStarts(rest, 0, "http://", false) ? 7 : 0; if (prefix === 0) return null;
|
|
276
|
+
let end = prefix, balance = 0;
|
|
277
|
+
for (; end < rest.length; end++) { const b = rest[end]!; if (nscMdSpace(b) || b === 10 || b === 60 || b === 62) break; if (b === 40) balance++; if (b === 41) balance--; }
|
|
278
|
+
while (end > prefix) { const b = rest[end - 1]!; if (b === 41) { if (balance < 0) { end--; balance++; continue; } break; } if (b === 46 || b === 44 || b === 59 || b === 58 || b === 33 || b === 63 || b === 39 || b === 34) end--; else break; }
|
|
279
|
+
if (end === prefix) return null; const target = rest.subarray(0, end); return { text: target, target, consumed: end };
|
|
280
|
+
}
|
|
281
|
+
function nscMdIssue(rest: Uint8Array): number { if (rest.length < 2 || rest[0] !== 35) return -1; let end = 1; while (end < rest.length && nscMdDigit(rest[end]!)) end++; return end === 1 || end < rest.length && nscMdWord(rest[end]!) ? -1 : end; }
|
|
282
|
+
function nscMdHeading(line: Uint8Array): number { let level = 0; while (level < line.length && line[level] === 35) level++; return level === 0 || level > 6 || level < line.length && line[level] !== 32 ? 0 : level; }
|
|
283
|
+
function nscMdRule(line: Uint8Array): boolean { if (line.length < 3 || line[0] !== 45 && line[0] !== 42 && line[0] !== 95) return false; let count = 0; for (const b of line) { if (b === line[0]) count++; else if (b !== 32) return false; } return count >= 3; }
|
|
284
|
+
interface NscMdMarker { kind: number; indent: number; label: Uint8Array; checked: boolean; content: Uint8Array }
|
|
285
|
+
function nscMdMarker(line: Uint8Array): NscMdMarker | null {
|
|
286
|
+
let spaces = 0; while (spaces < line.length && line[spaces] === 32) spaces++;
|
|
287
|
+
const indent = Math.min(Math.floor(spaces / 2), 3), rest = line.subarray(spaces); if (rest.length < 2) return null;
|
|
288
|
+
if ((rest[0] === 45 || rest[0] === 42 || rest[0] === 43) && rest[1] === 32) {
|
|
289
|
+
const content = nscMdTrim(rest.subarray(2), " \t", true, true); let kind = 0, checked = false, consumed = 0;
|
|
290
|
+
if (nscMdStarts(content, 0, "[ ] ", false)) { kind = 2; consumed = 4; }
|
|
291
|
+
else if (nscMdStarts(content, 0, "[x] ", true)) { kind = 2; consumed = 4; checked = true; }
|
|
292
|
+
return { kind, indent, label: new Uint8Array(0), checked, content: content.subarray(consumed) };
|
|
293
|
+
}
|
|
294
|
+
let digits = 0; while (digits < rest.length && nscMdDigit(rest[digits]!)) digits++;
|
|
295
|
+
if (digits > 0 && digits + 1 < rest.length && rest[digits] === 46 && rest[digits + 1] === 32) return { kind: 1, indent, label: rest.subarray(0, digits + 1), checked: false, content: nscMdTrim(rest.subarray(digits + 2), " \t", true, true) }; return null;
|
|
296
|
+
}
|
|
297
|
+
function nscMdReference(line: Uint8Array): boolean {
|
|
298
|
+
let i = 0; while (i < line.length && line[i] === 32) i++; if (i > 3 || i >= line.length || line[i] !== 91) return false;
|
|
299
|
+
i++; let content = false, closed = false;
|
|
300
|
+
while (i < line.length) { const b = line[i]!; if (b === 92 && i + 1 < line.length && nscMdPunctuation(line[i + 1]!)) { content = content || !nscMdSpace(line[i + 1]!); i += 2; continue; } if (b === 91) return false; if (b === 93) { closed = true; i++; break; } content = content || !nscMdSpace(b); i++; }
|
|
301
|
+
if (!closed || !content || i >= line.length || line[i] !== 58) return false;
|
|
302
|
+
i++; while (i < line.length && nscMdSpace(line[i]!)) i++; if (i >= line.length) return false;
|
|
303
|
+
const start = i;
|
|
304
|
+
if (line[i] === 60) {
|
|
305
|
+
i++; closed = false;
|
|
306
|
+
while (i < line.length) { const b = line[i]!; if (b === 92 && i + 1 < line.length && nscMdPunctuation(line[i + 1]!)) { i += 2; continue; } if (b === 60 || b === 10 || b === 13) return false; if (b === 62) { i++; closed = true; break; } i++; } if (!closed) return false;
|
|
307
|
+
} else {
|
|
308
|
+
let depth = 0;
|
|
309
|
+
while (i < line.length && !nscMdSpace(line[i]!)) { const b = line[i]!; if (b < 32 || b === 127 || b === 60) return false; if (b === 92 && i + 1 < line.length && nscMdPunctuation(line[i + 1]!)) { i += 2; continue; } if (b === 40) depth++; else if (b === 41) { if (depth === 0) return false; depth--; } i++; } if (i === start || depth !== 0) return false;
|
|
310
|
+
}
|
|
311
|
+
const end = i; while (i < line.length && nscMdSpace(line[i]!)) i++; if (i === line.length) return true; if (i === end) return false;
|
|
312
|
+
const opener = line[i]!, close = opener === 34 || opener === 39 ? opener : opener === 40 ? 41 : -1; if (close < 0) return false;
|
|
313
|
+
i++; closed = false;
|
|
314
|
+
while (i < line.length) { if (line[i] === 92 && i + 1 < line.length && nscMdPunctuation(line[i + 1]!)) { i += 2; continue; } if (line[i++] === close) { closed = true; break; } }
|
|
315
|
+
while (i < line.length && nscMdSpace(line[i]!)) i++; return closed && i === line.length;
|
|
316
|
+
}
|
|
317
|
+
function nscMdTable(line: Uint8Array): Uint8Array[] | null {
|
|
318
|
+
let rest = nscMdTrim(line, " \t", true, true); if (rest.length === 0 || nscMdFindByte(rest, 0, 124) < 0) return null;
|
|
319
|
+
if (rest[0] === 124) rest = rest.subarray(1);
|
|
320
|
+
if (rest.length > 0 && rest[rest.length - 1] === 124 && !(rest.length > 1 && rest[rest.length - 2] === 92)) rest = rest.subarray(0, rest.length - 1);
|
|
321
|
+
const cells: Uint8Array[] = []; let start = 0;
|
|
322
|
+
for (let i = 0; i < rest.length; i++) { if (rest[i] === 92) { i++; continue; } if (rest[i] !== 124) continue; if (cells.length >= 8) return null; cells.push(nscMdTrim(rest.subarray(start, i), " \t", true, true)); start = i + 1; }
|
|
323
|
+
if (cells.length >= 8) return null; cells.push(nscMdTrim(rest.subarray(Math.min(start, rest.length)), " \t", true, true)); return cells;
|
|
324
|
+
}
|
|
325
|
+
function nscMdDelimiters(line: Uint8Array): number[] | null {
|
|
326
|
+
const cells = nscMdTable(line); if (cells === null) return null; const out: number[] = [];
|
|
327
|
+
for (const cell of cells) { if (cell.length === 0) return null; let body = cell; const leading = body[0] === 58; if (leading) body = body.subarray(1); const trailing = body.length > 0 && body[body.length - 1] === 58; if (trailing) body = body.subarray(0, body.length - 1); if (body.length === 0) return null; for (const b of body) if (b !== 45) return null; out.push(leading && trailing ? 1 : trailing ? 2 : 0); } return out;
|
|
328
|
+
}
|
|
329
|
+
function nscMdTableStart(lines: NscMdLines): boolean { const first = lines.peek(), second = lines.second(); if (first === null || second === null) return false; const header = nscMdTable(first), align = nscMdDelimiters(second); return header !== null && align !== null && header.length === align.length; }
|
|
330
|
+
function nscMdNewBlock(line: Uint8Array): boolean {
|
|
331
|
+
const trimmed = nscMdTrim(line, " \t", true, true); if (trimmed.length === 0 || nscMdStarts(trimmed, 0, "```", false) || nscMdHeading(trimmed) > 0 || nscMdRule(trimmed) || trimmed[0] === 62 || nscMdMarker(line) !== null || nscMdStarts(trimmed, 0, "<details", true) || nscMdStarts(trimmed, 0, "<!--", false)) return true;
|
|
332
|
+
const tag = nscMdTag(trimmed, 0, true); return tag !== null && nscMdBlock(tag);
|
|
333
|
+
}
|
|
334
|
+
interface NscMdSpan { text: Uint8Array; link: Uint8Array; flags: number; scale: number }
|
|
335
|
+
function nscMdSpan(): NscMdSpan { return { text: new Uint8Array(0), link: new Uint8Array(0), flags: 0, scale: 0 }; }
|
|
336
|
+
class NscMdStyle {
|
|
337
|
+
bold = false; italic = false; strike = false; htmlBold = 0; htmlItalic = 0; htmlStrike = 0; underline = 0; mono = 0; mark = 0; small = 0; heading = 0; link: Uint8Array = new Uint8Array(0);
|
|
338
|
+
}
|
|
339
|
+
function nscMdAppend(spans: NscMdSpan[], base: NscMdSpan, style: NscMdStyle, text: Uint8Array, link: Uint8Array, mono: boolean): void {
|
|
340
|
+
if (text.length === 0 || spans.length >= 32) return;
|
|
341
|
+
let flags = base.flags | (mono ? 4 : 0), scale = base.scale;
|
|
342
|
+
if (style.bold || style.htmlBold > 0 || style.heading > 0) flags |= 1;
|
|
343
|
+
if (style.italic || style.htmlItalic > 0) flags |= 2;
|
|
344
|
+
if (style.strike || style.htmlStrike > 0) flags |= 16;
|
|
345
|
+
if (style.underline > 0) flags |= 8;
|
|
346
|
+
if (style.mono > 0) flags |= 4;
|
|
347
|
+
if (style.mark > 0) flags |= 32;
|
|
348
|
+
if (style.small > 0) scale = scale > 0 ? Math.fround(scale * 0.875) : 0.875;
|
|
349
|
+
if (style.heading > 0) scale = style.heading === 1 ? 2 : style.heading === 2 ? 1.5 : 1.25;
|
|
350
|
+
const target = link.length > 0 ? link : style.link; if (target.length > 0) flags |= 8;
|
|
351
|
+
spans.push({ text, link: target, flags, scale });
|
|
352
|
+
}
|
|
353
|
+
function nscMdDepth(depth: number, tag: NscMdTag): number { return tag.closing ? Math.max(0, depth - 1) : !tag.selfClosing ? depth + 1 : depth; }
|
|
354
|
+
function nscMdApply(spans: NscMdSpan[], base: NscMdSpan, style: NscMdStyle, tag: NscMdTag): void {
|
|
355
|
+
const empty = new Uint8Array(0);
|
|
356
|
+
switch (tag.kind) {
|
|
357
|
+
case 0: style.htmlBold = nscMdDepth(style.htmlBold, tag); break;
|
|
358
|
+
case 1: style.htmlItalic = nscMdDepth(style.htmlItalic, tag); break;
|
|
359
|
+
case 2: style.htmlStrike = nscMdDepth(style.htmlStrike, tag); break;
|
|
360
|
+
case 3: style.underline = nscMdDepth(style.underline, tag); break;
|
|
361
|
+
case 4: case 16: style.mono = nscMdDepth(style.mono, tag); break;
|
|
362
|
+
case 5: style.mark = nscMdDepth(style.mark, tag); break;
|
|
363
|
+
case 6: style.small = nscMdDepth(style.small, tag); break;
|
|
364
|
+
case 12: style.heading = tag.closing || tag.selfClosing ? 0 : tag.level; break;
|
|
365
|
+
case 7: { const href = nscMdAttr(tag, "href"); style.link = tag.closing || tag.selfClosing || href === null ? empty : nscMdDecode(href); break; }
|
|
366
|
+
case 9: if (!tag.closing) nscMdAppend(spans, base, style, nscMdBytes("\n"), empty, false); break;
|
|
367
|
+
case 10: if (!tag.closing) nscMdAppend(spans, base, style, nscMdBytes("\u200b"), empty, false); break;
|
|
368
|
+
case 8: if (!tag.closing) { const alt = nscMdAttr(tag, "alt"); if (alt !== null) nscMdAppend(spans, base, style, nscMdDecode(alt), empty, false); } break;
|
|
369
|
+
case 11: if (!tag.selfClosing) nscMdAppend(spans, base, style, nscMdBytes(tag.closing ? "”" : "“"), empty, false); break;
|
|
370
|
+
case 18: if (!tag.selfClosing) nscMdAppend(spans, base, style, nscMdBytes(tag.closing ? "\n" : "• "), empty, false); break;
|
|
371
|
+
case 22: if (tag.closing) nscMdAppend(spans, base, style, nscMdBytes("\t"), empty, false); break;
|
|
372
|
+
case 21: if (tag.closing) nscMdAppend(spans, base, style, nscMdBytes("\n"), empty, false); break;
|
|
373
|
+
case 13: if (!tag.closing) nscMdAppend(spans, base, style, nscMdBytes("\n"), empty, false); break;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
function nscMdInline(text: Uint8Array, base: NscMdSpan, issueBase: Uint8Array | null): NscMdSpan[] {
|
|
377
|
+
const spans: NscMdSpan[] = [], style = new NscMdStyle(), cache = new NscMdCache(), tags: Uint8Array[] = [], empty = new Uint8Array(0);
|
|
378
|
+
let literal = 0, index = 0, consumedHtml = false;
|
|
379
|
+
while (index < text.length) {
|
|
380
|
+
if (spans.length + 2 >= 32) break;
|
|
381
|
+
const rest = text.subarray(index), byte = rest[0]!; let link: NscMdLink | null = null, advance = 0, handled = false;
|
|
382
|
+
if (byte === 96) {
|
|
383
|
+
const close = nscMdFindByte(rest, 1, 96);
|
|
384
|
+
if (close >= 0) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); nscMdAppend(spans, base, style, rest.subarray(1, close), empty, true); advance = close + 1; }
|
|
385
|
+
} else if (nscMdStarts(rest, 0, "**", false) || nscMdStarts(rest, 0, "__", false)) {
|
|
386
|
+
const delimiter = byte === 42 ? "**" : "__";
|
|
387
|
+
if (style.bold || nscMdFind(rest, 2, delimiter, false) >= 0) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); style.bold = !style.bold; advance = 2; }
|
|
388
|
+
} else if (nscMdStarts(rest, 0, "~~", false)) {
|
|
389
|
+
if (style.strike || nscMdFind(rest, 2, "~~", false) >= 0) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); style.strike = !style.strike; advance = 2; }
|
|
390
|
+
} else if (byte === 42 || byte === 95) {
|
|
391
|
+
const boundary = byte === 42 || index === 0 || !nscMdWord(text[index - 1]!);
|
|
392
|
+
if (boundary && (style.italic || rest.length > 1 && !nscMdSpace(rest[1]!) && nscMdFindByte(rest, 1, byte) >= 0)) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); style.italic = !style.italic; advance = 1; }
|
|
393
|
+
} else if (byte === 91) link = nscMdLink(text, index, cache);
|
|
394
|
+
else if (byte === 33 && rest.length > 1 && rest[1] === 91) {
|
|
395
|
+
const image = nscMdLink(text, index + 1, cache);
|
|
396
|
+
if (image !== null) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); nscMdAppend(spans, base, style, image.text, empty, false); advance = image.consumed + 1; }
|
|
397
|
+
} else if (byte === 60) {
|
|
398
|
+
const comment = nscMdComment(text, index, cache);
|
|
399
|
+
if (comment >= 0) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); consumedHtml = true; advance = comment; }
|
|
400
|
+
else {
|
|
401
|
+
link = nscMdAutolink(text, index, cache);
|
|
402
|
+
if (link === null) {
|
|
403
|
+
const tag = nscMdTag(text, index, true);
|
|
404
|
+
if (tag !== null) {
|
|
405
|
+
const required = tag.kind !== 8 && tag.kind !== 9 && tag.kind !== 10 && tag.kind !== 13;
|
|
406
|
+
const accepted = tag.closing ? required && tags.length > 0 && nscMdEqual(tags[tags.length - 1]!, tag.name, true) : tag.selfClosing || !required || tags.length < 32 && nscMdHasClose(text, index + tag.consumed, tag.name);
|
|
407
|
+
if (!accepted) { index += tag.consumed; handled = true; }
|
|
408
|
+
else { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); consumedHtml = true; nscMdApply(spans, base, style, tag); if (tag.closing) tags.pop(); else if (required && !tag.selfClosing) tags.push(tag.name); advance = tag.consumed; }
|
|
409
|
+
} else {
|
|
410
|
+
const syntax = nscMdTag(text, index, false);
|
|
411
|
+
if (syntax !== null) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); const end = nscMdUnsupported(text, index, syntax); nscMdAppend(spans, base, style, text.subarray(index, end), empty, false); advance = end - index; }
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
} else if (byte === 38) {
|
|
416
|
+
const entity = nscMdEntity(rest); if (entity !== null) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); nscMdAppend(spans, base, style, entity.text, empty, false); consumedHtml = true; advance = entity.consumed; }
|
|
417
|
+
} else if (byte === 104 && nscMdBoundary(text, index)) link = nscMdBare(rest);
|
|
418
|
+
else if (byte === 35 && nscMdBoundary(text, index) && issueBase !== null) { const end = nscMdIssue(rest); if (end >= 0) link = { text: rest.subarray(0, end), target: nscMdConcat(issueBase, rest.subarray(1, end)), consumed: end }; }
|
|
419
|
+
if (handled) continue;
|
|
420
|
+
if (link !== null) { nscMdAppend(spans, base, style, text.subarray(literal, index), empty, false); nscMdAppend(spans, base, style, link.text, link.target, false); advance = link.consumed; }
|
|
421
|
+
if (advance > 0) { index += advance; literal = index; } else index++;
|
|
422
|
+
}
|
|
423
|
+
nscMdAppend(spans, base, style, text.subarray(literal), empty, false);
|
|
424
|
+
if (spans.length === 0 && !consumedHtml) spans.push({ text, link: empty, flags: base.flags, scale: base.scale });
|
|
425
|
+
return spans;
|
|
426
|
+
}
|
|
427
|
+
interface NscMdImage { source: Uint8Array; lower: number; upper: number; width: number; height: number }
|
|
428
|
+
interface NscMdLeading { source: Uint8Array; alt: Uint8Array; link: Uint8Array; consumed: number; width: number | null; height: number | null }
|
|
429
|
+
function nscMdDimension(tag: NscMdTag, name: string): number | null {
|
|
430
|
+
const raw = nscMdAttr(tag, name); if (raw === null) return null;
|
|
431
|
+
const text = nscMdTrim(raw, " \t", true, true);
|
|
432
|
+
const value = nscMdFloat(text); return Number.isFinite(value) && value > 0 ? value : null;
|
|
433
|
+
}
|
|
434
|
+
function nscMdLeading(text: Uint8Array): NscMdLeading | null {
|
|
435
|
+
let cursor = 0; while (cursor < text.length && nscMdSpace(text[cursor]!)) cursor++;
|
|
436
|
+
const empty = new Uint8Array(0);
|
|
437
|
+
if (cursor + 1 < text.length && text[cursor] === 33 && text[cursor + 1] === 91) { const link = nscMdLink(text, cursor + 1, new NscMdCache()); return link === null ? null : { source: link.target, alt: link.text, link: empty, consumed: cursor + link.consumed + 1, width: null, height: null }; }
|
|
438
|
+
const wrappers: Uint8Array[] = []; let link: Uint8Array = empty;
|
|
439
|
+
while (cursor < text.length) {
|
|
440
|
+
const tag = nscMdTag(text, cursor, true); if (tag === null || tag.closing) return null;
|
|
441
|
+
if (tag.kind === 8) {
|
|
442
|
+
const source = nscMdAttr(tag, "src"); if (source === null) return null; const alt = nscMdAttr(tag, "alt"); cursor += tag.consumed;
|
|
443
|
+
while (wrappers.length > 0) { while (cursor < text.length && nscMdSpace(text[cursor]!)) cursor++; const closing = nscMdTag(text, cursor, true); if (closing === null || !closing.closing || !nscMdEqual(closing.name, wrappers[wrappers.length - 1]!, true)) return null; cursor += closing.consumed; wrappers.pop(); }
|
|
444
|
+
return { source, alt: alt === null ? empty : alt, link, consumed: cursor, width: nscMdDimension(tag, "width"), height: nscMdDimension(tag, "height") };
|
|
445
|
+
}
|
|
446
|
+
if (!(tag.kind <= 3 || tag.kind === 5 || tag.kind === 6 || tag.kind === 7 || tag.kind === 23) || tag.selfClosing || wrappers.length >= 8) return null;
|
|
447
|
+
if (tag.kind === 7) { const href = nscMdAttr(tag, "href"); link = href === null ? empty : href; }
|
|
448
|
+
wrappers.push(tag.name); cursor += tag.consumed; while (cursor < text.length && nscMdSpace(text[cursor]!)) cursor++;
|
|
449
|
+
} return null;
|
|
450
|
+
}
|
|
451
|
+
// Round directly to f32 by comparing exact decimal integers with adjacent
|
|
452
|
+
// binary midpoints. This also covers hexadecimal spelling without f64
|
|
453
|
+
// conversion or a second rounding at the ABI boundary.
|
|
454
|
+
interface NscMdDecimal { digits: number[]; exponent: number }
|
|
455
|
+
function nscMdDigitValue(byte: number): number { return byte >= 48 && byte <= 57 ? byte - 48 : byte >= 65 && byte <= 70 ? byte - 55 : byte >= 97 && byte <= 102 ? byte - 87 : -1; }
|
|
456
|
+
function nscMdNormalize(digits: number[], exponent: number): NscMdDecimal {
|
|
457
|
+
let start = 0, end = digits.length;
|
|
458
|
+
while (end > 0 && digits[end - 1] === 0) end--;
|
|
459
|
+
if (end === 0) return { digits: [], exponent: 0 };
|
|
460
|
+
while (start < end && digits[start] === 0) start++;
|
|
461
|
+
return { digits: digits.slice(start, end), exponent: exponent + start };
|
|
462
|
+
}
|
|
463
|
+
function nscMdMultiply(digits: number[], factor: number, add: number): void {
|
|
464
|
+
let carry = add;
|
|
465
|
+
for (let i = 0; i < digits.length; i++) { const value = digits[i]! * factor + carry; digits[i] = value % 10; carry = Math.floor(value / 10); }
|
|
466
|
+
while (carry > 0) { digits.push(carry % 10); carry = Math.floor(carry / 10); }
|
|
467
|
+
}
|
|
468
|
+
function nscMdMidpoint(word: number): NscMdDecimal {
|
|
469
|
+
const field = Math.floor(word / 8388608), mantissa = field === 0 ? word : word % 8388608 + 8388608, power = field === 0 ? -150 : field - 151;
|
|
470
|
+
let value = mantissa * 2 + 1; const digits: number[] = [];
|
|
471
|
+
while (value > 0) { digits.push(value % 10); value = Math.floor(value / 10); }
|
|
472
|
+
for (let i = 0; i < Math.abs(power); i++) nscMdMultiply(digits, power < 0 ? 5 : 2, 0);
|
|
473
|
+
return nscMdNormalize(digits, Math.min(0, power));
|
|
474
|
+
}
|
|
475
|
+
function nscMdCompare(a: NscMdDecimal, b: NscMdDecimal): number {
|
|
476
|
+
const x = a.digits.length + a.exponent, y = b.digits.length + b.exponent;
|
|
477
|
+
if (x !== y) return x < y ? -1 : 1;
|
|
478
|
+
const count = Math.max(a.digits.length, b.digits.length);
|
|
479
|
+
for (let i = 0; i < count; i++) { const left = i < a.digits.length ? a.digits[a.digits.length - 1 - i]! : 0, right = i < b.digits.length ? b.digits[b.digits.length - 1 - i]! : 0; if (left !== right) return left < right ? -1 : 1; } return 0;
|
|
480
|
+
}
|
|
481
|
+
function nscMdFloat(text: Uint8Array): number {
|
|
482
|
+
if (text.length === 0) return NaN;
|
|
483
|
+
let at = 0; if (text[at] === 45) return NaN; if (text[at] === 43) at++;
|
|
484
|
+
if (at >= text.length) return NaN;
|
|
485
|
+
const hex = nscMdStarts(text, at, "0x", true); if (hex) at += 2;
|
|
486
|
+
const digits: number[] = []; let fractional = 0, dot = false;
|
|
487
|
+
while (at < text.length) {
|
|
488
|
+
const byte = text[at]!, digit = nscMdDigitValue(byte);
|
|
489
|
+
if (digit >= 0 && digit < (hex ? 16 : 10)) { digits.push(digit); if (dot) fractional++; at++; continue; }
|
|
490
|
+
if (byte === 95) { const before = at > 0 ? nscMdDigitValue(text[at - 1]!) : -1, after = at + 1 < text.length ? nscMdDigitValue(text[at + 1]!) : -1; if (before < 0 || after < 0 || before >= (hex ? 16 : 10) || after >= (hex ? 16 : 10)) return NaN; at++; continue; }
|
|
491
|
+
if (byte === 46 && !dot) { dot = true; at++; continue; } break;
|
|
492
|
+
}
|
|
493
|
+
if (digits.length === 0) return NaN;
|
|
494
|
+
let exponent = 0;
|
|
495
|
+
if (at < text.length && (text[at] === (hex ? 112 : 101) || text[at] === (hex ? 80 : 69))) {
|
|
496
|
+
at++; let negative = false; if (at < text.length && (text[at] === 45 || text[at] === 43)) { negative = text[at] === 45; at++; }
|
|
497
|
+
let count = 0;
|
|
498
|
+
while (at < text.length) { const byte = text[at]!; if (byte === 95) { if (at === 0 || !nscMdDigit(text[at - 1]!) || at + 1 >= text.length || !nscMdDigit(text[at + 1]!)) return NaN; at++; continue; } if (!nscMdDigit(byte)) return NaN; if (exponent < 0x10000000) exponent = exponent * 10 + byte - 48; count++; at++; }
|
|
499
|
+
if (count === 0) return NaN; if (negative) exponent = -exponent;
|
|
500
|
+
}
|
|
501
|
+
if (at !== text.length) return NaN;
|
|
502
|
+
let decimal: NscMdDecimal;
|
|
503
|
+
if (hex) {
|
|
504
|
+
let start = 0; while (start < digits.length && digits[start] === 0) start++;
|
|
505
|
+
if (start === digits.length) return 0;
|
|
506
|
+
exponent -= fractional * 4;
|
|
507
|
+
const extent = exponent + (digits.length - start) * 4; if (extent > 132) return Infinity; if (extent < -153) return 0;
|
|
508
|
+
const out: number[] = []; for (let i = start; i < digits.length; i++) nscMdMultiply(out, 16, digits[i]!);
|
|
509
|
+
for (let i = 0; i < Math.abs(exponent); i++) nscMdMultiply(out, exponent < 0 ? 5 : 2, 0);
|
|
510
|
+
decimal = nscMdNormalize(out, Math.min(0, exponent));
|
|
511
|
+
} else {
|
|
512
|
+
const out: number[] = []; for (let i = digits.length - 1; i >= 0; i--) out.push(digits[i]!); decimal = nscMdNormalize(out, exponent - fractional);
|
|
513
|
+
}
|
|
514
|
+
if (decimal.digits.length === 0) return 0;
|
|
515
|
+
let lo = 0, hi = 0x7f800000;
|
|
516
|
+
while (lo < hi) { const mid = Math.floor((lo + hi) / 2), comparison = nscMdCompare(decimal, nscMdMidpoint(mid)); if (comparison > 0 || comparison === 0 && mid % 2 !== 0) lo = mid + 1; else hi = mid; }
|
|
517
|
+
const bytes = new Uint8Array(4), wire = new DataView(bytes.buffer); wire.setUint32(0, lo, true); return wire.getFloat32(0, true);
|
|
518
|
+
}
|
|
519
|
+
interface NscMdNodeOptions { grow: number; padding: number; gap: number; width: number; height: number; alignment: number; main: number; flags: number; ordinal: number; lower: number; upper: number; language: number; label: Uint8Array; link: Uint8Array }
|
|
520
|
+
function nscMdOptions(): NscMdNodeOptions { return { grow: 0, padding: 0, gap: 0, width: 0, height: 0, alignment: 0, main: -1, flags: 0, ordinal: -1, lower: 0, upper: 0, language: 0, label: new Uint8Array(0), link: new Uint8Array(0) }; }
|
|
521
|
+
interface NscMdNode { operation: number; subtype: number; options: NscMdNodeOptions; children: number[]; spans: NscMdSpan[]; text: Uint8Array }
|
|
522
|
+
class NscMdWriter {
|
|
523
|
+
parts: Uint8Array[] = []; length = 0;
|
|
524
|
+
add(bytes: Uint8Array): void { this.parts.push(bytes); this.length += bytes.length; }
|
|
525
|
+
u32(value: number): void { const bytes = new Uint8Array(4); new DataView(bytes.buffer).setUint32(0, value, true); this.add(bytes); }
|
|
526
|
+
finish(): Uint8Array { const out = new Uint8Array(this.length); let at = 0; for (const part of this.parts) { out.set(part, at); at += part.length; } return out; }
|
|
527
|
+
}
|
|
528
|
+
function nscMdEncode(nodes: NscMdNode[], root: number): Uint8Array {
|
|
529
|
+
const out = new NscMdWriter(); out.u32(1); out.u32(nodes.length); out.u32(root); out.u32(0);
|
|
530
|
+
for (const node of nodes) {
|
|
531
|
+
const o = node.options, bytes = new Uint8Array(64), wire = new DataView(bytes.buffer);
|
|
532
|
+
bytes[0] = node.operation; bytes[1] = node.subtype; bytes[2] = o.alignment; bytes[3] = o.flags;
|
|
533
|
+
wire.setFloat32(4, o.grow, true); wire.setFloat32(8, o.padding, true); wire.setFloat32(12, o.gap, true); wire.setFloat32(16, o.width, true); wire.setFloat32(20, o.height, true);
|
|
534
|
+
wire.setUint32(24, o.lower, true); wire.setUint32(28, o.upper, true); wire.setInt32(32, o.ordinal, true);
|
|
535
|
+
wire.setUint32(36, node.children.length, true); wire.setUint32(40, node.spans.length, true); wire.setUint32(44, node.text.length, true); wire.setUint32(48, o.label.length, true); wire.setUint32(52, o.link.length, true); wire.setUint32(56, o.language, true); wire.setInt32(60, o.main, true); out.add(bytes);
|
|
536
|
+
for (const child of node.children) out.u32(child);
|
|
537
|
+
for (const span of node.spans) { const header = new Uint8Array(16), d = new DataView(header.buffer); d.setUint32(0, span.flags, true); d.setFloat32(4, span.scale, true); d.setUint32(8, span.text.length, true); d.setUint32(12, span.link.length, true); out.add(header); out.add(span.text); out.add(span.link); }
|
|
538
|
+
out.add(node.text); out.add(o.label); out.add(o.link);
|
|
539
|
+
} return out.finish();
|
|
540
|
+
}
|
|
541
|
+
interface NscMdScope { name: Uint8Array; previous: number; list: number; ordinal: number }
|
|
542
|
+
interface NscMdPresentation { alignment: number; depth: number; overflow: number }
|
|
543
|
+
interface NscMdResolved { mapping: NscMdImage; consumed: number; alt: Uint8Array; link: Uint8Array; width: number; height: number }
|
|
544
|
+
function nscMdLanguage(opening: Uint8Array): number {
|
|
545
|
+
const trimmed = nscMdTrim(opening, " \t", true, true); if (trimmed.length <= 3) return 0;
|
|
546
|
+
let info = nscMdTrim(trimmed.subarray(3), " \t", true, true); if (nscMdStarts(info, 0, "{.", false)) info = info.subarray(2);
|
|
547
|
+
let end = 0; while (end < info.length && (nscMdAlpha(info[end]!) || nscMdDigit(info[end]!) || info[end] === 95 || info[end] === 45 || info[end] === 43 || info[end] === 35)) end++;
|
|
548
|
+
const name = info.subarray(0, end), aliases = ["plain text", "zig", "js mjs javascript", "ts typescript", "json jsonc", "yaml yml", "sh bash zsh shell", "py python", "rs rust", "c h cc cpp c++ cs csharp java kotlin swift", "go golang", "html xml svg", "css scss less", "sql", "jsx", "tsx", "md markdown"];
|
|
549
|
+
for (let language = 0; language < aliases.length; language++) for (const alias of aliases[language]!.split(" ")) if (nscMdEqual(name, nscMdBytes(alias), true)) return language;
|
|
550
|
+
return 0;
|
|
551
|
+
}
|
|
552
|
+
function nscMdCollapse(raw: Uint8Array): Uint8Array {
|
|
553
|
+
const text = nscMdTrim(raw, " \t\r\n", true, true), out = new Uint8Array(text.length); let len = 0;
|
|
554
|
+
for (let i = 0; i < text.length; i++) { if (text[i] === 13) { if (i + 1 < text.length && text[i + 1] === 10) i++; out[len++] = 32; } else out[len++] = text[i] === 10 ? 32 : text[i]!; } return out.subarray(0, len);
|
|
555
|
+
}
|
|
556
|
+
function nscMdUnescape(text: Uint8Array): Uint8Array { const out = new Uint8Array(text.length); let len = 0; for (let i = 0; i < text.length; i++) { if (text[i] === 92 && i + 1 < text.length && text[i + 1] === 124) continue; out[len++] = text[i]!; } return out.subarray(0, len); }
|
|
557
|
+
class NscMdBuilder {
|
|
558
|
+
nodes: NscMdNode[] = []; details = 0; alignment = -1; scopes: NscMdScope[] = []; scopeDepth = 0; overflow = 0; blockDepth = 0;
|
|
559
|
+
expanded: boolean[]; issue: Uint8Array | null; images: NscMdImage[];
|
|
560
|
+
constructor(expanded: boolean[], issue: Uint8Array | null, images: NscMdImage[]) { this.expanded = expanded; this.issue = issue; this.images = images; }
|
|
561
|
+
add(operation: number, subtype: number, options: NscMdNodeOptions, children: number[], spans: NscMdSpan[], text: Uint8Array): number {
|
|
562
|
+
const index = this.nodes.length; this.nodes.push({ operation, subtype, options, children, spans, text }); return index;
|
|
563
|
+
}
|
|
564
|
+
container(row: boolean, gap: number, padding: number, grow: number, children: number[]): number { const o = nscMdOptions(); o.gap = gap; o.padding = padding; o.grow = grow; return this.add(row ? 1 : 0, 0, o, children, [], new Uint8Array(0)); }
|
|
565
|
+
el(kind: number, options: NscMdNodeOptions, children: number[]): number { return this.add(5, kind, options, children, [], new Uint8Array(0)); }
|
|
566
|
+
text(text: Uint8Array): number { return this.add(3, 0, nscMdOptions(), [], [], text); }
|
|
567
|
+
plainText(node: number): Uint8Array { const item = this.nodes[node]!; if (item.operation !== 2) return item.text; const out = new NscMdWriter(); for (const span of item.spans) out.add(span.text); return out.finish(); }
|
|
568
|
+
separator(): number { return this.add(4, 0, nscMdOptions(), [], [], new Uint8Array(0)); }
|
|
569
|
+
quoteRail(): number { const o = nscMdOptions(); o.width = 3; return this.el(0, o, []); }
|
|
570
|
+
code(source: Uint8Array, language: number): number { const o = nscMdOptions(); o.language = language; return this.add(7, 0, o, [], [], source); }
|
|
571
|
+
resolved(text: Uint8Array): NscMdResolved | null {
|
|
572
|
+
const image = nscMdLeading(text); if (image === null) return null; const source = nscMdDecode(image.source);
|
|
573
|
+
let mapping: NscMdImage | null = null;
|
|
574
|
+
for (let i = 0; i < Math.min(this.images.length, 16); i++) { const candidate = this.images[i]!; if (candidate.lower === 0 && candidate.upper === 0 || !Number.isFinite(candidate.width) || candidate.width <= 0 || !Number.isFinite(candidate.height) || candidate.height <= 0) continue; if (nscMdEqual(source, candidate.source, false)) { mapping = candidate; break; } }
|
|
575
|
+
if (mapping === null) return null;
|
|
576
|
+
let width = image.width === null ? mapping.width : image.width, height = image.height === null ? mapping.height : image.height;
|
|
577
|
+
if (image.width !== null && image.height === null) height = Math.fround(Math.fround(width * mapping.height) / mapping.width);
|
|
578
|
+
else if (image.width === null && image.height !== null) width = Math.fround(Math.fround(height * mapping.width) / mapping.height);
|
|
579
|
+
const scale = Math.min(1, Math.min(Math.fround(512 / width), Math.fround(512 / height)));
|
|
580
|
+
return { mapping, consumed: image.consumed, alt: nscMdDecode(image.alt), link: nscMdDecode(image.link), width: Math.fround(width * scale), height: Math.fround(height * scale) };
|
|
581
|
+
}
|
|
582
|
+
image(resolved: NscMdResolved): number { const o = nscMdOptions(); o.width = resolved.width; o.height = resolved.height; o.lower = resolved.mapping.lower; o.upper = resolved.mapping.upper; o.label = resolved.alt; o.link = resolved.link; return this.add(8, 0, o, [], [], new Uint8Array(0)); }
|
|
583
|
+
paragraph(text: Uint8Array, base: NscMdSpan, options: NscMdNodeOptions): number {
|
|
584
|
+
const resolved = this.resolved(text);
|
|
585
|
+
if (resolved !== null) {
|
|
586
|
+
const children = [this.image(resolved)], suffix = nscMdTrim(text.subarray(resolved.consumed), " \t", true, false);
|
|
587
|
+
if (suffix.length > 0) { const o = nscMdOptions(); o.grow = 1; o.alignment = options.alignment; o.flags = options.flags & 1; children.push(this.add(2, 0, o, [], nscMdInline(suffix, base, this.issue), new Uint8Array(0))); }
|
|
588
|
+
const o = nscMdOptions(); o.grow = options.grow; o.padding = options.padding; o.gap = 6; o.main = options.alignment; o.flags = 8; return this.add(1, 0, o, children, [], new Uint8Array(0));
|
|
589
|
+
}
|
|
590
|
+
return this.add(2, 0, options, [], nscMdInline(text, base, this.issue), new Uint8Array(0));
|
|
591
|
+
}
|
|
592
|
+
paragraphOptions(text: Uint8Array, grow: number, muted: boolean): number { const o = nscMdOptions(); o.grow = grow; o.flags = muted ? 1 : 0; o.alignment = Math.max(0, this.alignment); return this.paragraph(text, nscMdSpan(), o); }
|
|
593
|
+
heading(level: number, text: Uint8Array, alignment: number): number { const base = nscMdSpan(), o = nscMdOptions(); base.flags = 1; base.scale = level === 1 ? 2 : level === 2 ? 1.5 : 1.25; o.alignment = alignment; return this.paragraph(text, base, o); }
|
|
594
|
+
snapshot(): NscMdPresentation { return { alignment: this.alignment, depth: this.scopeDepth, overflow: this.overflow }; }
|
|
595
|
+
restore(state: NscMdPresentation): void { this.alignment = state.alignment; this.scopeDepth = state.depth; this.overflow = state.overflow; }
|
|
596
|
+
open(tag: NscMdTag): void {
|
|
597
|
+
if (!nscMdStructural(tag) || tag.selfClosing) return;
|
|
598
|
+
if (this.scopeDepth >= 16) { this.overflow++; return; }
|
|
599
|
+
const scope: NscMdScope = { name: tag.name, previous: this.alignment, list: tag.kind === 17 ? nscMdName(tag, "ol") ? 1 : nscMdName(tag, "dl") ? 2 : 0 : -1, ordinal: 1 };
|
|
600
|
+
this.scopes[this.scopeDepth++] = scope;
|
|
601
|
+
const alignment = nscMdAlignment(tag); if (alignment >= 0) this.alignment = alignment;
|
|
602
|
+
}
|
|
603
|
+
close(tag: NscMdTag): boolean {
|
|
604
|
+
if (!nscMdStructural(tag)) return true;
|
|
605
|
+
if (this.overflow > 0) { this.overflow--; return true; }
|
|
606
|
+
if (this.scopeDepth === 0) return false;
|
|
607
|
+
const scope = this.scopes[this.scopeDepth - 1]!; if (!nscMdEqual(scope.name, tag.name, true)) return false;
|
|
608
|
+
this.scopeDepth--; this.alignment = scope.previous; return true;
|
|
609
|
+
}
|
|
610
|
+
scopeName(): Uint8Array | null { return this.overflow > 0 || this.scopeDepth === 0 ? null : this.scopes[this.scopeDepth - 1]!.name; }
|
|
611
|
+
blocks(lines: NscMdLines, details: boolean): number[] {
|
|
612
|
+
const nodes: number[] = [];
|
|
613
|
+
for (;;) {
|
|
614
|
+
const line = lines.peek(); if (line === null) break; const trimmed = nscMdTrim(line, " \t", true, true);
|
|
615
|
+
if (details && nscMdStarts(trimmed, 0, "</details>", true)) { lines.next(); break; }
|
|
616
|
+
const name = this.scopeName(), close = name !== null || this.overflow > 0 ? nscMdUnbalanced(line, name) : null;
|
|
617
|
+
if (close !== null) {
|
|
618
|
+
lines.next(); const fragments = new NscMdLines(line.subarray(0, close.start));
|
|
619
|
+
for (;;) { const fragment = fragments.peek(); if (fragment === null) break; if (nscMdTrim(fragment, " \t", true, true).length === 0) { fragments.next(); continue; } const node = this.block(fragments); if (node < 0) continue; if (nodes.length >= 64) break; nodes.push(node); }
|
|
620
|
+
const suffix = line.subarray(close.end); if (suffix.length > 0) lines.prepend(suffix); this.close(close.tag); continue;
|
|
621
|
+
}
|
|
622
|
+
if (trimmed.length === 0) { lines.next(); continue; }
|
|
623
|
+
const node = this.block(lines); if (node < 0) continue; if (nodes.length >= 64) break; nodes.push(node);
|
|
624
|
+
} return nodes;
|
|
625
|
+
}
|
|
626
|
+
block(lines: NscMdLines): number {
|
|
627
|
+
const line = lines.peek(); if (line === null) return -1; const trimmed = nscMdTrim(line, " \t", true, true);
|
|
628
|
+
if (nscMdReference(line)) { lines.next(); return -1; }
|
|
629
|
+
if (nscMdStarts(trimmed, 0, "```", false)) {
|
|
630
|
+
const opening = lines.next()!; const start = lines.index; let end = start;
|
|
631
|
+
for (;;) { const part = lines.next(); if (part === null || nscMdStarts(nscMdTrim(part, " \t", true, true), 0, "```", false)) break; end = lines.index; }
|
|
632
|
+
return this.code(nscMdTrim(lines.source.subarray(start, Math.min(end, lines.source.length)), "\n", false, true), nscMdLanguage(opening));
|
|
633
|
+
}
|
|
634
|
+
const level = nscMdHeading(trimmed); if (level > 0) { lines.next(); return this.heading(level, nscMdTrim(trimmed.subarray(level), " \t#", true, true), Math.max(0, this.alignment)); }
|
|
635
|
+
if (nscMdRule(trimmed)) { lines.next(); return this.separator(); }
|
|
636
|
+
if (trimmed[0] === 62) { const text = this.joined(lines, true); if (text.length === 0) return -1; return this.container(true, 10, 0, 0, [this.quoteRail(), this.paragraphOptions(text, 1, true)]); }
|
|
637
|
+
if (nscMdMarker(line) !== null) return this.list(lines, 0, 0);
|
|
638
|
+
if (nscMdStarts(trimmed, 0, "<details", true)) return this.detail(lines);
|
|
639
|
+
const html = this.htmlBlock(lines, trimmed); if (html !== -2) return html;
|
|
640
|
+
if (nscMdTableStart(lines)) return this.table(lines);
|
|
641
|
+
const text = this.joined(lines, false); return text.length === 0 ? -1 : this.paragraphOptions(text, 0, false);
|
|
642
|
+
}
|
|
643
|
+
joinPiece(lines: NscMdLines, quote: boolean, length: number, opaque: NscMdOpaque): Uint8Array | null {
|
|
644
|
+
const line = lines.peek(); if (line === null) return null; const trimmed = nscMdTrim(line, " \t", true, true);
|
|
645
|
+
if (quote) { if (trimmed.length === 0 || trimmed[0] !== 62) return null; let inner = trimmed.subarray(1); if (inner.length > 0 && inner[0] === 32) inner = inner.subarray(1); return nscMdTrim(inner, " \t", true, true); }
|
|
646
|
+
if (!opaque.active()) {
|
|
647
|
+
if (trimmed.length === 0) return null;
|
|
648
|
+
if (length > 0) { const name = this.scopeName(), closes = name !== null || this.overflow > 0 ? nscMdUnbalanced(line, name) !== null : false; if (closes || nscMdNewBlock(line) || nscMdTableStart(lines)) return null; }
|
|
649
|
+
}
|
|
650
|
+
const scan = new NscMdTagScan(opaque); while (scan.next(line) !== null) {} return trimmed;
|
|
651
|
+
}
|
|
652
|
+
joined(lines: NscMdLines, quote: boolean): Uint8Array {
|
|
653
|
+
const probe = lines.copy(), opaque = new NscMdOpaque(); let total = 0;
|
|
654
|
+
for (;;) { const piece = this.joinPiece(probe, quote, total, opaque); if (piece === null) break; probe.next(); if (total > 0) total++; total += piece.length; }
|
|
655
|
+
if (total === 0) { lines.restore(probe); return new Uint8Array(0); }
|
|
656
|
+
const out = new Uint8Array(Math.min(total, 8192)), state = new NscMdOpaque(); let len = 0, joined = 0;
|
|
657
|
+
for (;;) { const piece = this.joinPiece(lines, quote, joined, state); if (piece === null) break; lines.next(); if (joined > 0 && len < out.length) out[len++] = 32; if (joined > 0) joined++; joined += piece.length; const take = Math.min(piece.length, out.length - len); out.set(piece.subarray(0, take), len); len += take; } return out.subarray(0, len);
|
|
658
|
+
}
|
|
659
|
+
list(lines: NscMdLines, indent: number, depth: number): number {
|
|
660
|
+
const items: number[] = [];
|
|
661
|
+
for (;;) {
|
|
662
|
+
const line = lines.peek(); if (line === null) break; const marker = nscMdMarker(line); if (marker === null || marker.indent < indent) break;
|
|
663
|
+
if (marker.indent > indent) { if (items.length === 0 || depth + 1 >= 4) { lines.next(); continue; } const nested = this.list(lines, marker.indent, depth + 1); if (nested >= 0) items[items.length - 1] = this.container(false, 4, 0, 0, [items[items.length - 1]!, nested]); continue; }
|
|
664
|
+
lines.next(); if (items.length >= 64) continue; items.push(this.listItem(marker, depth));
|
|
665
|
+
} return items.length === 0 ? -1 : this.container(false, 4, 0, 0, items);
|
|
666
|
+
}
|
|
667
|
+
listItem(marker: NscMdMarker, depth: number): number {
|
|
668
|
+
const content = this.paragraphOptions(marker.content, 1, false); let lead: number;
|
|
669
|
+
if (marker.kind === 2) { const o = nscMdOptions(); o.flags = marker.checked ? 16 : 0; o.label = marker.content; lead = this.add(6, 0, o, [], [], new Uint8Array(0)); }
|
|
670
|
+
else lead = this.text(marker.kind === 0 ? nscMdBytes("•") : marker.label);
|
|
671
|
+
const top = this.container(false, 0, 0, 0, [lead]); if (depth === 0) return this.container(true, 8, 0, 0, [top, content]);
|
|
672
|
+
const o = nscMdOptions(); o.width = depth * 16; const spacer = this.el(1, o, []); return this.container(true, 8, 0, 0, [spacer, top, content]);
|
|
673
|
+
}
|
|
674
|
+
collectHtml(lines: NscMdLines, name: Uint8Array): { content: Uint8Array; closed: boolean } {
|
|
675
|
+
const probe = lines.copy(), opaque = new NscMdOpaque(); let depth = 1, total = 0, closed = false;
|
|
676
|
+
for (;;) { const line = probe.next(); if (line === null) break; const scan = new NscMdTagScan(opaque); let end = line.length, closing = -1;
|
|
677
|
+
for (;;) { const match = scan.next(line); if (match === null) break; if (!nscMdEqual(match.tag.name, name, true)) continue; if (!match.tag.closing) { if (!match.tag.selfClosing) depth++; } else { if (depth > 0) depth--; if (depth === 0) { end = match.start; closing = match.end; break; } } }
|
|
678
|
+
total = Math.min(8192, total + end); if (closing >= 0) { const suffix = line.subarray(closing); if (suffix.length > 0) probe.prepend(suffix); closed = true; break; } total = Math.min(8192, total + 1);
|
|
679
|
+
}
|
|
680
|
+
if (!closed) return { content: new Uint8Array(0), closed: false };
|
|
681
|
+
const out = new Uint8Array(total), state = new NscMdOpaque(); depth = 1; let len = 0;
|
|
682
|
+
for (;;) { const line = lines.next(); if (line === null) break; const scan = new NscMdTagScan(state); let end = line.length, closing = -1;
|
|
683
|
+
for (;;) { const match = scan.next(line); if (match === null) break; if (!nscMdEqual(match.tag.name, name, true)) continue; if (!match.tag.closing) { if (!match.tag.selfClosing) depth++; } else { if (depth > 0) depth--; if (depth === 0) { end = match.start; closing = match.end; break; } } }
|
|
684
|
+
const take = Math.min(end, out.length - len); out.set(line.subarray(0, take), len); len += take;
|
|
685
|
+
if (closing >= 0) { const suffix = line.subarray(closing); if (suffix.length > 0) lines.prepend(suffix); break; } if (len < out.length) out[len++] = 10;
|
|
686
|
+
} return { content: out.subarray(0, len), closed: true };
|
|
687
|
+
}
|
|
688
|
+
htmlParagraph(content: Uint8Array, alignment: number, grow: number, bold: boolean): number { const o = nscMdOptions(), base = nscMdSpan(); o.grow = grow; o.alignment = alignment; base.flags = bold ? 1 : 0; return this.paragraph(content, base, o); }
|
|
689
|
+
htmlItem(tag: NscMdTag, content: Uint8Array): number {
|
|
690
|
+
if (nscMdName(tag, "dt")) return this.htmlParagraph(content, Math.max(0, this.alignment), 0, true);
|
|
691
|
+
if (nscMdName(tag, "dd")) { const o = nscMdOptions(); o.width = 16; return this.container(true, 8, 0, 0, [this.el(1, o, []), this.htmlParagraph(content, Math.max(0, this.alignment), 1, false)]); }
|
|
692
|
+
const marker: NscMdMarker = { kind: 0, indent: 0, label: new Uint8Array(0), checked: false, content };
|
|
693
|
+
if (this.overflow === 0) for (let i = this.scopeDepth - 1; i >= 0; i--) { const scope = this.scopes[i]!; if (scope.list < 0) continue; if (scope.list === 1) { marker.kind = 1; marker.label = nscMdBytes(String(scope.ordinal) + "."); scope.ordinal++; } break; }
|
|
694
|
+
return this.listItem(marker, 0);
|
|
695
|
+
}
|
|
696
|
+
htmlBlock(lines: NscMdLines, trimmed: Uint8Array): number {
|
|
697
|
+
if (nscMdStarts(trimmed, 0, "<!--", false)) {
|
|
698
|
+
const probe = lines.copy(), first = probe.next()!; const opening = nscMdFind(first, 0, "<!--", false); let close = nscMdFind(first, opening + 4, "-->", false), line = first;
|
|
699
|
+
while (close < 0) { const next = probe.next(); if (next === null) return -2; line = next; close = nscMdFind(line, 0, "-->", false); }
|
|
700
|
+
const suffix = line.subarray(close + 3); if (suffix.length > 0) probe.prepend(suffix); lines.restore(probe); return -1;
|
|
701
|
+
}
|
|
702
|
+
const opening = nscMdTag(trimmed, 0, true); if (opening === null) return -2;
|
|
703
|
+
if (opening.closing || !nscMdBlock(opening)) { if (opening.consumed === trimmed.length && opening.closing && nscMdStructural(opening)) { if (!this.close(opening)) return -2; lines.next(); return -1; } return -2; }
|
|
704
|
+
if (opening.kind === 13) { this.consumeOpening(lines, trimmed, opening); return this.separator(); }
|
|
705
|
+
if (opening.kind === 15 || opening.kind === 16 || opening.kind === 12 || opening.kind === 14 || opening.kind === 18) {
|
|
706
|
+
const before = lines.copy(); this.consumeOpening(lines, trimmed, opening); if (opening.selfClosing) return -1;
|
|
707
|
+
if (opening.kind === 15 && this.blockDepth >= 8) { this.skipHtml(lines, opening.name); return -1; }
|
|
708
|
+
const element = this.collectHtml(lines, opening.name); if (!element.closed) { lines.restore(before); return -2; }
|
|
709
|
+
if (opening.kind === 15) {
|
|
710
|
+
const state = this.snapshot(), align = nscMdAlignment(opening); if (align >= 0) this.alignment = align; this.blockDepth++;
|
|
711
|
+
const blocks = this.blocks(new NscMdLines(element.content), false); this.blockDepth--; const node = this.container(true, 10, 0, 0, [this.quoteRail(), this.container(false, 12, 0, 1, blocks)]); this.restore(state); return node;
|
|
712
|
+
}
|
|
713
|
+
if (opening.kind === 16) { let content = nscMdTrim(element.content, "\r\n", true, true); const tag = nscMdTag(content, 0, true); if (tag !== null && tag.kind === 4 && !tag.closing) { const single = nscMdSingle(content, tag); if (single !== null) content = single; } return this.code(nscMdDecode(nscMdTrim(content, "\r\n", true, true)), 0); }
|
|
714
|
+
const content = nscMdCollapse(element.content), align = nscMdAlignment(opening), alignment = align >= 0 ? align : Math.max(0, this.alignment);
|
|
715
|
+
return opening.kind === 12 ? this.heading(opening.level, content, alignment) : opening.kind === 14 ? this.htmlParagraph(content, alignment, 0, false) : this.htmlItem(opening, content);
|
|
716
|
+
}
|
|
717
|
+
const single = nscMdSingle(trimmed, opening);
|
|
718
|
+
if (single !== null) {
|
|
719
|
+
const align = nscMdAlignment(opening), alignment = align >= 0 ? align : Math.max(0, this.alignment);
|
|
720
|
+
if (opening.kind === 17) { lines.next(); const state = this.snapshot(); this.open(opening); const blocks = this.blocks(new NscMdLines(single), false); const node = this.container(false, 4, 0, 0, blocks); this.restore(state); return node; }
|
|
721
|
+
if (opening.kind === 23 || opening.kind >= 19 && opening.kind <= 22) { lines.next(); return this.htmlParagraph(single, alignment, 0, false); }
|
|
722
|
+
return -2;
|
|
723
|
+
}
|
|
724
|
+
if (opening.consumed === trimmed.length && nscMdStructural(opening)) { lines.next(); if (!opening.selfClosing) this.open(opening); return -1; }
|
|
725
|
+
return -2;
|
|
726
|
+
}
|
|
727
|
+
consumeOpening(lines: NscMdLines, trimmed: Uint8Array, tag: NscMdTag): void { lines.next(); const suffix = trimmed.subarray(tag.consumed); if (suffix.length > 0) lines.prepend(suffix); }
|
|
728
|
+
skipHtml(lines: NscMdLines, name: Uint8Array): void {
|
|
729
|
+
const opaque = new NscMdOpaque(); let depth = 1;
|
|
730
|
+
for (;;) { const line = lines.next(); if (line === null) return; const scan = new NscMdTagScan(opaque);
|
|
731
|
+
for (;;) { const match = scan.next(line); if (match === null) break; if (!nscMdEqual(match.tag.name, name, true)) continue; if (!match.tag.closing) { if (!match.tag.selfClosing) depth++; } else { if (depth > 0) depth--; if (depth === 0) { const suffix = line.subarray(match.end); if (suffix.length > 0) lines.prepend(suffix); return; } } }
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
skipDetails(lines: NscMdLines): void { let depth = 1; for (;;) { const line = lines.next(); if (line === null) return; const trimmed = nscMdTrim(line, " \t", true, true); if (nscMdStarts(trimmed, 0, "<details", true)) depth++; if (nscMdStarts(trimmed, 0, "</details>", true)) { depth--; if (depth === 0) return; } } }
|
|
735
|
+
detail(lines: NscMdLines): number {
|
|
736
|
+
lines.next(); const ordinal = this.details; if (ordinal >= 16) { this.skipDetails(lines); return -1; } this.details++;
|
|
737
|
+
const expanded = ordinal < this.expanded.length && this.expanded[ordinal]!; let summary = nscMdBytes("Details"); const line = lines.peek();
|
|
738
|
+
if (line !== null) { const trimmed = nscMdTrim(line, " \t", true, true); if (nscMdStarts(trimmed, 0, "<summary>", true)) { lines.next(); summary = trimmed.subarray(9); const close = nscMdFind(summary, 0, "</summary>", true); if (close >= 0) summary = summary.subarray(0, close); summary = nscMdTrim(summary, " \t", true, true); } }
|
|
739
|
+
const indicator = nscMdBytes(expanded ? "▾" : "▸"), summaryNode = this.paragraphOptions(summary, 1, false), label = nscMdConcat(nscMdConcat(indicator, nscMdBytes(" ")), this.plainText(summaryNode));
|
|
740
|
+
const o = nscMdOptions(); o.gap = 6; o.ordinal = ordinal; o.label = label; o.flags = expanded ? 4 : 0;
|
|
741
|
+
const header = this.el(2, o, [this.text(indicator), summaryNode]);
|
|
742
|
+
if (!expanded) { this.skipDetails(lines); return this.container(false, 4, 0, 0, [header]); }
|
|
743
|
+
const blocks = this.blocks(lines, true), body = this.container(false, 12, 8, 0, blocks); return this.container(false, 4, 0, 0, [header, body]);
|
|
744
|
+
}
|
|
745
|
+
table(lines: NscMdLines): number {
|
|
746
|
+
const header = nscMdTable(lines.next()!)!, alignments = nscMdDelimiters(lines.next()!)!, rows = [this.tableRow(header, alignments, true)];
|
|
747
|
+
for (;;) { const line = lines.peek(); if (line === null) break; const trimmed = nscMdTrim(line, " \t", true, true); if (trimmed.length === 0 || nscMdFindByte(trimmed, 0, 124) < 0) break; const row = nscMdTable(line); if (row === null) break; lines.next(); if (rows.length >= 64) continue; rows.push(this.tableRow(row, alignments, false)); }
|
|
748
|
+
return this.el(3, nscMdOptions(), rows);
|
|
749
|
+
}
|
|
750
|
+
tableRow(row: Uint8Array[], alignments: number[], header: boolean): number { const cells: number[] = []; for (let i = 0; i < alignments.length; i++) cells.push(this.tableCell(i < row.length ? row[i]! : new Uint8Array(0), alignments[i]!, header)); return this.el(4, nscMdOptions(), cells); }
|
|
751
|
+
tableCell(content: Uint8Array, alignment: number, header: boolean): number {
|
|
752
|
+
const text = nscMdUnescape(content), base = nscMdSpan(); base.flags = header ? 1 : 0; const resolved = this.resolved(text), o = nscMdOptions(); o.grow = 1; o.padding = 8; o.alignment = alignment;
|
|
753
|
+
if (resolved !== null) { const children = [this.image(resolved)], suffix = nscMdTrim(text.subarray(resolved.consumed), " \t", true, false);
|
|
754
|
+
if (suffix.length > 0) { const p = nscMdOptions(); p.grow = 1; p.alignment = alignment; children.push(this.add(2, 0, p, [], nscMdInline(suffix, base, this.issue), new Uint8Array(0))); }
|
|
755
|
+
o.gap = 6; o.main = alignment; o.flags = 8; return this.el(5, o, children);
|
|
756
|
+
}
|
|
757
|
+
o.flags = 2; return this.add(2, 0, o, [], nscMdInline(text, base, this.issue), new Uint8Array(0));
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
class NscMdDiscoveryState {
|
|
761
|
+
opaque = new NscMdOpaque(); code: Uint8Array[] = []; overflow = 0;
|
|
762
|
+
active(): boolean { return this.opaque.active() || this.code.length > 0 || this.overflow > 0; }
|
|
763
|
+
visible(line: Uint8Array): Uint8Array {
|
|
764
|
+
let first = this.active() ? 0 : -1, cursor = 0;
|
|
765
|
+
while (cursor < line.length) {
|
|
766
|
+
if (this.opaque.comment) { if (first < 0) first = cursor; const close = nscMdFind(line, cursor, "-->", false); if (close < 0) return line.subarray(0, first < 0 ? 0 : first); this.opaque.comment = false; cursor = close + 3; continue; }
|
|
767
|
+
const start = nscMdFindByte(line, cursor, 60); if (start < 0) break;
|
|
768
|
+
if (nscMdStarts(line, start, "<!--", false)) { if (first < 0) first = start; const close = nscMdFind(line, start + 4, "-->", false); if (close < 0) { this.opaque.comment = true; break; } cursor = close + 3; continue; }
|
|
769
|
+
const tag = nscMdTag(line, start, false); if (tag === null) { cursor = start + 1; continue; } cursor = start + tag.consumed;
|
|
770
|
+
if (this.opaque.depth > 0) { if (first < 0) first = start; if (!nscMdEqual(tag.name, this.opaque.name, true)) continue; if (tag.closing) { this.opaque.depth--; if (this.opaque.depth === 0) this.opaque.name = new Uint8Array(0); } else if (!tag.selfClosing) this.opaque.depth++; continue; }
|
|
771
|
+
if (tag.kind < 0 && !nscMdName(tag, "details") && !nscMdName(tag, "summary")) { if (first < 0) first = start; if (!tag.closing && !tag.selfClosing && !nscMdVoid(tag.name)) { this.opaque.name = tag.name; this.opaque.depth = 1; } continue; }
|
|
772
|
+
if (tag.kind !== 4 && tag.kind !== 16) continue;
|
|
773
|
+
if (first < 0) first = start; if (tag.selfClosing) continue;
|
|
774
|
+
if (tag.closing) { if (this.overflow > 0) this.overflow--; else if (this.code.length > 0 && nscMdEqual(this.code[this.code.length - 1]!, tag.name, true)) this.code.pop(); }
|
|
775
|
+
else if (this.code.length < 8) this.code.push(tag.name); else this.overflow++;
|
|
776
|
+
} return line.subarray(0, first < 0 ? line.length : first);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
class NscMdDiscovery {
|
|
780
|
+
output: Uint8Array[] = []; capacity: number; paragraph = false; quote = false;
|
|
781
|
+
constructor(capacity: number) { this.capacity = capacity; }
|
|
782
|
+
append(text: Uint8Array): void {
|
|
783
|
+
if (this.output.length >= this.capacity) return; const image = nscMdLeading(text); if (image === null || image.source.length === 0) return;
|
|
784
|
+
const source = nscMdDecode(image.source); if (source.length > 2048) return;
|
|
785
|
+
for (const existing of this.output) if (nscMdEqual(existing, source, false)) return; this.output.push(source);
|
|
786
|
+
}
|
|
787
|
+
html(trimmed: Uint8Array): boolean {
|
|
788
|
+
if (nscMdStarts(trimmed, 0, "<summary>", true)) { let content = trimmed.subarray(9); const close = nscMdFind(content, 0, "</summary>", true); if (close >= 0) content = content.subarray(0, close); this.append(nscMdTrim(content, " \t", true, true)); this.paragraph = false; return true; }
|
|
789
|
+
const syntax = nscMdTag(trimmed, 0, false); if (syntax !== null && (nscMdName(syntax, "details") || nscMdName(syntax, "summary"))) { this.paragraph = false; return true; }
|
|
790
|
+
const opening = nscMdTag(trimmed, 0, true); if (opening === null || !nscMdBlock(opening)) return false;
|
|
791
|
+
if (opening.kind === 13 || opening.selfClosing) { this.paragraph = false; return true; }
|
|
792
|
+
if (opening.closing) { const suffix = nscMdTrim(trimmed.subarray(opening.consumed), " \t", true, false); if (suffix.length > 0) this.append(suffix); this.paragraph = suffix.length > 0; return true; }
|
|
793
|
+
const single = nscMdSingle(trimmed, opening); if (single !== null) { this.append(nscMdTrim(single, " \t", true, true)); this.paragraph = false; return true; }
|
|
794
|
+
const suffix = nscMdTrim(trimmed.subarray(opening.consumed), " \t", true, false); if (suffix.length > 0) this.append(suffix); this.paragraph = suffix.length > 0; return true;
|
|
795
|
+
}
|
|
796
|
+
collect(source: Uint8Array): Uint8Array[] {
|
|
797
|
+
const lines = new NscMdLines(source), html = new NscMdDiscoveryState(); let fence = false;
|
|
798
|
+
for (;;) {
|
|
799
|
+
const line = lines.next(); if (line === null) break; const trimmed = nscMdTrim(line, " \t", true, true);
|
|
800
|
+
if (html.active()) { html.visible(line); continue; }
|
|
801
|
+
if (nscMdStarts(trimmed, 0, "```", false)) { fence = !fence; this.paragraph = false; this.quote = false; continue; } if (fence) continue;
|
|
802
|
+
const visible = html.visible(line), vt = nscMdTrim(visible, " \t", true, true);
|
|
803
|
+
if (vt.length === 0) {
|
|
804
|
+
if (trimmed.length === 0) { this.paragraph = false; this.quote = false; }
|
|
805
|
+
else { const tag = nscMdTag(trimmed, 0, true); this.paragraph = !(nscMdStarts(trimmed, 0, "<!--", false) || tag !== null && !tag.closing && tag.kind === 16); }
|
|
806
|
+
continue;
|
|
807
|
+
}
|
|
808
|
+
if (nscMdReference(visible)) { this.paragraph = false; this.quote = false; continue; }
|
|
809
|
+
const level = nscMdHeading(vt), marker = nscMdMarker(visible);
|
|
810
|
+
if (level > 0) { this.append(nscMdTrim(vt.subarray(level), " \t#", true, true)); this.paragraph = false; this.quote = false; if (this.output.length >= this.capacity) break; continue; }
|
|
811
|
+
if (marker !== null) { this.append(marker.content); this.paragraph = false; this.quote = false; if (this.output.length >= this.capacity) break; continue; }
|
|
812
|
+
if (vt[0] === 62) { let inner = vt.subarray(1); if (inner.length > 0 && inner[0] === 32) inner = inner.subarray(1); inner = nscMdTrim(inner, " \t", true, true); if (!this.quote && inner.length > 0) this.append(inner); if (inner.length > 0) this.quote = true; this.paragraph = false; if (this.output.length >= this.capacity) break; continue; }
|
|
813
|
+
this.quote = false;
|
|
814
|
+
if (visible.length === line.length) {
|
|
815
|
+
const header = nscMdTable(visible), delimiter = lines.peek(), alignments = delimiter === null ? null : nscMdDelimiters(delimiter);
|
|
816
|
+
if (header !== null && alignments !== null && header.length === alignments.length) {
|
|
817
|
+
for (const cell of header) this.append(cell); lines.next();
|
|
818
|
+
for (;;) { const rowLine = lines.peek(); if (rowLine === null) break; const rowTrim = nscMdTrim(rowLine, " \t", true, true); if (rowTrim.length === 0 || nscMdFindByte(rowTrim, 0, 124) < 0) break; lines.next(); const row = nscMdTable(html.visible(rowLine)); if (row !== null) for (const cell of row) this.append(cell); if (this.output.length >= this.capacity) break; }
|
|
819
|
+
this.paragraph = false; if (this.output.length >= this.capacity) break; continue;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
if (this.html(vt)) { if (this.output.length >= this.capacity) break; continue; }
|
|
823
|
+
if (nscMdRule(vt)) { this.paragraph = false; continue; }
|
|
824
|
+
if (!this.paragraph) this.append(visible); this.paragraph = true; if (this.output.length >= this.capacity) break;
|
|
825
|
+
} return this.output;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
class NscMdReader {
|
|
829
|
+
bytes: Uint8Array; wire: DataView; at = 0;
|
|
830
|
+
constructor(bytes: Uint8Array) { this.bytes = bytes; this.wire = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); }
|
|
831
|
+
take(length: number): Uint8Array { if (length > this.bytes.length - this.at) throw new Error("truncated Markdown packet"); const out = this.bytes.subarray(this.at, this.at + length); this.at += length; return out; }
|
|
832
|
+
u32(): number { const at = this.at; this.take(4); return this.wire.getUint32(at, true); }
|
|
833
|
+
f32(): number { const at = this.at; this.take(4); return this.wire.getFloat32(at, true); }
|
|
834
|
+
}
|
|
835
|
+
function nscvMarkdownContentPolicy(request: Uint8Array): Uint8Array {
|
|
836
|
+
const reader = new NscMdReader(request), header = reader.take(4);
|
|
837
|
+
if (header[0] !== 22 || header[1]! > 1 || header[2] !== 1 || header[3]! > 1) throw new Error("invalid Markdown operation");
|
|
838
|
+
const sourceLength = reader.u32(), issueLength = reader.u32(), detailsLength = reader.u32(), imageCount = reader.u32(), capacity = reader.u32();
|
|
839
|
+
if (imageCount > 16 || header[3] === 0 && issueLength !== 0 || header[1] === 1 && (header[3] !== 0 || issueLength !== 0 || detailsLength !== 0 || imageCount !== 0) || header[1] === 0 && capacity !== 0) throw new Error("invalid Markdown request facts");
|
|
840
|
+
const source = reader.take(sourceLength), issue = reader.take(issueLength), detailBytes = reader.take(detailsLength), expanded: boolean[] = [], images: NscMdImage[] = [];
|
|
841
|
+
for (const byte of detailBytes) { if (byte > 1) throw new Error("invalid Markdown expanded flag"); expanded.push(byte === 1); }
|
|
842
|
+
for (let i = 0; i < imageCount; i++) { const lower = reader.u32(), upper = reader.u32(), width = reader.f32(), height = reader.f32(), length = reader.u32(); images.push({ source: reader.take(length), lower, upper, width, height }); }
|
|
843
|
+
if (reader.at !== request.length) throw new Error("trailing Markdown bytes");
|
|
844
|
+
if (header[1] === 1) { const sources = new NscMdDiscovery(capacity).collect(source), out = new NscMdWriter(); out.u32(1); out.u32(sources.length); for (const value of sources) { out.u32(value.length); out.add(value); } return out.finish(); }
|
|
845
|
+
const builder = new NscMdBuilder(expanded, header[3] === 1 ? issue : null, images), blocks = builder.blocks(new NscMdLines(source), false), root = builder.container(false, 12, 0, 0, blocks); return nscMdEncode(builder.nodes, root);
|
|
846
|
+
}
|
|
847
|
+
function nscvMarkdownRecipe(source: Uint8Array, expanded: readonly boolean[], issue: Uint8Array | null, mappings: readonly { source: Uint8Array; image: number; width: number; height: number }[]): number[] {
|
|
848
|
+
const flags: boolean[] = []; for (const flag of expanded) flags.push(flag);
|
|
849
|
+
const images: NscMdImage[] = [];
|
|
850
|
+
for (let i = 0; i < Math.min(mappings.length, 16); i++) {
|
|
851
|
+
const mapping = mappings[i]!, identity = mapping.image;
|
|
852
|
+
if (!Number.isSafeInteger(identity) || identity < 0) throw new Error("Markdown registered image identity is not exact");
|
|
853
|
+
images.push({ source: mapping.source, lower: identity % 4294967296, upper: Math.floor(identity / 4294967296), width: Math.fround(mapping.width), height: Math.fround(mapping.height) });
|
|
854
|
+
}
|
|
855
|
+
const builder = new NscMdBuilder(flags, issue, images), blocks = builder.blocks(new NscMdLines(source), false), root = builder.container(false, 12, 0, 0, blocks), recipe = nscMdEncode(builder.nodes, root), output: number[] = [];
|
|
856
|
+
for (const byte of recipe) output.push(byte); return output;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
function nscvMarkdownRecipeWords(source: Uint8Array, expanded: readonly boolean[], issue: Uint8Array | null, mappings: readonly { source: Uint8Array; image: { imageLower: number; imageUpper: number }; width: number; height: number }[]): number[] {
|
|
860
|
+
const flags: boolean[] = []; for (const flag of expanded) flags.push(flag);
|
|
861
|
+
const images: NscMdImage[] = [];
|
|
862
|
+
for (let i = 0; i < Math.min(mappings.length, 16); i++) {
|
|
863
|
+
const mapping = mappings[i]!, identity = mapping.image;
|
|
864
|
+
if (!Number.isInteger(identity.imageLower) || !Number.isInteger(identity.imageUpper) || identity.imageLower < 0 || identity.imageLower > 4294967295 || identity.imageUpper < 0 || identity.imageUpper >= 2147483648) throw new Error("Markdown registered image identity is not exact");
|
|
865
|
+
images.push({ source: mapping.source, lower: identity.imageLower, upper: identity.imageUpper, width: Math.fround(mapping.width), height: Math.fround(mapping.height) });
|
|
866
|
+
}
|
|
867
|
+
const builder = new NscMdBuilder(flags, issue, images), blocks = builder.blocks(new NscMdLines(source), false), root = builder.container(false, 12, 0, 0, blocks), recipe = nscMdEncode(builder.nodes, root), output: number[] = [];
|
|
868
|
+
for (const byte of recipe) output.push(byte); return output;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/** Discover canonical image sources using the same bounded parser as the view. */
|
|
872
|
+
export interface MarkdownImageSource { readonly source: Uint8Array; }
|
|
873
|
+
export function markdownImageSources(source: Uint8Array, capacity: number): readonly MarkdownImageSource[] {
|
|
874
|
+
if (!Number.isSafeInteger(capacity) || capacity < 0 || capacity > 16) throw new Error("invalid Markdown image capacity");
|
|
875
|
+
const output: MarkdownImageSource[] = [];
|
|
876
|
+
for (const value of new NscMdDiscovery(capacity).collect(source)) output.push({ source: value });
|
|
877
|
+
return output;
|
|
878
|
+
}
|