Skip to content

Commit

Permalink
feat: update some samples to show more features
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 5, 2024
1 parent aaa56e1 commit 7a741c9
Show file tree
Hide file tree
Showing 12 changed files with 1,574 additions and 209 deletions.
2 changes: 1 addition & 1 deletion packages/tm-grammars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ import { grammars } from 'tm-grammars'
| `mdx` | | [wooorm/markdown-tm-language](https://github.com/wooorm/markdown-tm-language/blob/0229d1b8fc37a7dea8cf7225d7713ac8a69621ca/source.mdx.tmLanguage) | [MIT](https://raw.githubusercontent.com/wooorm/markdown-tm-language/main/license) | `tsx` `toml` `yaml` `c` `clojure` `coffee` `cpp` `csharp` `css` `diff` `docker` `elixir` `elm` `erlang` `go` `graphql` `haskell` `html` `ini` `java` `javascript` `json` `julia` `kotlin` `less` `lua` `make` `markdown` `objective-c` `perl` `python` `r` `ruby` `rust` `scala` `scss` `shellscript` `shellsession` `sql` `xml` `swift` `typescript` | 130.87 kB |
| `mermaid` | | [bpruitt-goddard/vscode-mermaid-syntax-highlight](https://github.com/bpruitt-goddard/vscode-mermaid-syntax-highlight/blob/8b62f487cb7a89afcd152febfbf47f5d4787657f/syntaxes/mermaid.tmLanguage.yaml) | [MIT](https://raw.githubusercontent.com/bpruitt-goddard/vscode-mermaid-syntax-highlight/master/LICENSE) | | 27.89 kB |
| `mojo` | | [modularml/mojo-syntax](https://github.com/modularml/mojo-syntax/blob/46eccdaeda2b1587e9b82808cdb35670f359d16d/syntaxes/mojo.syntax.json) | [MIT](https://raw.githubusercontent.com/modularml/mojo-syntax/main/LICENSE) | | 68.68 kB |
| `move` | | [damirka/move-syntax](https://github.com/damirka/move-syntax/blob/ff3b79424638c8904787cd388b9823b546f5dd79/syntaxes/move.tmLanguage.json) | [MIT](https://raw.githubusercontent.com/damirka/move-syntax/main/LICENSE) | | 18.97 kB |
| `move` | | [damirka/move-syntax](https://github.com/damirka/move-syntax/blob/848d483fb3ead050caddbb8346f5af406df64495/syntaxes/move.tmLanguage.json) | [MIT](https://raw.githubusercontent.com/damirka/move-syntax/main/LICENSE) | | 18.95 kB |
| `narrat` | `nar` | [liana-p/narrat-syntax-highlighting-vscode](https://github.com/liana-p/narrat-syntax-highlighting-vscode/blob/00d4b410338fc50ca0ce77a1f7e873c1fb66d376/syntaxes/narrat.tmLanguage.yaml) | [MIT](https://raw.githubusercontent.com/liana-p/narrat-syntax-highlighting-vscode/main/LICENSE) | | 3.39 kB |
| `nextflow` | `nf` | [nextflow-io/vscode-language-nextflow](https://github.com/nextflow-io/vscode-language-nextflow/blob/7eeb9be8d01556b7c51c59307275c2f720f2ddf4/syntaxes/nextflow.tmLanguage.json) | [MIT](https://raw.githubusercontent.com/nextflow-io/vscode-language-nextflow/master/LICENSE.md) | | 4.40 kB |
| `nginx` | | [hangxingliu/vscode-nginx-conf-hint](https://github.com/hangxingliu/vscode-nginx-conf-hint/blob/49159c02fd09dce264ded7802c9499a0f09646f5/src/syntax/nginx.tmLanguage) | [GPL-3.0](https://raw.githubusercontent.com/hangxingliu/vscode-nginx-conf-hint/main/LICENSE) | `lua` | 34.10 kB |
Expand Down
8 changes: 4 additions & 4 deletions packages/tm-grammars/grammars/move.json
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@
"name": "meta.path.call.move"
},
"module": {
"begin": "\\b(module|spec)\\b",
"begin": "\\b(module)\\b",
"beginCaptures": {
"1": {
"name": "storage.modifier.type.move"
Expand All @@ -895,7 +895,7 @@
"include": "#comments"
},
{
"begin": "(?<=\\b(module|spec)\\b)",
"begin": "(?<=\\b(module)\\b)",
"comment": "Module name",
"end": "(?=[;{])",
"patterns": [
Expand All @@ -906,7 +906,7 @@
"include": "#escaped_identifier"
},
{
"begin": "(?<=\\b(module|spec))",
"begin": "(?<=\\b(module))",
"comment": "Module namespace / address",
"end": "(?=[(::){])",
"name": "constant.other.move",
Expand Down Expand Up @@ -1016,7 +1016,7 @@
"include": "#escaped_identifier"
},
{
"begin": "(?<=\\b(module|spec))",
"begin": "(?<=\\bmodule\\b)",
"comment": "Module namespace / address",
"end": "(?=[(::){])",
"name": "constant.other.move"
Expand Down
8 changes: 4 additions & 4 deletions packages/tm-grammars/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1972,15 +1972,15 @@ export const grammars = [
source: 'https://github.com/modularml/mojo-syntax/blob/46eccdaeda2b1587e9b82808cdb35670f359d16d/syntaxes/mojo.syntax.json',
},
{
byteSize: 19426,
byteSize: 19407,
displayName: 'Move',
lastUpdate: '2024-08-20T09:34:05Z',
lastUpdate: '2024-09-05T13:36:49Z',
license: 'MIT',
licenseUrl: 'https://raw.githubusercontent.com/damirka/move-syntax/main/LICENSE',
name: 'move',
scopeName: 'source.move',
sha: 'ff3b79424638c8904787cd388b9823b546f5dd79',
source: 'https://github.com/damirka/move-syntax/blob/ff3b79424638c8904787cd388b9823b546f5dd79/syntaxes/move.tmLanguage.json',
sha: '848d483fb3ead050caddbb8346f5af406df64495',
source: 'https://github.com/damirka/move-syntax/blob/848d483fb3ead050caddbb8346f5af406df64495/syntaxes/move.tmLanguage.json',
},
{
aliases: [
Expand Down
8 changes: 4 additions & 4 deletions packages/tm-grammars/raw/move.json
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@
"name": "meta.path.call.move"
},
"module": {
"begin": "\\b(module|spec)\\b",
"begin": "\\b(module)\\b",
"beginCaptures": {
"1": {
"name": "storage.modifier.type.move"
Expand All @@ -895,7 +895,7 @@
"include": "#comments"
},
{
"begin": "(?<=\\b(module|spec)\\b)",
"begin": "(?<=\\b(module)\\b)",
"comment": "Module name",
"end": "(?=[;{])",
"patterns": [
Expand All @@ -906,7 +906,7 @@
"include": "#escaped_identifier"
},
{
"begin": "(?<=\\b(module|spec))",
"begin": "(?<=\\b(module))",
"comment": "Module namespace / address",
"end": "(?=[(::){])",
"name": "constant.other.move",
Expand Down Expand Up @@ -1016,7 +1016,7 @@
"include": "#escaped_identifier"
},
{
"begin": "(?<=\\b(module|spec))",
"begin": "(?<=\\bmodule\\b)",
"comment": "Module namespace / address",
"end": "(?=[(::){])",
"name": "constant.other.move"
Expand Down
174 changes: 148 additions & 26 deletions samples/javascript.sample
Original file line number Diff line number Diff line change
@@ -1,29 +1,151 @@
function resolveAfter2Seconds(x) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}

// async function expression assigned to a variable
const add = async function (x) {
const a = await resolveAfter2Seconds(20);
const b = await resolveAfter2Seconds(30);
return x + a + b;
};

add(10).then((v) => {
console.log(v); // prints 60 after 4 seconds.
});
// --- Demonstration of imports ---

// Importing named exports and the default export
import defaultAnimal, { person, add } from './module.js';

// Importing everything from the module as an alias
import * as Module from './module.js';


// --- Demonstration of exports ---

// Named exports
export const person = { name: "Alice", age: 30 };

export function add(x, y = 0) {
return x + y;
}

// Default export
const defaultAnimal = { name: "Default Animal" };
export default defaultAnimal;

// Generator function
export function* idGenerator() {
let id = 0;
while (true) {
yield id++;
}
}

// Using typeof for runtime type checks
console.log(typeof person); // object
console.log(add(2, 3)); // 5
console.log(defaultAnimal); // { name: "Default Animal" }

// Emulating 'satisfies' behavior using runtime checks
function createAnimal(animal) {
if (typeof animal.name === 'string') {
return animal; // Ensures the animal has a 'name' property
}
throw new Error("Animal must have a name");
}

const dog = createAnimal({ name: "Buddy", breed: "Golden Retriever" });
console.log(dog); // { name: 'Buddy', breed: 'Golden Retriever' }

// Generator usage
const generator = Module.idGenerator();
console.log(generator.next().value); // 0
console.log(generator.next().value); // 1
console.log(generator.next().value); // 2

// Handling a generic-like behavior by allowing any type and runtime checks
function identity(arg) {
return arg;
}

let str = identity("Hello");
let num = identity(42);
console.log(str, num); // "Hello", 42

// Emulating default generics by using default parameters
function wrapInArray(value = "") {
return [value];
}

const stringArray = wrapInArray(); // Default is empty string
const numberArray = wrapInArray(42); // Passes 42 explicitly
console.log(stringArray, numberArray); // [""] , [42]

// --- for-of and for-in loops ---

// for-of: Iterates over iterable objects like arrays, strings, maps, etc.
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit); // Outputs: apple, banana, cherry
}

// for-in: Iterates over enumerable properties of an object
const car = { make: "Tesla", model: "Model S", year: 2021 };
for (const key in car) {
if (car.hasOwnProperty(key)) {
console.log(`${key}: ${car[key]}`); // Outputs key-value pairs of car object
}
}

// --- IIFE (Immediately Invoked Function Expression) ---

(function () {
console.log("This IIFE runs immediately after it's defined.");
const privateVar = "I'm private inside the IIFE!";
console.log(privateVar); // Accessing the private variable inside the IIFE
})();

// --- Using a generator to loop indefinitely ---
function* infiniteGenerator() {
let i = 0;
while (true) {
yield i++;
}
}

const gen = infiniteGenerator();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1

// --- Async and Await ---

async function fetchData(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return data;
} catch (error) {
console.error('There has been a problem with your fetch operation:', error);
}
}

// Example usage of async function
fetchData('https://jsonplaceholder.typicode.com/todos/1')
.then(data => console.log(data)) // Outputs fetched data
.catch(error => console.error(error));

// JSDoc type annotations
/**
* @typedef {Object} Task
* @property {string} title
* @property {boolean} completed
*/

/**
* Create a task
* @param {Task} task
* @returns {Task}
*/
function createTask(task) {
return task;
}

// async function expression used as an IIFE
(async function (x) {
const p1 = resolveAfter2Seconds(20);
const p2 = resolveAfter2Seconds(30);
return x + (await p1) + (await p2);
})(10).then((v) => {
console.log(v); // prints 60 after 2 seconds.
const myTask = createTask({
title: "Learn JavaScript",
completed: false,
});
console.log(myTask);

// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/async_function
// Importing everything as a namespace (simulated for demonstration)
console.log(Module.person); // { name: "Alice", age: 30 }
console.log(Module.add(10, 20)); // 30
9 changes: 9 additions & 0 deletions samples/markdown.sample
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,13 @@ $$I = \int \rho R^{2} dV$$
And note that you can backslash-escape any punctuation characters
which you wish to be displayed literally, ex.: \`foo\`, \*bar\*, etc.

| Syntax | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |

- [x] Write the press release
- [ ] Update the website
- [ ] Contact the media

<!--- From http://www.unexpected-vortices.com/sw/rippledoc/quick-markdown-example.html -->
20 changes: 20 additions & 0 deletions samples/mdc.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
title: Introduction
---

::card
---
icon: Icon
Expand All @@ -11,3 +15,19 @@ Default slot
Description slot
::
::

::alert{type="warning" icon="exclamation-circle"}
Oops! An error occurred
::

Hello [World]{style="background-color: var(--color-primary-500)"}!

Hello [World]{style="color: green;" .custom-class #custom-id}!

Attributes work on:

- ![](/favicon.ico){style="display: inline; margin: 0;"} image,
- [link](#attributes){style="background-color: pink;"}, `code`{style="color: cyan;"},
- _italic_{style="background-color: yellow; color:black;"} and **bold**{style="background-color: lightgreen;"} texts.

<!-- from: https://content.nuxt.com/usage/markdown -->
Loading

0 comments on commit 7a741c9

Please sign in to comment.