Skip to content

Commit

Permalink
resolved cozy-blog/issue#64 Prism undefined error (#33)
Browse files Browse the repository at this point in the history
* resolve cozy-blog/issue#64 Prism undefined error

* npm published
  • Loading branch information
Moon-DaeSeung authored Mar 23, 2023
1 parent 7f39e72 commit 549d6f4
Show file tree
Hide file tree
Showing 13 changed files with 1,275 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/svelte-notion-page/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cozy-blog/svelte-notion-page",
"version": "0.0.7",
"version": "0.0.12",
"scripts": {
"dev": "vite dev",
"build": "svelte-kit sync && svelte-package",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
<script lang="ts">
import type { CodeProps } from '$lib/types';
import copyToClipboard from 'clipboard-copy';
import Prism from 'prismjs';
import 'prismjs/themes/prism.css';
import 'prismjs/components/prism-dart';
import 'prismjs/components/prism-typescript';
import 'prismjs/components/prism-kotlin';
import 'prismjs/components/prism-java';
import 'prismjs/components/prism-markdown';
import 'prismjs/components/prism-python';
import 'prismjs/components/prism-sql';
import 'prismjs/components/prism-go';
import 'prismjs/components/prism-elixir';
import RichText from '../base/richtext/RichText.svelte';
import { fade, fly } from 'svelte/transition';
import Prisim from 'prismjs';
import 'prismjs/themes/prism.css';
import loadDart from './support-language/dart';
import loadElixir from './support-language/elixir';
import loadGo from './support-language/go';
import loadJava from './support-language/java';
import loadKotlin from './support-language/kotlin';
import loadMarkdown from './support-language/markdown';
import loadPython from './support-language/python';
import loadSql from './support-language/sql';
import loadTypescript from './support-language/typescript';
export let props: CodeProps;
export let depth: number;
/*
prismjs가 기본적으로 지원하는 언어:
[ 'plaintext', 'markup', 'clike',
'html', 'svg', 'xml', 'css', 'javascript',
]
'plaintext', 'markup', 'clike',
'html', 'svg', 'xml', 'css', 'javascript'
*/
[
loadDart,
loadTypescript,
loadElixir,
loadGo,
loadJava,
loadKotlin,
loadMarkdown,
loadPython,
loadSql
].forEach((load) => load(Prisim));
const {
code: { caption, rich_text: texts, language }
Expand Down Expand Up @@ -70,9 +78,9 @@
</button>
</div>
<code>
{@html Prism.highlight(
{@html Prisim.highlight(
content,
Prism.languages[language] || Prism.languages['plain'],
Prisim.languages[language] || Prisim.languages['plain'],
language
)}
</code>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable no-useless-escape */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function loadDart (Prism: any) {
const keywords = [
/\b(?:async|sync|yield)\*/,
/\b(?:abstract|assert|async|await|break|case|catch|class|const|continue|covariant|default|deferred|do|dynamic|else|enum|export|extends|extension|external|factory|final|finally|for|get|hide|if|implements|import|in|interface|library|mixin|new|null|on|operator|part|rethrow|return|set|show|static|super|switch|sync|this|throw|try|typedef|var|void|while|with|yield)\b/
];

// Handles named imports, such as http.Client
const packagePrefix = /(^|[^\w.])(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;

// based on the dart naming conventions
const className = {
pattern: RegExp(packagePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
lookbehind: true,
inside: {
namespace: {
pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
inside: {
punctuation: /\./
}
}
}
};

Prism.languages.dart = Prism.languages.extend('clike', {
'class-name': [
className,
{
// variables and parameters
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
pattern: RegExp(packagePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()])/.source),
lookbehind: true,
inside: className.inside
}
],
keyword: keywords,
operator: /\bis!|\b(?:as|is)\b|\+\+|--|&&|\|\||<<=?|>>=?|~(?:\/=?)?|[+\-*/%&^|=!<>]=?|\?/
});

Prism.languages.insertBefore('dart', 'string', {
'string-literal': {
pattern: /r?(?:("""|''')[\s\S]*?\1|(["'])(?:\\.|(?!\2)[^\\\r\n])*\2(?!\2))/,
greedy: true,
inside: {
interpolation: {
pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:\w+|\{(?:[^{}]|\{[^{}]*\})*\})/,
lookbehind: true,
inside: {
punctuation: /^\$\{?|\}$/,
expression: {
pattern: /[\s\S]+/,
inside: Prism.languages.dart
}
}
},
string: /[\s\S]+/
}
},
string: undefined
});

Prism.languages.insertBefore('dart', 'class-name', {
metadata: {
pattern: /@\w+/,
alias: 'function'
}
});

Prism.languages.insertBefore('dart', 'class-name', {
generics: {
pattern: /<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/,
inside: {
'class-name': className,
keyword: keywords,
punctuation: /[<>(),.:]/,
operator: /[?&|]/
}
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* eslint-disable no-useless-escape */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function Dart(Prism: any) {
Prism.languages.elixir = {
doc: {
pattern:
/@(?:doc|moduledoc)\s+(?:("""|''')[\s\S]*?\1|("|')(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2)/,
inside: {
attribute: /^@\w+/,
string: /['"][\s\S]+/
}
},
comment: {
pattern: /#.*/,
greedy: true
},
// ~r"""foo""" (multi-line), ~r'''foo''' (multi-line), ~r/foo/, ~r|foo|, ~r"foo", ~r'foo', ~r(foo), ~r[foo], ~r{foo}, ~r<foo>
regex: {
pattern:
/~[rR](?:("""|''')(?:\\[\s\S]|(?!\1)[^\\])+\1|([/|"'])(?:\\.|(?!\2)[^\\\r\n])+\2|\((?:\\.|[^\\)\r\n])+\)|\[(?:\\.|[^\\\]\r\n])+\]|\{(?:\\.|[^\\}\r\n])+\}|<(?:\\.|[^\\>\r\n])+>)[uismxfr]*/,
greedy: true
},
string: [
{
// ~s"""foo""" (multi-line), ~s'''foo''' (multi-line), ~s/foo/, ~s|foo|, ~s"foo", ~s'foo', ~s(foo), ~s[foo], ~s{foo} (with interpolation care), ~s<foo>
pattern:
/~[cCsSwW](?:("""|''')(?:\\[\s\S]|(?!\1)[^\\])+\1|([/|"'])(?:\\.|(?!\2)[^\\\r\n])+\2|\((?:\\.|[^\\)\r\n])+\)|\[(?:\\.|[^\\\]\r\n])+\]|\{(?:\\.|#\{[^}]+\}|#(?!\{)|[^#\\}\r\n])+\}|<(?:\\.|[^\\>\r\n])+>)[csa]?/,
greedy: true,
inside: {
// See interpolation below
}
},
{
pattern: /("""|''')[\s\S]*?\1/,
greedy: true,
inside: {
// See interpolation below
}
},
{
// Multi-line strings are allowed
pattern: /("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
greedy: true,
inside: {
// See interpolation below
}
}
],
atom: {
// Look-behind prevents bad highlighting of the :: operator
pattern: /(^|[^:]):\w+/,
lookbehind: true,
alias: 'symbol'
},
module: {
pattern: /\b[A-Z]\w*\b/,
alias: 'class-name'
},
// Look-ahead prevents bad highlighting of the :: operator
'attr-name': /\b\w+\??:(?!:)/,
argument: {
// Look-behind prevents bad highlighting of the && operator
pattern: /(^|[^&])&\d+/,
lookbehind: true,
alias: 'variable'
},
attribute: {
pattern: /@\w+/,
alias: 'variable'
},
function: /\b[_a-zA-Z]\w*[?!]?(?:(?=\s*(?:\.\s*)?\()|(?=\/\d))/,
number: /\b(?:0[box][a-f\d_]+|\d[\d_]*)(?:\.[\d_]+)?(?:e[+-]?[\d_]+)?\b/i,
keyword:
/\b(?:after|alias|and|case|catch|cond|def(?:callback|delegate|exception|impl|macro|module|n|np|p|protocol|struct)?|do|else|end|fn|for|if|import|not|or|quote|raise|require|rescue|try|unless|unquote|use|when)\b/,
boolean: /\b(?:false|nil|true)\b/,
operator: [
/\bin\b|&&?|\|[|>]?|\\\\|::|\.\.\.?|\+\+?|-[->]?|<[-=>]|>=|!==?|\B!|=(?:==?|[>~])?|[*\/^]/,
{
// We don't want to match <<
pattern: /([^<])<(?!<)/,
lookbehind: true
},
{
// We don't want to match >>
pattern: /([^>])>(?!>)/,
lookbehind: true
}
],
punctuation: /<<|>>|[.,%[\]{}()]/
};

Prism.languages.elixir.string.forEach(function (o: any) {
o.inside = {
interpolation: {
pattern: /#\{[^}]+\}/,
inside: {
delimiter: {
pattern: /^#\{|\}$/,
alias: 'punctuation'
},
rest: Prism.languages.elixir
}
}
};
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable no-useless-escape */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function go(Prism: any) {
Prism.languages.go = Prism.languages.extend('clike', {
string: {
pattern: /(^|[^\\])"(?:\\.|[^"\\\r\n])*"|`[^`]*`/,
lookbehind: true,
greedy: true
},
keyword:
/\b(?:break|case|chan|const|continue|default|defer|else|fallthrough|for|func|go(?:to)?|if|import|interface|map|package|range|return|select|struct|switch|type|const)\b/,
boolean: /\b(?:_|false|iota|nil|true)\b/,
number: [
// binary and octal integers
/\b0(?:b[01_]+|o[0-7_]+)i?\b/i,
// hexadecimal integers and floats
/\b0x(?:[a-f\d_]+(?:\.[a-f\d_]*)?|\.[a-f\d_]+)(?:p[+-]?\d+(?:_\d+)*)?i?(?!\w)/i,
// decimal integers and floats
/(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.\d[\d_]*)(?:e[+-]?[\d_]+)?i?(?!\w)/i
],
operator:
/[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\./,
builtin:
/\b(?:append|bool|byte|cap|close|complex|complex(?:64|128)|copy|delete|error|float(?:32|64)|u?int(?:8|16|32|64)?|imag|len|make|new|panic|print(?:ln)?|real|recover|rune|string|uintptr)\b/
});

Prism.languages.insertBefore('go', 'string', {
char: {
pattern: /'(?:\\.|[^'\\\r\n]){0,10}'/,
greedy: true
}
});

delete Prism.languages.go['class-name'];
}
Loading

0 comments on commit 549d6f4

Please sign in to comment.