Skip to content

Commit

Permalink
Merge pull request #355 from nuejs/dev
Browse files Browse the repository at this point in the history
Beta 2
  • Loading branch information
tipiirai authored Sep 12, 2024
2 parents de8abed + f517dbd commit 9f6fba7
Show file tree
Hide file tree
Showing 36 changed files with 418 additions and 965 deletions.
2 changes: 1 addition & 1 deletion packages/glow/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nue-glow",
"version": "0.2.0",
"version": "0.2.1",
"description": "Tiny and powerful Markdown syntax highlighter",
"homepage": "https://nuejs.org/blog/introducing-glow",
"license": "MIT",
Expand Down
26 changes: 11 additions & 15 deletions packages/glow/src/glow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ const COMMON_WORDS = 'null|true|false|undefined|import|from|async|await|package|
const SPECIAL_WORDS = {
cpp: 'cout|cin|using|namespace',
python: 'None|nonlocal|lambda',
go: 'chan|fallthrough',
css: 'important'
go: 'chan|fallthrough'
}

// special rules (growing list)
const RULES = {
css: [
{ tag: 'strong', re: /#[0-9a-f]{3,7}/gi },
{ tag: 'label', re: /!important/gi },
{ tag: 'em', re: /--[\w\d\-]+/gi },
],

json: [
{ tag: 'b', re: /(".+"):/gi },
]
}

Expand All @@ -39,14 +44,13 @@ const HTML_TAGS = [

// HTML tag name
{ tag: 'strong', re: /<([\w\-]+ )/g, shift: true, lang: MIXED_HTML },

{ tag: 'strong', re: /<\/?([\w\-]+)>/g, shift: true, lang: MIXED_HTML },

// ALL CAPS (constants)
{ tag: 'b', re: /\b[A-Z]{2,}\b/g },

// @special
{ tag: 'label', re: /\B@[\w]+/gi },
{ tag: 'label', re: /\B@[\w\-]+/gi },

// char
{ tag: 'i', re: /[^\w •]/g },
Expand Down Expand Up @@ -98,8 +102,8 @@ function elem(name, str) {
}

/*
Markdown code block inside Markdown is so different,
that it requires a special treatment
Markdown/MDX requires a special treatment, because it's so
different from others (not a programming language)
*/
function isMD(lang) {
return ['md', 'mdx', 'nuemark'].includes(lang)
Expand Down Expand Up @@ -230,15 +234,6 @@ export function parseSyntax(str, lang) {
}

str.split(/\r\n|\r|\n/).forEach((line, i) => {

// hack to join lines when there was newline in the middle of a line
const quote = /^("|')/.exec(line)
if (quote && line[1] != quote[0]) {
const prev = lines[lines.length -1]
if (prev?.line) prev.line += '\\n' + line
return
}

if (!comment) {
if (comm_start.test(line)) {
comment = [line]
Expand All @@ -262,6 +257,7 @@ export function parseSyntax(str, lang) {
}
})


return lines
}

Expand Down
38 changes: 30 additions & 8 deletions packages/glow/test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,30 @@ const CSS = `
/* Let's check out CSS code */
.syntax {
border: 1px solid #fff1 !important;
background-color: #20293A;
border-radius: 4px;
background-color: var(--base-600);
border-radius: var(--radius);
margin-bottom: 3em;
header {
border-bottom: 1px solid #fff1;
padding: .7em 1.5em;
@media(width < 900) {
transform: scale(1.1);
filter: blur(4px);
}
@starting-style {
transition: transform 4s;
}
}
`
const JAVASCRIPT = `
"use strict"
// import some UI stuff
import { layout } from 'components/layout'
// environment
const ENV = {
scripts: ['lol.js'],
styles: ['lmao.css'],
styles: ['lma\\no.css'],
desc: undefined
}
Expand Down Expand Up @@ -247,6 +253,20 @@ func main() {
}
`

const JSON = `
{
"author": "John Doe <john.doe@gmail.com>",
"keywords": ["json", "es5"],
"version": 1.5,
"keywords": ["json", "json5"],
"version": 1.7,
"scripts": {
"test": "mocha --ui exports --reporter spec",
"build": "./lib/cli.js -c package.json5",
}
}
`

const JSON5 = `
{
Expand Down Expand Up @@ -669,7 +689,8 @@ await renderPage([
{ title: 'Haskell', code: HASKELL, },
{ title: 'HTML', lang: 'html', code: HTML, },
{ title: 'Java', code: JAVA, lang: 'java' },
{ title: 'JavaScript', code: JAVASCRIPT, lang: 'js numbered', },
{ title: 'JavaScript', code: JAVASCRIPT, lang: 'js', },
{ title: 'JSON', code: JSON, lang: 'json', },
{ title: 'JSON5', code: JSON5, lang: 'json5', },
{ title: 'JSX', code: JSX, lang: 'jsx' },
{ title: 'Julia', code: JULIA, lang: 'julia' },
Expand All @@ -692,7 +713,8 @@ await renderPage([
{ title: 'TypeScript', code: TS, lang: 'ts', },
{ title: 'ZIG', code: ZIG, lang: 'zig', },

] // .filter(el => ['html'].includes(el.lang))
].filter(el => ['js'].includes(el.lang))
// ]

)

Expand Down
Loading

0 comments on commit 9f6fba7

Please sign in to comment.